import cv2
# Load the image
image = cv2.imread('image.png')
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply thresholding
ret, thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)
# Apply morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
# Apply edge detection
edges = cv2.Canny(opening, 100, 200)
# Find contours
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Filter contours by area and shape
sticker_contours = []
for contour in contours:
area = cv2.contourArea(contour)
perimeter = cv2.arcLength(contour, True)
if area > 100 and area < 1000 and perimeter > 50 and perimeter < 300:
approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True)
if len(approx) == 4:
sticker_contours.append(contour)
# Draw bounding boxes around stickers
for contour in sticker_contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the image with stickers highlighted
cv2.imshow('Stickers', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Note: The specific values used for thresholding, morphological operations, and contour filtering will depend on the specific image and may need to be adjusted for best results.