Write a python program that detects white stickers in images
- User
Write a python program that detects white stickers in images
- Assistant
We can perform the task of detecting white stickers by using the Python OpenCV library which is an open-source computer vision and machine learning library. Here is a Python program that uses OpenCV to detect white stickers in images:
import cv2 import numpy as np def detect_white_stickers(image_path): # Function to display the image def showImage(img, title): cv2.imshow(title, img) cv2.waitKey(0) cv2.destroyAllWindows() # Reading the input image image = cv2.imread(image_path) # Convert input image to grayscale gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Apply Gaussian Blur to the grayscale image blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0) # Threshold the image using Binary Inverse Thresholding (_, threshold_image) = cv2.threshold(blurred_image, 245, 255, cv2.THRESH_BINARY) # Find contours from the threshold image contours, _ = cv2.findContours(threshold_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Draw contours on the input image for contour in contours: contour_area = cv2.contourArea(contour) if contour_area > 50: # Filter out small contours, tweak the value as needed cv2.drawContours(image, [contour], -1, (0, 255, 0), 2) # Display the original image and the image with stickers detected showImage(cv2.imread(image_path), "Original Image") showImage(image, "White Stickers Detected") # Provide the image path image_path = "path/to/your/image.jpg" detect_white_stickers(image_path)Make sure to have the OpenCV library installed before running the code. You can install it using the following command:
pip install opencv-pythonReplace the "path/to/your/image.jpg" with the path of your actual image file. The program reads the input image, converts it to grayscale, applies Gaussian blur, and uses Binary Inverse Thresholding to create a threshold image. Then, it detects the contours in the threshold image and draws them on the original image. It then displays the original image and the image with the detected white stickers. You can adjust the contour area threshold depending on the size of the stickers in your images.