BayerBG8 to RBG image and putting in PictureBox

colinodowd

Member
Joined
Jan 23, 2020
Messages
12
Programming Experience
1-3
Capture.PNG


I am having a lot of trouble converting a BagerBG8 byte array to RGB and then populating the RGB image into a pictureBox. I believe the code below solves the transformation to RGB but I cannot get that new byte array to fit inside the pictureBox.

C#:
int src_width = 3840;
int src_height = 2748;

int dst_width = src_width / 2;
int dst_height = src_height / 2;
int col = 0;
int a = 0;
byte[] destination = new byte[dst_width * dst_height * 3];
int pos = 0;

for (int row = 0; row <= dst_width; row += 2)
{
    for (col = 0; col < dst_height; col += 2)
    {
        a = row * src_width + col;
        //blue from this line
        destination[pos++] = buffer[a];

        //green average of two greens from this line and next line:
        destination[pos++] = (byte) ((buffer[a + 1] + buffer[a + dst_width]) / 2);

        //red from the next line
        destination[pos++] = buffer[a + dst_width + 1];
    }
}

Bitmap bitmap = bitmap = new Bitmap(1920, 1374, PixelFormat.Format24bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(test, 0, pNative, test.Length);
bitmap.UnlockBits(bmData);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

This results in only 1/3 of the pictureBox being filled. Does anybody know what I am doing wrong?
 
Last edited by a moderator:
Now, that's an interesting variation on cross-posting... Posting the same question on two different forums, but presenting two completely code bases. For those following along, it looks like our OP posted different code in StackOverflow.
 
Hi, and welcome to the forum.

It means you posted the same question on two different forums, with two completely different code bases. That's where he is going with that. Why did you delete your question from Stack Overflow?

So does that mean, you have resolved your issue?
 
Last edited:
@Sheepings Completely different is a stretch, I was using a slightly different approach to find the R,G,B pixels of the Bayer image but everything else was the same. Besides, the posts were asking different questions. I deleted my SO question because I solved the problem. I am now stuck on populating the RGB image into a pictureBox as seen in the image above.
 
Line 30 is copying some array test[/code] as bitmap data, but your loops on lines 8-25 is populating an array named [icode]destination. Since you don't show us how the test array is created and populated, we don't really know why only a third of the bitmap is filled.
 
@Skydiver I am getting my test array from the Basler SDK.


C#:
IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);
using (grabResult)

{
    // Image grabbed successfully?
    if (grabResult.GrabSucceeded)

    {
        test = grabResult.PixelData as byte[];
        ImageWindow.DisplayImage(0, grabResult);
        image_filepath += file_extension;
        ImagePersistence.Save(ImageFileFormat.Png, image_filepath, grabResult);
    }
    else
    {
        Console.WriteLine("Error: {0} {1}", grabResult.ErrorCode, grabResult.ErrorDescription);
    }
}



int src_width = 3840;
int src_height = 2748;

int dst_width = src_width / 2;
int dst_height = src_height / 2;

int col = 0;
int a = 0;
byte[] destination = new byte[dst_width * dst_height * 3];
int pos = 0;

for (int row = 0; row <= dst_width; row += 2)
{
    for (col = 0; col < dst_height; col += 2)
    {
        a = row * src_width + col;
        //blue from this line
        destination[pos++] = test[a];
        //green average of two greens from this line and next line:
        destination[pos++] = (byte) ((test[a + 1] + test[a + dst_width]) / 2);
        //red from the next line
        destination[pos++] = test[a + dst_width + 1];
    }
}



Bitmap bitmap = bitmap = new Bitmap(1920, 1374, PixelFormat.Format24bppRgb);
BitmapData bmData = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative = bmData.Scan0;
Marshal.Copy(destination, 0, pNative, destination.Length);
bitmap.UnlockBits(bmData);
pictureBox1.Image = bitmap;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
 
Last edited:
I've never used the Basket SDK. I find it really strange that you can get a reference to an array in the IGrabResult (line 9), and then dispose that IGrabResult (end of using scope at line 18), but still be able to use the array in your nested for loops (lines 39-43). So much for the SDK following the .NET Framework's design guidelines regarding "least surprise".

Anyway, with this new code, are you getting a different result, or just the same? What happens if you comment out line 2?
 

Latest posts

Back
Top Bottom