OpenCV in Python can't scan through pixels
Posted
by
Marco L.
on Stack Overflow
See other posts from Stack Overflow
or by Marco L.
Published on 2011-01-14T20:38:04Z
Indexed on
2011/01/15
3:53 UTC
Read the original article
Hit count: 375
Hi everyone, I'm stuck with a problem of the python wrapper for OpenCv. I have this function that returns 1 if the number of black pixels is greater than treshold
def checkBlackPixels( img, threshold ):
width = img.width
height = img.height
nchannels = img.nChannels
step = img.widthStep
dimtot = width * height
data = img.imageData
black = 0
for i in range( 0, height ):
for j in range( 0, width ):
r = data[i*step + j*nchannels + 0]
g = data[i*step + j*nchannels + 1]
b = data[i*step + j*nchannels + 2]
if r == 0 and g == 0 and b == 0:
black = black + 1
if black >= threshold * dimtot:
return 1
else:
return 0
The loop (scan each pixel of a given image) works good when the input is an RGB image...but if the input is a single channel image I get this error:
for j in range( width ):
TypeError: Nested sequences should have 2 or 3 dimensions
The input single channel image (called 'rg' in the next example) is taken from an RGB image called 'src' processed with cvSplit and then cvAbsDiff
cvSplit( src, r, g, b, 'NULL' )
rg = cvCreateImage( cvGetSize(src), src.depth, 1 ) # R - G
cvAbsDiff( r, g, rg )
I've also already noticed that the problem comes from the difference image got from cvSplit...
Anyone can help me? Thank you
© Stack Overflow or respective owner