473,503 Members | 10,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Image has wrong colors

I have a Windows form program written in C# 2005.
I continually read a stream of data via TCP that comes from a video camera
(NTSC).
When I have all the data for one frame I update a picturebox (pbVideo) like
this:

pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes), true, true);
or this
pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

Everything works as expected except the colors are wrong. For example, the
things that should look yellow are turquoise and the things that should be
blue appear as red or orange.

The data stream is in mjpeg format - I just pick out the frames one at a time.
Any ideas or suggestions would be appreciated...

Thank you,
Joe

Jan 18 '08 #1
14 3865
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:0B**********************************@microsof t.com...
>I have a Windows form program written in C# 2005.
I continually read a stream of data via TCP that comes from a video camera
(NTSC).
When I have all the data for one frame I update a picturebox (pbVideo)
like
this:

pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes), true, true);
or this
pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

Everything works as expected except the colors are wrong. For example,
the
things that should look yellow are turquoise and the things that should be
blue appear as red or orange.

The data stream is in mjpeg format - I just pick out the frames one at a
time.
Any ideas or suggestions would be appreciated...
What is mjpeg format? Possibly you should be using Bitmap.LockBits and
copying the data in as it is received. That way you keep only 1 copy of the
image data and can display it as it comes in.

Michael
Jan 18 '08 #2
Hi Michael,

Thank you for the reply. I'm not sure of the format - I beleive it's 24 bit
color. Each frame is just a jpg file. I am trying to rewrite an application
that was written on a Linux box and it displays the colors correctly (I don't
have that code).

As far as my app goes, I gather the bytes 1450 at a time until I get a full
frame. Then I build the whole bitmap at once. I'm not familiar with
LockBits - what does it do? I was hoping it was just a color map problem or
something like that.

Thank you,
Joe
Jan 18 '08 #3
On Thu, 17 Jan 2008 21:44:00 -0800, Joe Thompson
<Jo*********@discussions.microsoft.comwrote:
Thank you for the reply. I'm not sure of the format - I beleive it's 24
bit
color. Each frame is just a jpg file. I am trying to rewrite an
application
that was written on a Linux box and it displays the colors correctly (I
don't
have that code).
Do you know whether the Linux code is using a plain JPEG library, or has
something custom-written for the purpose?

I would think that if the former, the code you're using should work. But
if the latter, all bets are off.

I don't know enough about the M-JPEG format to comment with specifics.
However, I do know that many video formats are _not_ RGB, and it's
possible that the same is true for the data you're receiving.

To be honest, your question doesn't sound like the sort of thing that's
likely to get a good answer in this newsgroup. You should monitor the
thread just in case, but I would look to a forum more specific to video
streaming, perhaps even specific to the device you're using (there is as
far as I know no well-defined specification for M-JPEG, so you may need
device-specific advice).

Pete
Jan 18 '08 #4
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:F0**********************************@microsof t.com...
Hi Michael,

Thank you for the reply. I'm not sure of the format - I beleive it's 24
bit
color. Each frame is just a jpg file. I am trying to rewrite an
application
that was written on a Linux box and it displays the colors correctly (I
don't
have that code).
Can you save the data to a file and open it using an image program? Does it
look the same as what you get in C#?
As far as my app goes, I gather the bytes 1450 at a time until I get a
full
frame. Then I build the whole bitmap at once. I'm not familiar with
LockBits - what does it do? I was hoping it was just a color map problem
or
something like that.
LockBits gives you high speed access to the raw data in a bitmap object.
It's possible if it's coming across from a linux box that it's a little/big
endian issue and the RGB values are just back to front. If the image is
correct except that the colours are wrong then this is what you could do:
1) Create the bitmap as you are doing currently.
2) Use LockBits to 'fix' all the data in the bitmap.

Lockbits is slightly more challenging than most C# card but it's nothing too
difficult.

Try attaching the bitmap to a post, I know that's frowned apon here but we
can cope with 1450 bytes.

Michael
Jan 18 '08 #5
Hi Michael,

Yes, I can and do log it to a file also. When I display it in Paint or just
use "Preview" from windows it still looks messed up. The file is actually
larger than 1450 bytes, that's just how many I read at a time. The picture
is at work and I'm off until Monday.

What I meant to say is we already have a Linux laptop with an application
that receives the same data but displays is correctly. The app I'm writing
in windows doesn't. Actually, when the camera is set to black and white, the
image looks good. Just the color mode produces a bad picture. Would this be
the case if it was a big/little endian issue? Is there a way I could apply
a PixelFormat to the jpg to correct it?

Thanks for all your help,
Joe
Jan 18 '08 #6
Hi Peter,

I'm really not sure the format it is using - I may be able to find out
Monday. Hopefully this thread isn't buried too deep by then. I was
originally going to post this in the Graphics and Multimedia group but it
seems more of a C# issue - maybe I will though.

Thanks for the help,
Joe
Jan 18 '08 #7
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:1D**********************************@microsof t.com...
Hi Michael,

Yes, I can and do log it to a file also. When I display it in Paint or
just
use "Preview" from windows it still looks messed up. The file is actually
larger than 1450 bytes, that's just how many I read at a time. The
picture
is at work and I'm off until Monday.

What I meant to say is we already have a Linux laptop with an application
that receives the same data but displays is correctly. The app I'm
writing
in windows doesn't.
That is interesting.
Actually, when the camera is set to black and white, the
image looks good. Just the color mode produces a bad picture. Would this
be
the case if it was a big/little endian issue?
Yes (although big endian might not be technically the correct term). It's
likely windows is expecting RGB when the webcam is delivering BGR.
Is there a way I could apply
a PixelFormat to the jpg to correct it?
You could fix it with a ColorMatrix which is fairly easy to do. You'd define
a matrix like this I think:

0 0 1 0 0
0 1 0 0 0
1 0 0 0 0
0 0 0 1 0
0 0 0 0 1

Try this air code:

ColorMatrix cm = new ColorMatrix( above values in an array);
Graphics g = Graphics.FromImage(myBitmap);
ImageAttributes ia = new imageAttributes();
ia.SetColorMatrix(cm);
g.DrawImage(myBitmap,,,,,ia); //<- fill in the rest of the params
g.dispose();

You can achieve the same thing using LockBits probably faster but with more
code and it needs to be unsafe code. I would probably use LockBits myself
although the above will work quite well also. I'm presuming you can draw the
bitmap onto itself, if not you might need to create a second bitmap. I've
not tested the above code but I think it should work. If you want a lockbits
example let me know. I won't be back till monday either. Have a good
weekend.

Regards,
Michael
Jan 18 '08 #8
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:B6**********************************@microsof t.com...
Hi Peter,

I'm really not sure the format it is using - I may be able to find out
Monday. Hopefully this thread isn't buried too deep by then. I was
originally going to post this in the Graphics and Multimedia group but it
seems more of a C# issue - maybe I will though.
This would be your best bet I would think as these sort of issues are dealt
with all the time there:
microsoft.public.dotnet.framework.drawing

Michael
Jan 18 '08 #9
Hi Michael,

I'll definitely give that a try by Monday and let you know.

Thanks again,
Joe
Jan 18 '08 #10
Michael,

Great news - I just had a guy at work email me a bad jpg , applyied the
colormatrix and it worked! Monday I'll try it "real time" to see if I can
keep up with the fastest video rate (I think it's 20 x second). Any
suggestions on this are welcome.

Thanks again for all your help,
Joe
Jan 18 '08 #11
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:E7**********************************@microsof t.com...
Michael,

Great news - I just had a guy at work email me a bad jpg , applyied the
colormatrix and it worked! Monday I'll try it "real time" to see if I can
keep up with the fastest video rate (I think it's 20 x second). Any
suggestions on this are welcome.
Yep, try this code also, I'd be suprised if it couldn't keep up with 20
frames per sec. You'll need to mark it as unsafe and allow unsafe code in
your assembly. Out of interest, if you compare the 2 methods can you post
the max framerate of each?

Bitmap bitmap = GetBitmapFromTCP(....);
int width = bitmap.Width;
int height = bitmap.Height;
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int offset = data.Stride - data.Width * 3;
byte* ptr = (byte*)data.Scan0;
for(int y = 0; y < height; y++, ptr += offset)
{
for(int x = 0; x < width; x++, ptr += 3)
{
byte swap = ptr[0];
ptr[0] = ptr[2];
ptr[2] = swap;
}
}
bitmap.LockBits(data);

Michael
Jan 19 '08 #12
Hi Michael,

I was able to implement and run my program using the LockBits approach. It
seems to keep up pretty well at 15 frames/sec but a little worse at 30. If I
drop the resolution from 320 x 240 to 160 x 120 it gets better.

I would still like to try it using the ColorMatrix approach. The little
test app I ran at home was much simpler - I can't seem to figure out what to
do in my real app.

I have a picturebox named pbVideo, and a byte[] named m_ImageBuff. All I
had to do was one line of code everytime I had a complete frame:

pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

Now I'm confused in the ColorMatrix approach - after I do g.DrawImage how do
I get that image to my picturebox? pbVideo.Image = ???

Thanks again,
Joe

All
Jan 21 '08 #13
"Joe Thompson" <Jo*********@discussions.microsoft.comwrote in message
news:FD**********************************@microsof t.com...
Hi Michael,

I was able to implement and run my program using the LockBits approach.
It
seems to keep up pretty well at 15 frames/sec but a little worse at 30.
If I
drop the resolution from 320 x 240 to 160 x 120 it gets better.

I would still like to try it using the ColorMatrix approach. The little
test app I ran at home was much simpler - I can't seem to figure out what
to
do in my real app.

I have a picturebox named pbVideo, and a byte[] named m_ImageBuff. All I
had to do was one line of code everytime I had a complete frame:

pbVideo.Image = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

Now I'm confused in the ColorMatrix approach - after I do g.DrawImage how
do
I get that image to my picturebox? pbVideo.Image = ???
Just something like this:
Bitmap myBitmap = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));
......
g.DrawImage(myBitmap....);
pbVideo.Image = myBitmap;
But I would abandon the picturebox myself and just do this:
Graphics g = this.CreateGraphics();//this being a form
//insert code here to create color matrix
g.drawImage(myBitmap, .... )
g.Dispose();

That way you're cutting down on CPU usage a lot because you're cutting out 1
step by drawing straight to the form. I spent 4 years writing an imaging app
and only used 1 picturebox which was to display an image in the About
screen.

>
Thanks again,
Joe

All

Jan 21 '08 #14
Hi Michael,

I have both methods working now:
// ColorMatrix approach - works!
//
m_bmpPicture = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

if (m_bmpPicture.PixelFormat ==
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
m_gfxPicture.DrawImage(m_bmpPicture, m_rctPicture, 0, 0,
m_bmpPicture.Width, m_bmpPicture.Height, GraphicsUnit.Pixel,
m_iaPicture);
}
else
{
pbVideo.Image = m_bmpPicture;
}

Or...
// LockBits approach - works!
//
m_bmpPicture = (Bitmap)Bitmap.FromStream(new MemoryStream(m_ImageBuff, 0,
m_TotalImageBytes));

if (m_bmpPicture.PixelFormat ==
System.Drawing.Imaging.PixelFormat.Format24bppRgb)
{
ConvertToRGB(ref m_bmpPicture);
}
pbVideo.Image = m_bmpPicture;

public static bool ConvertToRGB(ref Bitmap b)
{
int width = b.Width;
int height = b.Height;
BitmapData data = b.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int offset = data.Stride - data.Width * 3;

unsafe
{
byte* ptr = (byte*)data.Scan0;
for (int y = 0; y < height; y++, ptr += offset)
{
for (int x = 0; x < width; x++, ptr += 3)
{
byte swap = ptr[0];
ptr[0] = ptr[2];
ptr[2] = swap;
}
}
}
b.UnlockBits(data);
return true;

}
Both seem to run about the same speed but I haven't actually measured them
in any way. My biggest problem now is I need to build the inital bitmap
faster. I'm collecting the video data on one port and other telemetry data
on another port.

Thanks again for all your help,
Joe
Jan 23 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2569
by: | | last post by:
hi, is it a php bug or i do smth wrong? From site http://www.allegro.pl/show_user_auctions.php?uid=45432 I`m taking images (those thumbs) and its name at right.
7
2884
by: Laszlo Zsolt Nagy | last post by:
Hello, How can I determine the number of colors used in an image? I tried to search on Google but I could figure out. I read the PIL handbook but I do not see how to do it. Can anyone help? ...
4
6250
by: Oliver Albrecht | last post by:
Has somebody a example-script how i can put a other palette to a image? (after this the image should have similar outlook)
0
1717
by: bearophileHUGS | last post by:
Hello, this time I have a question about PIL usage, maybe if Lundh has some time he can answer me. I am experimenting different color quantization algorithms, so having computed the palette with a...
4
3929
by: Jmc | last post by:
Hi Need some advice on how to get all colors in an image. I wish to have an input file, in my case it will be a scanned piece of fabric. I would like to first of all gen an array with all the...
1
1579
by: jitu78 | last post by:
GIF Images Use GIF files for images that have a small number of colors. GIF files are always reduced to no more than 256 unique colors. The compression algorithm for GIF files is less complex than...
1
1933
by: oruccim | last post by:
hi I want understanding pictures colorfull for examle colorfull or black-white image.google.com there are understand it .Can I understand it thanks....
7
17035
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
10
7040
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7015
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4693
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1523
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.