Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

axCWIMAQVision.ExtractSingleColorPlane, performed twice on two separate images, throws error on second try

Solved!
Go to solution

I have the following code in C#:

 

private void ExtractColor(CWIMAQImage SourceImage, CWIMAQImage DestinationImage)
{
    CWIMAQImage colorPlane = new CWIMAQImage();
    axCWIMAQVision1.ExtractSingleColorPlane(SourceImage, colorPlane, CWIMAQColorPlanes.cwimaqLuminancePlane);
    axCWIMAQVision1.Copy(colorPlane, DestinationImage);

}

I need to run this method twice on two separate, identical images, each time overwriting the original image. It works on the first image fine, but then on the second image it throws an exception ("Invalid Image Type"). What's going on here? I tried switching the order of which image gets process but the first one ALWAYS works and the second fails.
0 Kudos
Message 1 of 10
(4,996 Views)
I am not very sure of c# but what is the image that you are using? RGB, greyscale? Are you using a proper img destination?
0 Kudos
Message 2 of 10
(4,988 Views)

It's the strangest thing. I know the source and destination images are OK because this method works perfectly the first time. But the second time, with literally NO code in between, applied to exact copies of the image used the first time, it fails.

 

The source image is RGB and I use the source also as the destination so it gets over-written. It looks like this:

 

CWIMAQImage Original = new CWIMAQImage();

 

CWIMAQImage Image1 = Original;

CWIMAQImage Image2 = Original;

 

ExtractColor(Image1,Image1);

ExtractColor(Image2,Image2); //Fails Here

0 Kudos
Message 3 of 10
(4,981 Views)
I'm sorry, I missed a line. After I instantiate the OriginalImage, I acquire something from a camera, then copy it.
0 Kudos
Message 4 of 10
(4,980 Views)
So I realized there is a very simple work-around for this problem, which is to filter one image and then copy to the second. BUT I'd still like to know why my original technique failed.
0 Kudos
Message 5 of 10
(4,972 Views)

I have seen this before.  You are creating the images in one format (grayscale, color) and during your process it is getting converted to a different format.  The second time through the loop, it is still the new format, which is not compatible with the first function.

 

An example where I had this problem is reading a color image from a camera, then converting it to a grayscale image for processing.  The second time through the loop, the grayscale image was not the proper format to read a color image.

 

The solution to this problem is usually to use a separate destination image in the new format during processing.  In my case, I created a grayscale image that was used as the destination image during the conversion.  This way, I still had a color image for acquisition and a grayscale image for processing.

 

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 6 of 10
(4,944 Views)
I don't think this is the problem. You see I'm performing the same function on two separate images. It doesn't seem to matter that the images are separate; the function always fails the second time around.
0 Kudos
Message 7 of 10
(4,942 Views)

I'm not sure how it works in C#, but in LabVIEW if you set Image1 = Original and Image2 = Original, they are still both pointing to the same image.  You aren't actually making a copy of the data, just the pointer.

 

Is there a way to check what format your images are when they enter the subroutine?  Can you check if they are color, grayscale, etc.?  I would be willing the bet the format of one changes between the calls.

 

Bruce

Bruce Ammons
Ammons Engineering
0 Kudos
Message 8 of 10
(4,939 Views)
Solution
Accepted by topic author dstanisl

I just looked at your code again, and now I am sure of it.  Image1 and Image2 are pointing to the same image as Original.  Since you are using them as the destination images, they are getting overwritten.  Your code is just like the following:

 

ExtractColor(Original, Original)  // Original has now been converted from color to grayscale

ExtractColor(Original, Original)  // Can't convert grayscale image, needs to be color - error!!

 

Bruce

Message Edited by Bruce Ammons on 02-08-2010 12:18 PM
Bruce Ammons
Ammons Engineering
0 Kudos
Message 9 of 10
(4,938 Views)

Aha! You are right.

 

So if I do the following it works:

 

            CWIMAQImage OriginalImage = new CWIMAQImage();
            AcquirePixelImage(OriginalImage);

            CWIMAQImage ImageForDefectFinding = new CWIMAQImage();
            ExtractColor(OriginalImage, ImageForDefectFinding);
            CWIMAQImage ImageForEdgeFinding = new CWIMAQImage();
            ExtractColor(OriginalImage, ImageForEdgeFinding);
            EdgeFilter(ImageForEdgeFinding, ImageForEdgeFinding);/**/

 

Or, alternately I could have instantiated the latter two image objects, then COPIED the original to them and processed them appropriately.

 

Thanks!

0 Kudos
Message 10 of 10
(4,935 Views)