Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color difference, how to detect it #12694

Open
1 task done
monkeycc opened this issue May 15, 2024 · 1 comment
Open
1 task done

Color difference, how to detect it #12694

monkeycc opened this issue May 15, 2024 · 1 comment
Labels
question Further information is requested

Comments

@monkeycc
Copy link

Search before asking

Question

Color difference, how to detect it
Classification detection cannot detect color difference

Additional

No response

@monkeycc monkeycc added the question Further information is requested label May 15, 2024
@glenn-jocher
Copy link
Member

Detecting color differences is not inherently supported through typical object classification or detection with YOLOv8 models, as these models primarily focus on object shapes and sizes. However, you can approach this challenge from a couple of angles:

  1. Pre-Processing Approach: Manually analyze the pixel RGB values within the detection bounding boxes returned by YOLOv8. Compute color histograms or average color values and use these for your differentiating conditions.

  2. Custom Training: Train a model specifically to recognize different colors as different classes. This involves preparing a dataset where similar objects in different colors are labeled as separate classes (e.g., 'red car' vs. 'blue car').

Here’s a simple code snippet for reading an image and plotting the color distribution of detected objects to help start with the pre-processing approach:

import cv2
from matplotlib import pyplot as plt
from ultralytics import YOLO

# Load the model
model = YOLO('path/to/model.pt')

# Load image and get detections
image = cv2.imread('path/to/image.jpg')
results = model(image)

# For each detection, calculate and plot histogram
for i, det in enumerate(results.pandas().xyxy[0].to_numpy()):
    x1, y1, x2, y2 = map(int, det[:4])
    cropped_image = image[y1:y2, x1:x2]
    for j, color in enumerate(['b', 'g', 'r']):
        hist = cv2.calcHist([cropped_image], [j], None, [256], [0, 256])
        plt.plot(hist, color=color)
    plt.title(f'Color Distribution for Detection {i+1}')
    plt.show()

This script loads an image, detects objects using a YOLO model, crops these objects out, and then plots color histograms for each detected object. Customize and expand upon this code based on specific application needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants