KarunP
KarunP

Reputation: 1

Can't acess Image<Bgr, byte>.Data property in C# emgucv

I could access each pixel of a Gray image using Data[,,] but cannot do so for Bgr image. I have written the following code:

Image<Bgr, byte> currentFrame = capture.QueryFrame();
Image<Gray, byte> grayFrame = currentFrame.Convert<Gray, byte>();
Byte gray = grayFrame.Data[0, 10, 0];
Byte blue = currentFrame.Data[0, 10, 0];

which throws an exception: Object reference not set to an instance of an object.

I checked by adding breakpoint and the result was this:

currentFrame.Data is nul
grayFrame.Data has 3d array
gray has value 71

and then the next line caused error

Why is the currentFrame.Data, which should have been a 3d array, null? How can I access Image.Data property for Bgr image?

I am using emgucv 2.2.1. Same problem occured with 2.1 version.

Thanks for any help


What I have found is quite surprising.

Image<Bgr, byte> currentFrame = capture.QueryFrame();
byte b;
try
{
   b = image.Data[0, 0, 0];            //Line (A)
}
catch (Exception ex)
{ MessageBox.Show("Before Convert : "+ex.Message); }         

image = image.Convert<Bgr, byte>();

try
{
   b = image.Data[0, 0, 0];           //Line (B)
}
catch (Exception ex)
{ MessageBox.Show("AfterConvert : "+ex.Message); }

In the above code: Line (A) throws exception "Object refence not set to an instance of an object". But after adding the code

image = image.Convert<Bgr, byte>();

Line(B) runs smoothly without any exception.

Does anyone know why this is hapenning?

Upvotes: 0

Views: 3722

Answers (3)

AzDev
AzDev

Reputation: 1

Heyy there,try to add .xml file in your .../bin/Debug. Then type in your ProcesFrame Method: //haar is HaarCascade haar = new HaarCascade("haarcascade_frontalface_default.xml");

Upvotes: -1

asif
asif

Reputation: 1015

[Though it is very late to answer, I am posting this for future reference.]
Recently I faced the same problem. I could not access pixel values of an image returned by QueryFrame through Data property. But after applying any operation (e.g. resize, convert) resulting image is accessible. The reason behind this and the solution is quite simple.

Data property of an image returned by QueryFrame is always null. Its image data is stored in unmanaged memory, therfore is not accessible thorough Data property. To access the pixel values of a frame, you just have to clone it.

Image<Bgr, byte> currentFrame = capture.QueryFrame();
Image<Bgr, byte> frame = currentFrame.Clone();
// now access using Data property
byte b = frame.Data[0,0,0];
byte g = frame.Data[0,0,1];
byte r = frame.Data[0,0,2];

Upvotes: 3

Robert Jeppesen
Robert Jeppesen

Reputation: 7877

capture.QueryFrame() returns null, which you try to access on the last line. I'd look inside that method. I'm guessing .Convert is an extension method, and that that method checks for null and returns something that is not null.

Upvotes: 0

Related Questions