43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import cv2
|
|
|
|
# Load the image
|
|
image = cv2.imread("mid_image.jpg")
|
|
|
|
# Convert image to grayscale
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# Resize the image to half its original size for display (pop up window)
|
|
scale_percent = 20 # percent of original size
|
|
width = int(gray.shape[1] * scale_percent / 100)
|
|
height = int(gray.shape[0] * scale_percent / 100)
|
|
dim = (width, height)
|
|
|
|
resized_gray = cv2.resize(gray, dim, interpolation=cv2.INTER_AREA)
|
|
|
|
# Show resized image
|
|
cv2.imshow("Detected Rectangle", resized_gray)
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
"""# Apply Gaussian blur
|
|
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
|
|
|
# Run Canny edge detector
|
|
edges = cv2.Canny(blur, 50, 150)
|
|
|
|
# Find contours
|
|
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
|
|
|
# Select the largest contour (presumed to be the rectangle)
|
|
contour = max(contours, key=cv2.contourArea)
|
|
|
|
# Draw the detected contour on a copy of the original image
|
|
output = image.copy()
|
|
cv2.drawContours(output, [contour], -1, (0, 255, 0), 3)
|
|
|
|
# Save or show the result
|
|
cv2.imwrite("rectangle_edges.jpg", output)
|
|
cv2.imshow("Detected Rectangle", output)
|
|
cv2.waitKey(0)
|
|
cv2.destroyAllWindows()""" |