Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

Counting the number of pixels in a certain range within an ROI

I am using Vision 6.0 for Visual Basic.

I would like to get a count of the number of pixels within a particular region whose value falls within a certain range (e.g., the number of pixels with values between 86 and 255).

Can anyone tell me how I can do this?

Thanks.
0 Kudos
Message 1 of 6
(3,539 Views)
The simplest way would probably be to do a histogram of the ROI, then sum the bins in the range you want.

Bruce
Bruce Ammons
Ammons Engineering
0 Kudos
Message 2 of 6
(3,539 Views)
Thanks for the response. I have been trying to use the Histogram and Histogram2 methods but I can't figure out how to get the count. I have been looking at the examples in the help file, but I'm not having much luck with this. I find the help file doesn't always provide the amount of detail that I need. I am new to using this product, so it's all a bit foreign to me.
0 Kudos
Message 3 of 6
(3,539 Views)
I am using LabVIEW, so I don't know the details for VB. The histogram should return an array of 256 values which correspond to the number of pixels at each intensity level (0-255). If you loop through the indices you want, you should be able to sum them up easily.

Figure out how to do the histogram one step at a time. First do the entire image, which is the default. Once you have that working, figure out how to do the ROI.

Bruce
Bruce Ammons
Ammons Engineering
0 Kudos
Message 4 of 6
(3,539 Views)
bep -

Have you looked at the Histogram example? It should be under the Analysis directory wherever the examples are.

After doing the Histogram like the example does, you will have a CWIMAQHistogramReport object (let's call it HistogramReport) that contains the result of the Histogram. To access the 1-dimensional array containing the count in each bin, use the Histogram property on the CWIMAQHistogramReportItem that the HistogramReport contains.

To use this array, however, you will need to assign it to a variant. So, to find the number of pixels that are in the 5th bin (with the default options, this will be the pixels that have exactly the value 4), do

Dim Histogram As Variant
Dim HistogramReport As New CWIMAQHistogramReport

CWIMA
QVision1.Histogram2 CWIMAQViewer1.Image, HistogramReport
Histogram = HistogramReport(1).Histogram
' Now Histogram(5) contains the number of pixels in the 5th bin.

Let me know if this helps.

Greg Stoll
IMAQ R & D
National Instruments
Greg Stoll
LabVIEW R&D
0 Kudos
Message 5 of 6
(3,539 Views)
Greg,

That seemed to work. The piece I was missing was the "Histogram = HistogramReport(1).Histogram", allowing me to access the values in the bins.

Thanks for the help!
0 Kudos
Message 6 of 6
(3,539 Views)