-
Notifications
You must be signed in to change notification settings - Fork 1
/
window_detect.py
61 lines (48 loc) · 1.99 KB
/
window_detect.py
1
2
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
# Read the image
img = cv2.imread('img5.png')
height, width = img.shape[:2]
# Convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blur_img = cv2.GaussianBlur(gray_img, (5, 5), 0)
# Perform edge detection using Canny
edge_threshold = 10
edges = cv2.Canny(blur_img, threshold1=edge_threshold, threshold2=edge_threshold*3)
# Find contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter and find contours based on area
min_area = (height / 5) * (width / 7)
max_area = (height / 2) * (width / 2)
contour_list = []
offset_pixel = 50
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
area = w * h
if area >= min_area and area <= max_area:
new_contour = 1
for item in contour_list:
# Check if the current contour x and y is within any existing
if (x + offset_pixel > item[0] and
x + w - offset_pixel < item[2] and
y + offset_pixel > item[1] and
y + h - offset_pixel < item[3]):
# Ignore contour
new_contour = 0
break
# Check if current contour is bigger than existing contour
elif (x - offset_pixel < item[0] and
x + w + offset_pixel > item[2] and
y - offset_pixel < item[1] and
y + h + offset_pixel > item[3]):
# Remove the existing contour and add the new one
contour_list.remove(item)
# Store contour bottom left and top right coordinates
if new_contour:
contour_list.append((x, y, x + w, y + h))
for square in contour_list:
cv2.rectangle(img, (square[0], square[1]), (square[2], square[3]), (0, 255, 0), 2)
# Display the image with potential windows
cv2.imshow('Potential Windows', img)
cv2.waitKey(0)
cv2.destroyAllWindows()