Issue
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:
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.
Solution
Avoid loops, use numpy
!
You can use numpy's argwhere()
. The statement returns an array of coordinates where a certain condition is satified.
a = np.argwhere(image == [0,255,0])
# returns array a with three columns,
# since you need only the coordinates, you can delete the third column:
a = np.delete(a, 2, 1) # delete third column in a
a
would return following sample:
array([[ 0, 18],
[ 0, 19],
[ 0, 21],
...,
[539, 675],
[539, 677],
[539, 677]], dtype=int64)
The above returns coordinates where the green pixel values [0, 255, 0]
are present.
Based on the returned coordinates, you can filter the ones closer to the bottom of the image.
Answered By - Jeru Luke
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.