Ask Your Question
0

How to compute tresholded (black) pixels after binarization?

asked 2015-06-08 08:06:55 -0600

Mat29 gravatar image

updated 2015-06-08 08:08:24 -0600

How to compute tresholded (black) pixels after binarization?

Here is a Python code:

 img = cv2.imread('imageexperiment.tif',0)

 imageone = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

Now i want somehow to compute number of black pixels (stained area)

edit retag flag offensive close merge delete

Comments

imageone .rows*imageone .cols-sum(imageone )

LBerger gravatar imageLBerger ( 2015-06-08 08:11:52 -0600 )edit

This question has already been answered more than once...

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-06-08 10:52:23 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-06-08 08:17:19 -0600

You want to compute the number of pixels not null, there is a function for that: [countNonZero] (http://docs.opencv.org/modules/core/d...)

edit flag offensive delete link more

Comments

arearesult = cv2.countNonZero(imageone), probelm is that it asks a numerical array, not just an image, I should convert the image somehow?

Mat29 gravatar imageMat29 ( 2015-06-08 08:30:32 -0600 )edit

That's because cv2.threshold returns 2 arguments (see the doc). Change your line with

ret, imageone = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
arearesult = cv2.countNonZero(imageone)

and it should work fine.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2015-06-08 08:41:56 -0600 )edit

@Mathieu: Now I am confused: what is the retval? @Mat29: If you want to have the number of black pixels, you can subtract the arearesult from the imagearea; because the countNonZero funciton is returning (as expected) the number of non-zero pixels (that is the number of white pixels)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-06-08 10:59:08 -0600 )edit
1

You're right, you need to subtract the image size to the number of non zero pixels. retval is indeed undocumented, but It seem it is the threshold value.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2015-06-09 02:30:24 -0600 )edit

Thanks, If I`ll perform deconvolution will I compute the number of pixels I need (stained area of lymphatic vessels)?

Mat29 gravatar imageMat29 ( 2015-06-09 03:50:00 -0600 )edit

I suggest to make another question for that and add the code, too. Or just test it and tell us what have you done and if it has changed ;)

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-06-10 02:21:54 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-06-08 08:06:55 -0600

Seen: 101 times

Last updated: Jun 08 '15