Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Locating color in video frame - python

I'm trying to locate a given color within the frame and compare its location to a given value, ideally I would like a color range. Right now I would like to know if it's on the left or the right hand side of the frame. I'm able to do this for a single frame using a really slow search method and I feel like there has to be a more efficient way of doing this using an opencv function.

Here's what I'm currently using

# Searches an image for a given color and plots results


import cv2 
import pylab as p

# Import the image
im = cv2.imread('orange.jpg')

width = len(im[0,:])
height = len(im[:,0])

# Define color
B = 34
G = 36
R = 36

result_x = []
result_y = []
for i in range(height):
for j in range(width):
    for k in range(2):
        if (im[i,j,0]==B and im[i,j,1]==G and im[i,j,2]==R):

            result_x.append(j)
            result_y.append(i)


p.plot(result_x,result_y,'ro')
p.xlim(0,width)
p.ylim(0,height)
p.gca().invert_yaxis()
p.show()

Locating color in video frame - python

I'm trying to locate a given color within the frame and compare its location to a given value, ideally I would like a color range. Right now I would like to know if it's on the left or the right hand side of the frame. I'm able to do this for a single frame using a really slow search method and I feel like there has to be a more efficient way of doing this using an opencv function.

Here's what I'm currently using

# Searches an image for a given color and plots results


import cv2 
import pylab as p

# Import the image
im = cv2.imread('orange.jpg')

width = len(im[0,:])
height = len(im[:,0])

# Define color
B = 34
G = 36
R = 36

result_x = []
result_y = []
for i in range(height):
 for j in range(width):
     for k in range(2):
         if (im[i,j,0]==B and im[i,j,1]==G and im[i,j,2]==R):

             result_x.append(j)
             result_y.append(i)


p.plot(result_x,result_y,'ro')
p.xlim(0,width)
p.ylim(0,height)
p.gca().invert_yaxis()
p.show()