pythonopencvobject-detectioncolor-detection

How to find the pixel values of objects detected from yolo in python?


I am using yolo for image detection on my custom dataset, where i train to identiy certain object in the dataset. And post training, the algorithm correctly predicted these objects in the images. I would like to get the rbg values of the predicted objects alone, in opencv i didnot find a way to check the rgb values for objects inside a detected box. In the sample image attached, i would like to see the rgb value of the cup detected.

enter image description here


Solution

    1. Get a object's bounding box coordinate

    For example, result = [x, y, w, h] or [x1, y1, x2, y2]

    1. Check image to get RGB value by using bbox coordinate

    For example, if your result is [x1, y1, x2, y2] and your image shape is img[640,480,3]

    rgb_values = img[x1:x2, y1:y2, 0:2]

    you will get rgb value of object you detect