LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculating the ratio of black and white pixels in a binary image

Solved!
Go to solution

Hello,

I would like to calculate the ratio between my black and white pixels in a binary image.

 

I managed to get the histogram report which shows me a the amout of pixels for 0 and 255. Moreover, I can see the number of all pixels in the image.

 

Is there a possibilty to calculate the ratio?

 

Thank you!


Unbenannt 5.PNG

0 Kudos
Message 1 of 10
(4,086 Views)

Unbundle the histogram array and just use the first and last elements (Index Array and Delete From Array will make this simple).  Then you just divide.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 10
(4,082 Views)

I'd say if you divide the mean value by 255, you should get the b/(b+w) ratio.

 

So (mean/255)/(1-mean/255) is the b/w ratio. So mean/(255-mean) should give you the b/w ratio as well. If I did the math right...

 

In both cases you have to be careful. If all pixels are white or black, you can get a divide by zero, resulting in Nan.

0 Kudos
Message 3 of 10
(4,058 Views)

Thanks for your answer. It works, but you calculated the w/(w+b) with mean/(255-mean). Thanks for the idea and your help. 

 


@crossrulz wrote:

Unbundle the histogram array and just use the first and last elements (Index Array and Delete From Array will make this simple).  Then you just divide.


I do not really know what you mean. I am new with LabVIEW and have not really figured out the arrays yet. 

 

This is how my block diagram looks like atm. If I replace the histogram by an array, than I do not know how to read out the array to get the number of pixels for the calculation.

 

Unbenannt 6.PNG

0 Kudos
Message 4 of 10
(4,018 Views)

@Christinaa wrote:

Thanks for your answer. It works, but you calculated the w/(w+b) with mean/(255-mean). Thanks for the idea and your help. 

Ah, yes. In my head 255 was black, not sure why.

 

Crossrulz means you can use an unbunble (use ctrl+space and type "unbundle", pick "unbundle by name") to get the histograph's array. Then you can use array index to get the first (element 0) and second (element 1) element.

 

While you're getting used to LabVIEW, try to straighten those wires right from the start. Try keeping the error line perfectly horizontal. For what you have now being messy works, but when it grows you'll have a huge problem.

 

 

0 Kudos
Message 5 of 10
(4,011 Views)

Oh thanks for the advice, I did not know that.

 

I tried this now but it still doesn't work. Can you tell me what my mistake is? Thanks a lot!! 

 

 

Unbenannt 7.PNG

0 Kudos
Message 6 of 10
(3,991 Views)
Solution
Accepted by topic author Christinaa

Click the unbundle, and change the value you are reading from "Starting Value" to "histogram".

 

"histogram" is an array. You need to use the index array (already there) on that array. The delete from array is not needed anymore, but you can use it as an alternative to get the last element (when arrays have more then two elements). Not sure if your array is only two elements...

 

Note that the "0" and "1" can be removed from the index array, since those are the default values. First index is "0", next is subsequent. When you wire an index, next one is always subsequent. But you can keep it there, it doesn't hurt either.

0 Kudos
Message 7 of 10
(3,985 Views)
Solution
Accepted by topic author Christinaa

wiebe@CARYA wrote:  The delete from array is not needed anymore, but you can use it as an alternative to get the last element (when arrays have more then two elements). Not sure if your array is only two elements...

Yeah, I was not sure how many elements were in the histogram.  For 2 elements, just use the Index Array.  If more, then use Index Array to get the first element (black) and Delete From Array with just the array connected and the "deleted element" will be the last element in the array (white).


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 8 of 10
(3,973 Views)

Thank you it worked with Index Array and Delete From Array. 

 

I still cannot do it without the Delete from Array, than the result is always one. But it works the other way! 

 

0 Kudos
Message 9 of 10
(3,965 Views)

I think there are 256 values in the histogram. Start 0, end 255, interval 1... So delete will give you the last element. If you want to use index array, wire a 0 and a 255 to the two indices. That should give you the first and last as well.

 

If not, right click the array indicator (not the element in it) on the front panel, and select Advanced>Select Last Element. Use that index (should be 255).

0 Kudos
Message 10 of 10
(3,955 Views)