Ask Your Question

opencv pixel locations

asked 2011-07-18 04:31:50 -0500

Bradley Powers gravatar image

updated 2014-01-28 17:10:03 -0500

ngrennan gravatar image

Hi all,

I have an image that looks like this: Skeleton Image

From this image, I want to get a list of all of the pixel locations for pixels which are nonzero (white). I'm using OpenCV in Python, and I don't have a good sense of how.

Thanks, Bradley Powers

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2011-07-18 06:01:10 -0500

abroun gravatar image

If you haven't already, I'd suggest looking at NumPy for manipulating your OpenCV images in Python. If you load your images as CvMat objects instead of IplImages then you can manipulate them as NumPy objects without copying the data.

NumPy takes a while to get your head around, but once you do you can use techniques such as the ones given here to achieve what you want.

Based on that, running this program

#! /usr/bin/env python

import cv
import numpy

cvImg = cv.LoadImageM( "testImage.png",     # Must use LoadImageM
    iscolor=cv.CV_LOAD_IMAGE_GRAYSCALE )
npImg = numpy.asarray( cvImg )  # No copying takes place

coordList = numpy.argwhere( npImg == 255 )
numWhitePoints = len( coordList )

print coordList
print "Found {0} points".format( numWhitePoints )

on your image should output like this

[[ 97 318]
 [ 97 322]
 [ 97 323]
   .. ..
   .. ..
 [141 200]
 [146 189]
 [153 178]]
Found 216 points
edit flag offensive delete link more

answered 2011-07-18 07:13:29 -0500

Bradley Powers gravatar image

Looks good. I actually figured this out as well, with slight modification. It turns out that I want these as a list of tuples, so:

def bool_img_to_idx(img):
     return zip(numpy.where(array==numpy.asarray(img).max())[0].tolist(),

My Intro Computer Science prof (who taught me Python) would be rolling over in his grave if he were dead.


edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-07-18 04:31:50 -0500

Seen: 5,059 times

Last updated: Jul 18 '11