Update cv.py
Browse files
cv.py
CHANGED
|
@@ -1,34 +1,46 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|