Create cv.py
Browse files
cv.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|