Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

C# - How to check if image is black/very dark

Hello,

i have a working C#2008-program that currently checks if there is a round circle on a picture.

Now i need to create a new function that gets the same input but should just check if the picture is black / very dark.

 

My current code looks like this:

 

//private NationalInstruments.CWIMAQControls.CWIMAQImage IMAG_Mask = new NationalInstruments.CWIMAQControls.CWIMAQImage();

//private NationalInstruments.CWIMAQControls.CWIMAQCirclesReport PARA_CirclesReport = new NationalInstruments.CWIMAQControls.CWIMAQCirclesReport();


 public void CircleDetect()
        {
            short minRad = 1;
            short maxRad = 4;

            axCWIMAQVision1.Circles(IMAG_Mask, axCWIMAQViewer5.Image, PARA_CirclesReport, minRad, maxRad);
            IMAG_CircleDetection = axCWIMAQViewer5.Image;

            if (PARA_CirclesReport.Count == 1)
            {
                //cut

                if (PARA_CirclesReport.get_Radius(1) >= minRadius)
                    cirFound = true;
                else
                    cirFound = false;
            }
            else
                cirFound = false;
        }

 

Because the circle-detection is not very stable i want to add a new function that simply takes the already masked image and returns the mean-value of the brightness.

So if there is no LED on the picture then the function gets a black/very dark picture.

 

How can this be done?

 public void CreateAverageBrightness()

{

// ???

}

 

Thanks for tips

 

Using Microsoft Visual C# 2008 Express Edition, XP, NI Vision Development 8.6)

0 Kudos
Message 1 of 14
(8,102 Views)

In LabVIEW there is a Quantify function, which returns a lot of information about the image, including average brightness and standard deviation.  Either one of these would be good for determining if there is enough detail in your image to proceed.

 

I'm not sure if you would have the same function available in C#.  If not, you could just loop through the image pixels, add them up, and divide by the total count.

 

Bruce

Bruce Ammons
Ammons Engineering
Message 2 of 14
(8,087 Views)

Good catch, Bruce.  In case that counterpart for "IMAQ Quantify" is not available, "IMAQ Histogram" produces almost the same output. Both allow to specify an image mask - in case you just want a part of the image analysed.

Message 3 of 14
(8,086 Views)

Thx for the ideas.

Someone here who knows how to do this in C#?

 

The main-task is to use the attached picture.

Pass wih parameters the x/y/Width/High-position of one circle (fixed position).

Check of there is "something" or not.

 

The current function with circle-detection is not working as the circle is not a "good" circle because the contour is not straight.

So the new idea is to just check if the given position is black.

0 Kudos
Message 4 of 14
(8,073 Views)
If I were you, I use for C# programming not NI products but other MV libraries/products (e.g. Halcon). C# in NI vision is total useless and waste of time.
0 Kudos
Message 5 of 14
(8,062 Views)

The problem is we already bought NI and Halcon is not for free.

So i have to do this with the NI-Vision.

0 Kudos
Message 6 of 14
(8,054 Views)
I understand. In that case i wish good luck and lots of patience.
0 Kudos
Message 7 of 14
(8,040 Views)

Hello,

 

I don't know a lot of C# and Labview combo, but if I would pass my Labview image pointer to C++, I would do something like this (the suggestion Mr. Ammons provided - sum of the values inside the specified mask, divided by total count):

 

#include <iostream>
using namespace std;

const unsigned char my_array[5][6] = {
	{1, 2, 3, 4, 5, 6},
	{7, 8, 9, 10, 11, 12},
	{13, 14, 15, 16, 17, 18},
	{19, 20, 21, 22, 23, 24},
	{25, 26, 27, 28, 29, 30}
};

int main()
{	
	double sum = 0;

	// width and height of the data
	int W = 6, H = 5;
	// mask width and height (mask width = xR-xL, height = yB-yT)
	int xL = 2, xR = 5;
	int yT = 1, yB = 3;
	// pointer to data
	unsigned char *pD = (unsigned char*) &my_array;	
	
	for(int j=yT;j<yB;j++) {
		for(int i=xL;i<xR;i++) {
			sum += (double) pD[j*W+i];
			//cout << "index = " << (int) j*W+i << " value = " << (int) pD[j*W+i] << endl;				
		}
	}
	sum = sum/((xR-xL)*(yB-yT));
	cout << "mean value = " << (double) sum << endl;				
	cin.get();


	return 0;
}

 

I guess that C# should not be much different. You just need to figure out how to create a pointer to your Labview image data.

 

Alternatively, you could create a Labview DLL and call it in C#. I suppose this should work (so far, I have only called C++ DLL's in .NET before, so I am not positive about this).

 

Best regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
0 Kudos
Message 8 of 14
(8,027 Views)

Hi Klemen,

thank you for the example.

 

But: Is someone here that knows about how to to this in C# - better: what exactly to do in my code above to do this?

I have never done something with Vision before...

 

Thanks for the help

0 Kudos
Message 9 of 14
(8,022 Views)

Hello,

 

do you have any function in C# (NI Vision) for pixel access to your image? Something like:

 

IMAQ GetImagePixelPtr VI 

 

or

 

IMAQ GetPixelValue VI?

 

Best regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
0 Kudos
Message 10 of 14
(7,973 Views)