LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to decode a set of datas received from serial port and display image on a canvas based on CVI ?

Hi !

I have received a set of datas via rs232 and it contains picture messages. I have to decode the datas first and then display them on a canvas.

I have known several functions that may be used . Such as

int SetBitmapData (int bitmapID, int bytesPerRow, int pixelDepth, int colorTable[], unsigned char bits[], unsigned char mask[]);
int CanvasDrawBitmap (int panelHandle, int controlID, int bitmapID, Rect sourceRectangle, Rect destinationRectangle);

 However,I don't know how to set the following parameters according to the actual pixel values and color values.

int bytesPerRow, int pixelDepth, int colorTable[], unsigned char bits[], unsigned char mask[]

 What's more,I have no idea how to decode the datas.

The  attachment is a incomplete project. I will be very appreciated if anyone could help me .

Best regards.

 

xiepei

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 1 of 14
(6,398 Views)

The first thing of all is to know what you are receiving through the serial port. Is it the dump of an image file? Which file format is it? JPEG? Bitmap? ...? In case it is the raw image content, does it contain an informational header part? These are preliminary informations that gives you the correct path to follow to rebuild the image.

 

Supposing it is the dump of a file you could for example try to simply save the received data to disk with the approrpiate file extension and reload it with GetBitmapFromFile.

 

In case it is the raw content of a bitmap file, you can try to rebuild the bitmap but again you may need to know some informations from the source to perform the appropriate adaptations from it to the final bitmap in memory (NewBitmap always produces a bitmap that matches the color depth of your actual display, which may be different from the color depth with which the image was acquired so some operations on bit informations may be necessary). Some informations on bitmap file formats and how to treat them can be found in my example here and in the documentation that is linked in that document.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 14
(6,391 Views)

Hi !

Thank you for your reply.

My colleague use camera capture some pictures which are the format of Bitmap and the bitmap size is 57600. He told me the picture datas encoding automatically.He sends the datas to me through the serial port and I receive the raw content of  a bitmap file. I have seen your examplehttp://https://decibel.ni.com/content/docs/DOC-9786

The image file is on the computer and coul be loaded directly. I only have the datas and could I use the following function ?

GetCtrlVal (panel, PANEL_BMP, file);

	//-------------------------------------------------------------------------
	// Get informations on the bitmap
	//-------------------------------------------------------------------------
	GetBitmapFromFile (file, &bitmap);
	GetBitmapData (bitmap, &bpr, &bpp, &width, &height, NULL, NULL, NULL);
	GetBitmapInfo (bitmap, &cSize, &bSize, NULL);

	//-------------------------------------------------------------------------
	// Display informations on bitmap
	//-------------------------------------------------------------------------
	SetCtrlVal (tmpH, BMPINFO_bpr, bpr);
	SetCtrlVal (tmpH, BMPINFO_bpp, bpp);
	SetCtrlVal (tmpH, BMPINFO_width, width);
	SetCtrlVal (tmpH, BMPINFO_height, height);
	SetCtrlVal (tmpH, BMPINFO_cSize, cSize);
	SetCtrlVal (tmpH, BMPINFO_bSize, bSize);

 Best regards .

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 3 of 14
(6,385 Views)

As far as I can see there may be two scenarios.

 

1. Your colleague gets the image, saves them to disk and then sends the file over rs232; doing so, he is automatically sending all header informations that are implicit in a .bmp file. In this case you can simply save the rs232 content to disk are read it back with GetBitmapFromFile. You could also work directly in memory but is a bit more complicated (there is a section in my example that works on bitmaps in memory that you can take as a starting point)

 

2. Your colleague sends only "image" bits over the serial channel. In this case you must manually rebuild all bitmap informations and structures so that your system is able to interpret those data. In this case informations on camera settings can be useful.

 

Which scenario are you in?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 14
(6,383 Views)

Hi !

He sends each pixel datas over the serial  channel and I think I am in the second scenario.He tells me that the model  of LCD screen output is QVGA and acquisition 320*240 pixels data .The format of image output is Bayer Raw. All I have to do is take the image reductive into BMP format.

 

His camera is connected to a LCD screen .Both camera and LCD screen will be placed at work site where we could not enter into.So the acquisition image would be sended to the computer via rs232 communication.So the field situation could be watched through the CVI monitoring.

 

In this case I have no idea about rebuilding all bitmap informations and structions. Could you help me interpret those data ?

Thank you very much !

 

Best regards.

 

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 5 of 14
(6,304 Views)

Hi,

I'm not familiar with camera acquisition. I searched some documentation on Bayer Raw Data finding this Wikipedia page which introduces some principles on camera image acquisition, raw image formats and filters used to decode image data: you may find it useful for your needs. Some of the linked resources treat Bayer filter in detail, but I couldn't locate a clear algorithm for you to integrate directly in your code.

Nevertheless, principles for one algorithm can be found in this Knowledgbase page. I seem to understand the IMAQ Vision module for CVI integrates functions for  demosaicing raw image data, but is it a separate toolkit to buy. I have also found this Bayer Decoder IP in the Community, but it's a LabVIEW module: if you have access to LV you can take a look at it and use it as a guide for generating your own algorithm, or better build it into a DLL to integrate in your application.

 

But first of all, are you sure the camera cannot perform demosaicing for you?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 14
(6,295 Views)

Hi !

Thank you for your kind help.

I'm sorry to tell you that I don't know how to decode image data.

I'm really sorry.I didn't explain clearly. I don't need to do camera acquisition.My colleague has already done camera acquisition.All what have to do is the image reduction.

He sends me a set of datas containing image information.I think it is your second scenario I am in.

 

May I use  SetBitmapData to build a bitmapID which CVI could recognize  according to those datas I acquire via rs232, and then use CanvasDrawBitmap  with the bitmapID to draw the image on a canvas ?

 

But I have no idea how to set the following parameters according to actual pixel value and color value.

int bytesPerRow, int pixelDepth, int colorTable[], unsigned char bits[], unsigned char mask[]

 

int SetBitmapData (int bitmapID, int bytesPerRow, int pixelDepth, int colorTable[], unsigned char bits[], unsigned char mask[]);

int CanvasDrawBitmap (int panelHandle, int controlID, int bitmapID, Rect sourceRectangle, Rect destinationRectangle);

 I'm sorry to trouble you again.

 

Best regards.

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 7 of 14
(6,285 Views)

Hi !

I'm sorry to trouble you again.

I'm seriously thinking about my problem once again.

My colleague use the following codes with stm32 to send image data.

#define hang_max 240		//Define image acquisition and number of lines
#define lie_max  240		//Define image acquisition column number
uint8_t Image[hang_max][lie_max];   / / image data resolution

//send data on serial channel
void USART1_Send(void)
{
    uint8_t i,j;
	RS485_Send;
	for(i=0;i<hang_max;i++)
	{
	    for(j=0;j<(lie_max);j++) 
		{
		    while (!(USART1->SR & USART_FLAG_TXE));
			USART1->DR = Image[i][j];   //get pixel Y values		}
	}
	while(!USART_GetFlagStatus(USART1,USART_FLAG_TC));  
}

 I receive these datas  by  reading bytes on a serial COM  port using CVI.

unsigned char image[60000];
int comport; 

inqlen = GetInQLen (comport);   											 		
for(i=0;i<inqlen;i++)
{
  image[i]=ComRdByte(comport);	 
}

 Next is how to display the image problem. I don't know how to connect the array "image[i]"  with  SetBitmapData \ GetBitmapData\GetBitmapInfo  or other functions.

 

Thank you very much !

Best regards.

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 8 of 14
(6,269 Views)

Well, things are getting a bit clearer now.

 

1. Your image is actually 240x240 pixel (not 320x240 as you told before). The size of image data is 57600 bytes which corresponds to that frmat (I was puzzled by such a data size compared with larger image dimensions!)

2. The image is a 8-bits-per-pixel one; this means that you must provide a color palette to create the bitmap

3. You cannot simply copy image data into bitmap fields in CVI: CreateBitmap always produce an image with pixeldepth matched with display colour depth (normally 24 or 32 bpp)

 

All that means that you must:

1. Create the appropriate color palette with 256 colors: it may be a grayscale or color palette depending on camera characteristics. Grayscale palette can be found in my sample project, while sample colour palettes can be found here (here the description of a "standard" 8-bpp color palette)

2. Create the bits array correctly dimensioned for the color depth of your monitor

3. Get each pixel value from the camera, lookup in the color palette for the appropriate colour and create the RGB informations into the bits array in memory

4. Display the bitmap on the canvas

 

As a first step, I would configure my system for 256-color display setting, create a bitmap and simply copy image data into it: you should be able to get the correct image back to your screen.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 14
(6,265 Views)

Hi !

Thank you very much for your reply. I agree with your appointment.

However,I could not find the function CreateBitmap though I have added the header file "windows.h" .My CVI version is 8.0.I only find this function http://http://msdn.microsoft.com/en-us/library/dd183485(v=VS.85).aspx

Thus I have some difficulties in copying image data into bitmap fields and creating the bits array correctly dimensioned for the color depth of my monitor.

Would you please give me some advice on this function CreateBitmap that I could produce an image with pixeldepth matched with display color depth ?

 

Because it's the first time that I get to the image processing and I am not familiar with some image processing technical terms(e.g. color palette).

I'm not sure the function panel found in your sample project is color palette.CVI bitmap details

 

Thank you very much indeed.

Best regards.

I wouldn't care success or failure,for I will only struggle ahead as long as I have been destined to the distance.
0 Kudos
Message 10 of 14
(6,259 Views)