11-22-2013 10:11 AM - edited 11-22-2013 10:12 AM
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)
11-23-2013 09:01 AM
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
11-23-2013 09:25 AM
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.
11-25-2013 03:01 AM - edited 11-25-2013 03:30 AM
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.
11-25-2013 06:27 AM
11-25-2013 06:40 AM
The problem is we already bought NI and Halcon is not for free.
So i have to do this with the NI-Vision.
11-25-2013 04:15 PM
11-26-2013 04:04 AM
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
11-26-2013 05:10 AM
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
12-02-2013 08:05 AM - edited 12-02-2013 08:05 AM
Hello,
do you have any function in C# (NI Vision) for pixel access to your image? Something like:
or
IMAQ GetPixelValue VI?
Best regards,
K