jkushwaha commited on
Commit
066c230
·
verified ·
1 Parent(s): 2588959

Update cv.py

Browse files
Files changed (1) hide show
  1. cv.py +43 -31
cv.py CHANGED
@@ -1,34 +1,46 @@
1
  import cv2
2
  import numpy as np
3
 
4
- # Read the image
5
- image = cv2.imread('your_image_path.jpg')
6
-
7
- # Convert the image to grayscale
8
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9
-
10
- # Apply GaussianBlur to reduce noise and improve edge detection
11
- blurred = cv2.GaussianBlur(gray, (5, 5), 0)
12
-
13
- # Perform edge detection using Canny
14
- edges = cv2.Canny(blurred, 50, 150)
15
-
16
- # Find contours in the edge-detected image
17
- contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
18
-
19
- # Loop over the contours
20
- for contour in contours:
21
- # Calculate the area of the contour
22
- area = cv2.contourArea(contour)
23
-
24
- # If the area is small (indicating a small patch), draw a bounding box around it
25
- if area < 500: # Adjust this threshold according to your needs
26
- x, y, w, h = cv2.boundingRect(contour)
27
- cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
28
-
29
- # Save or display the image with detected patches
30
- cv2.imwrite('detected_patches.jpg', image)
31
- # Or to display:
32
- # cv2.imshow('Detected Patches', image)
33
- # cv2.waitKey(0)
34
- # cv2.destroyAllWindows()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import cv2
2
  import numpy as np
3
 
4
+ def find_black_roi(image_path, threshold_area=1000):
5
+ # Read the image
6
+ image = cv2.imread(image_path)
7
+ if image is None:
8
+ print("Error: Image not found.")
9
+ return
10
+
11
+ # Convert image to grayscale
12
+ grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
13
+
14
+ # Apply binary thresholding to separate black patches
15
+ _, thresholded = cv2.threshold(grayscale, 30, 255, cv2.THRESH_BINARY)
16
+
17
+ # Find contours
18
+ contours, _ = cv2.findContours(thresholded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
19
+
20
+ # Filter contours based on area
21
+ roi_list = []
22
+ for contour in contours:
23
+ area = cv2.contourArea(contour)
24
+ if area > threshold_area:
25
+ # Get bounding box coordinates
26
+ x, y, w, h = cv2.boundingRect(contour)
27
+ roi = image[y:y+h, x:x+w] # Extract the ROI
28
+ roi_list.append(roi)
29
+
30
+ return roi_list
31
+
32
+ # Example usage
33
+ image_path = "example_image.jpg"
34
+ rois = find_black_roi(image_path)
35
+
36
+
37
+
38
+ import matplotlib.pyplot as plt
39
+ import cv2
40
+
41
+ def display_image(image):
42
+ plt.imshow(image)
43
+ plt.axis('off') # Hide axis
44
+ plt.show()
45
+
46
+ display_image(gray)