gpt-4-0314· openai
Suzhen/CodeChat
Write a program that detects white stickers on cardboard boxes
- User
Write a program that detects white stickers on cardboard boxes
- Assistant
I suggest using Python and OpenCV library for image processing and computer vision tasks to detect white stickers on cardboard boxes. Here's a sample code to get you started:
import cv2 import numpy as np def detect_white_sticker(image_path): # Load image image = cv2.imread(image_path) # Convert image to grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to remove noise blurred = cv2.GaussianBlur(gray, (5, 5), 0) # Detect edges using Canny edges = cv2.Canny(blurred, 50, 150) # Find contours in the image contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Loop over the contours for contour in contours: # Get the bounding rectangle for the contour x, y, w, h = cv2.boundingRect(contour) # Calculate the aspect ratio of the bounding rectangle aspect_ratio = float(w) / h # Check if the bbox is square-like (aspect ratio close to 1) and has a minimum size if 0.8 <= aspect_ratio <= 1.2 and w >= 10 and h >= 10: # Crop the region of interest from the original image roi = image[y:y+h, x:x+w] # Calculate average color of the region average_color = np.mean(roi, axis=(0, 1)) # If the average color is white (values close to 255) if all(val >= 240 for val in average_color): # Draw a rectangle around the detected sticker cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) # Show the output image with detected stickers cv2.imshow('Detected Stickers', image) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == "__main__": image_path = "path/to/your/image.jpg" detect_white_sticker(image_path)