|
Bug in API getImage? Missing top 4 pixel rows from United States [214 posts] |
16 year
|
Hi STeven,
I'm having some trouble with the API getImage method in my C# program. I have the latest RoboRealm running on the same machine as my C# program and I'm using a Logitech Fusion webcam. I'm using the native API not the DLL. If I fire up my C# program, connect to RR, then do a getImage on a grayscale image, the returned image is missing the top 4 rows of pixels (they are all black/0). If I repeat getImage a few times in a row, then I eventually get the full image. I'm not sure if the problem is with getImage or with the routine I am using below to convert the byte array to a Bitmap. Would you please take a look?
The code block I am using to get the grayscale image from RR is as follows:
int width = 1280;
int height = 960;
string marker = "Grayscale";
byte[] imageData = new byte[width * height];
Dimension d = null;
while (d == null)
{
d = RR.getImage(marker, imageData, width * height);
}
I then convert the byte array imageData into a bitmap using the following:
public Bitmap BitsToBitmapRGB24(Byte[] bytes, int width, int height)
{
//swap RGB to BGR
Byte tmp;
for (int x = 3; x < bytes.GetLength(0); x += 3)
{
tmp = bytes[(x + 2)];
bytes[x + 2] = bytes[x];
bytes[x] = tmp;
}
if (bytes.GetLength(0) < width * height * 3)
{
return null;
}
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
int i;
BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
if (data.Stride == width * 3)
{
Marshal.Copy(bytes, 0, data.Scan0, width * height * 3);
}
else
{
for (i = 0; i < bmp.Height; i++)
{
IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i);
Marshal.Copy(bytes, i * bmp.Width * 3, p, bmp.Width * 3);
}
}
bmp.UnlockBits(data);
return bmp;
}
Thanks!
patrick
|
|
|
from United States [214 posts] |
16 year
|
I just found a possible clue-- if I replace the getImage block above with:
while (d == null)
{
d = RR.getImage(imageData, width * height);
Thread.Sleep(1);
d = RR.getImage(marker, imageData, width * height);
Thread.Sleep(1);
}
then the problem seems mostly to go away. In other words, I get the image without the marker argument first, then get it again with the marker.
--patrick
|
|
|
Anonymous |
16 year
|
Patrick,
Are you starting up RR by your application or is this to an already running RR? Seems that the black lines are from the image prior to the camera being accessed.
You might also try to wait for an image prior to grabbing it. Using
RR.waitImage();
which should wait until an image becomes available.
STeven.
|
|
|
from United States [214 posts] |
16 year
|
Hi STeven,
Thanks for your reply. It looks like the problem is just in the display of the image in my C# picture box--I saved one of the images with the black lines, then looked at it outside my program and the black lines were gone. So RoboRealm is returning the bytes fine--the bug seems to be in C#.
But to answer your question, I am connecting to an already running RR instance. Using RR.waitImage() solves the problem only if I double up on the getImage statements like this:
while (d == null)
{
d = RR.getImage(marker, imageData, width * height);
RR.waitImage();
d = RR.getImage(marker, imageData, width * height);
RR.waitImage();
}
But as I say, RR is apparently getting the full image even on the first attempt and I can live with the image display glitch.
--patrick
|
|
|
Anonymous |
16 year
|
Cool. Thanks for the follow up!
STeven.
|
|