ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Machine Vision

cancel
Showing results for 
Search instead for 
Did you mean: 

VisionImage Load Image

Dear comunity,

 

I am working on vision application, using c# and vision library 2010. I need to fill an object VisionImage with an image from Bitmap class, from memory.

I've already Bitmap object but I've difficult to understand how copy byte[] array bitmap into VisionImage object.

I used this approch:

 

 

Bitmap Image2Work;

.

.

.

.

 

PixelValue2D pv = new PixelValue2D(BmpToBytes(Image2Work)); // BmpToBytes return byte[]

viImg.ArrayToImage(pv);

 

 

I can't compile this. PixelValue2D doesn't like byte[] returned from BmpToBytes 

 

any idea ?

thanks

 

 

 

 

 

 

 

 

 

0 Kudos
Message 1 of 3
(3,595 Views)

Hi,

 

I found this on the web:

 

Initialization:

static Int16[,] intArray=new Int16[320,256];
PixelValue2D pxl=new PixelValue2D(intArray);

in the worker method:

_session.Grab(imageViewer.Image,true); //So I can see the image
pxl=imageViewr.Image.ImageToArray();
intArray=pxl.I16;

Bitmap bmp=new Bitmap(320,256,PixelFormat.Format32bppRgb);
for(int y=0;y
{
for(int x=0;x
{
int val=intArray;
int v =
0xff<<24 | (val<<16) | (val<<8) |val; // This is new  otherwise the image is blue but is still distorted
bmp.SetPixel(x,y,Color.FromArgb(v);
}
}
pictureBox1.Image=bmp;

 

I hope that this portion of code could help you.

 

Have a good evening!

 

Claudio

 

 

0 Kudos
Message 2 of 3
(3,584 Views)

Where can I find the documentation about PixelValue2D format byte array ?

 

I need to set ImageVsion oject from Bitmap ojbect.

I tried using follow code, but I've two issues:

    1. I can set the ImageVision Object, but the image is rotate 90°
    2. the loop into BmpToBytes is too slow.

 

PixelValue2D pv = new PixelValue2D(BmpToBytes(Image2Work));                       

viImg.SetSize(Image2Work.Width, Image2Work.Height);                       

viImg.ArrayToImage(pv);

 

private unsafe byte[,] BmpToBytes(Bitmap bmp)       

{      

BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size), ImageLockMode.ReadOnly,  bmp.PixelFormat);           

unsafe

        {       // number of bytes in the bitmap               

int byteCount = bData.Stride * bmp.Height;
                byte[] buffTmp = new byte[bData.Stride*bmp.Height];

                byte[,] bmpBytes = new byte[bData.Stride, bmp.Height];

                Marshal.Copy(bData.Scan0, buffTmp, 0, bData.Stride * bmp.Height);

                

                // Copy the locked bytes from memory               

int iIdx=0;               

for (int ii = bmp.Width - 1; ii > 0; ii--)                               

{                   

for (int i = bmp.Height - 1; i > 0; i--)                   

{                       

bmpBytes[bmp.Width - ii, i] = buffTmp[i * bmp.Width + ii];
                   }                                                   

}                
                // don't forget to unlock the bitmap!!               

bmp.UnlockBits(bData);               

return bmpBytes;           

}                   

}

 

 

 

 

0 Kudos
Message 3 of 3
(3,581 Views)