Ask Your Question
0

opencv pixel locations

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

Bradley Powers gravatar image

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

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
4

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

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
0

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

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(),
                numpy.where(array==numpy.asarray(img).max())[1].tolist())

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

Thanks!

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

Stats

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

Seen: 4,469 times

Last updated: Jul 18 '11