Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple square detection initialized #1270

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import cv2 as cv
import numpy as np

image = cv.imread('./test_images/dock_blue1.jpg')

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

# Apply Gaussian blur to the image
blurred = cv.GaussianBlur(gray, (5, 5), 0)

# Use Canny edge detection
edges = cv.Canny(blurred, 50, 150)

# Find contours in the edged image
contours, _ = cv.findContours(edges, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

# Loop over the contours
for contour in contours:
# Approximate the contour to a polygon
epsilon = 0.02 * cv.arcLength(contour, True)
approx = cv.approxPolyDP(contour, epsilon, True)

# If the approximated contour has 4 vertices, it's a square (or rectangle)
if len(approx) == 4:
x, y, w, h = cv.boundingRect(approx)

# Calculate the aspect ratio
aspect_ratio = float(w) / h

# Check if the aspect ratio is close to 1 (square)
if 0.95 <= aspect_ratio <= 1.05:
cv.drawContours(image, [approx], -1, (0, 255, 0), 2)

# Display the result
cv.imshow("Squares Detected", image)
cv.waitKey(0)
cv.destroyAllWindows()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading