Home > Enterprise >  Is there a more efficient way to search an image for a specific pixel?
Is there a more efficient way to search an image for a specific pixel?

Time:05-19

I am currently working on an computer vision project with python and openCV.

I need to search in an image with a size of about 620x420 pixels for the green pixel, which is the closest to the bottom of the image. There are multiple green pixels (e.g. 100) in the image from previous contour analysis.

Here is an example image:

Here is an example image

I already implemented it with the following code:

    # Search for the most bottom contour point
    xLowestContour = 0                         # x coordinate of the lowest contour point
    yLowestContour = 0                         # y coordinate of the lowest contour point
    for y in range(roi_h):                     # roi_h is the heigth of the image
        for x in range(roi_w):                 # roi_w is the width of the image
            (b, g, r) = roi_copy[y, x]         # roi_copy is the actual image
            if g == 255 and b == 0 and r == 0:
                xLowestContour = x
                yLowestContour = y

This code works. But there is a big problem with it. It looks like that this way of searching for a specific pixel in an image is very inefficient. The framerate drops from 25 FPS to 2 FPS with this codesnippet. The CPU utilization is only at 10 % when using this codesnippet.

Is there a more efficient way to do this operation? I would also like to utilize more CPU power and achieve a higher framerate.

CodePudding user response:

Avoid loops, use numpy!

You can use numpy's enter image description here

Example output: enter image description here

  • Related