Saturday, May 11, 2024

Evolving from Edge Detection to Object Detection: Exploring Color Identification

 In the realm of computer vision, the journey from edge detection to object detection is an exciting evolution, marking significant progress in image processing and machine learning. Today, we'll explore this journey as we enhance a simple edge detection program to identify objects and discern their colors.

Sample Output:




Note that the detected object was enclosed in a rectangle with dimensions extended 10 pixels beyond the outermost detected pixels around the contour. And also note that the program assumes that the object has no other contours identified inside of it and it has solid and uniformly distributed color, hire me for more complex images.

 If you want to save the detected object from the image assuming that there are several objects detected in the original image, you can use the following code:

# Crop the region of interest (ROI)
    roi = image[y:y+h, x:x+w]

    # Save the cropped image
    cv2.imwrite("cropped_image.jpg", roi)

Edge Detection: The Starting Point

We begin our journey(see previous post "Edge Detection with Python and OpenCV: A Simple Guide") with a classic edge detection program. Using OpenCV, we load an image and apply the Canny edge detection algorithm. This process extracts edges from the image, highlighting the boundaries between different objects.

Progression to Object Detection

As we inspect our edge detection program, we notice its potential to evolve into an object detection tool. By identifying contours and drawing bounding boxes around objects, we take a step closer to this transformation.

Introducing Color Identification

To enrich our program, we integrate color identification. Utilizing the average color within the bounding box, we determine the predominant color of each detected object and display it alongside its RGB values.

Conclusion: Towards Image Classification

Our journey from edge detection to object detection, supplemented by color identification, illustrates the iterative nature of computer vision development. As we refine our techniques, we inch closer to the ultimate goal: enabling machines to perceive and understand visual information, laying the groundwork for advanced tasks like image classification and or optical character recognition(OCR) in machine learning.

Cheers to the endless possibilities that lie ahead in the realm of computer vision and artificial intelligence!

Here is the code:

import cv2
import numpy as np

# Load the image
image = cv2.imread("ball.jpg")

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply edge detection
edges = cv2.Canny(gray, 100, 200)  # You can adjust the thresholds as needed

# Find contours
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Iterate through detected contours
for contour in contours:
    # Get the bounding box of the contour
    x, y, w, h = cv2.boundingRect(contour)
    # Identify the color of the object
    roi = image[y:y+h, x:x+w]
    roi_rgb = cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)
    mask = np.all(roi_rgb == [255, 2255, 255], axis=-1)
    mask = ~mask
    roi_without_color = roi_rgb[mask]
    avg_color = np.average(roi_without_color, axis=0)
    print(roi_without_color)
    
    color = tuple(np.round(avg_color).astype(int))

    # Enlarge the rectangle by 10 pixels
    x -= 10
    y -= 10
    w += 20
    h += 20
    
    # Draw a rectangle around the contour
    cv2.rectangle(image, (x, y), (x + w, y + h), (128, 64, 0), 1)
    
    
    
    # Display the color name and RGB values
    color_name = ('RGB: ' + str(color[0]) + ', '  +  str(color[1])+ ', '  + str(color[2]))
    
    
    cv2.putText(image, f"{color_name}", (x+5, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (128, 0, 0), 2)
    # Draw the detected edges in blue color with thickness 1
    cv2.drawContours(image, contours, -1, (255, 0, 0), 1)
# Display the result
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

No comments:

Post a Comment