n
unknown
plain_text
2 years ago
1.5 kB
8
Indexable
import cv2
import numpy as np
# Define function to find contours and filter for tray edges
def find_tray_edges(image):
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding to separate tray from background
# Adjust threshold value based on your image lighting
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Filter contours based on size and aspect ratio for tray edges
tray_edges = []
for cnt in contours:
approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
if len(approx) == 4: # Check for quadrilateral shape
x, y, w, h = cv2.boundingRect(approx)
aspect_ratio = float(w) / h
# Adjust size and aspect ratio thresholds based on your tray dimensions
if w > 50 and h > 50 and 0.8 < aspect_ratio < 1.2: # Adjust thresholds for tray size
tray_edges.append(approx)
return tray_edges
# Load image (replace 'path/to/image.jpg' with your image path)
image = cv2.imread('path/to/image.jpg')
# Find tray edges
tray_edges = find_tray_edges(image.copy())
# Draw edges on the image (optional)
if tray_edges:
for edge in tray_edges:
cv2.drawContours(image, [edge], -1, (0, 255, 0), 2) # Draw green lines on edges
# Display the image with identified edges (optional)
cv2.imshow('Image with Tray Edges', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Editor is loading...
Leave a Comment