10-24-2017 12:25 PM
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!
Solved! Go to Solution.
10-24-2017 12:36 PM
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.
10-24-2017 01:38 PM - edited 10-24-2017 01:41 PM
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.
10-25-2017 04:18 AM
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.
10-25-2017 04:39 AM
@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.
10-25-2017 07:41 AM
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!!
10-25-2017 08:02 AM
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.
10-25-2017 09:44 AM - edited 10-25-2017 09:45 AM
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).
10-25-2017 10:56 AM
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!
10-25-2017 11:25 AM
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).