After obtaining the JPEG image from the arm camera, extract the JPEG image from the memory and use opencv for processing.

Source: Internet
Author: User

Target Board: gm8126
Opencv1.0
Since the image obtained by the 8126 Board is in JPG format and the JPG format is encoded in the memory, the jpg image data in the memory must be saved to the local disk during processing.
Because cvloadimage () in opencv1.0 cannot load JPG images, the jpg image data in the memory needs to be decoded. To enable
8126 the obtained image is processed by opencv and the processing speed is accelerated. This section describes how to decode the JPG data in the memory and convert it to the iplimage format.
First download the SRC file for the jpeg-6b: javassrc.v6b.tar.gz
Decompress the package and modify the functions:
1. libjpeg. h
1) Add "# define jpeg_stdio_src jstdsrc"
"# Define pai_stdio_mem_src jmemsrc"
2) Add "extern (void) into _stdio_src JPP (j_decompress_ptr Cinfo, file * infile:
"Extern (void) 1__stdio_mem_src JPP (j_decompress_ptr Cinfo, char * indata, int nsize ));"
2. jdatasrc. c
1) Add the variable in the "my_source_mgr" struct definition:
Char * indata;/* Memory Source stream */INT ninoffset;
Int nsize;
2) Add a function:
Methoddef (Boolean)
Fill_input_mem_buffer (j_decompress_ptr Cinfo)
{
My_src_ptr src = (my_src_ptr) Cinfo-> SRC;
Size_t nbytes;

// Nbytes = jfread (SRC-> infile, Src-> buffer, input_buf_size );
Nbytes = Src-> nsize-Src-> ninoffset;

If (nbytes> input_buf_size) nbytes = input_buf_size;

If (nbytes <= 0 ){
If (SRC-> start_of_file)/* treat empty input file as fatal error */
Errexit (Cinfo, jerr_input_empty );
Warnms (Cinfo, jwrn_1__eof );
/* Insert a fake EOI marker */
Src-> buffer [0] = (joctet) 0xff;
Src-> buffer [1] = (joctet) jpeg_eoi;
Nbytes = 2;
}

Memcpy (SRC-> buffer, Src-> indata + Src-> ninoffset, nbytes );
Src-> ninoffset + = nbytes;

Src-> pub. next_input_byte = Src-> buffer;
Src-> pub. bytes_in_buffer = nbytes;
Src-> start_of_file = false;

Return true;
}

3) Add a function:
Methoddef (void)
Skip_input_mem_data (j_decompress_ptr Cinfo, long num_bytes)
{
My_src_ptr src = (my_src_ptr) Cinfo-> SRC;

/* Just a dumb implementation for now. cocould use fseek () cannot
* It doesn' t work on pipes. Not clear that being smart is worth
* Any trouble anyway --- large skips are infrequent.
*/
If (num_bytes> 0 ){
While (num_bytes> (long) Src-> pub. bytes_in_buffer ){
Num_bytes-= (long) Src-> pub. bytes_in_buffer;
(Void) fill_input_mem_buffer (Cinfo );
/* Note we assume that fill_input_buffer will never return false,
* So suspension need not be handled.
*/
}
Src-> pub. next_input_byte + = (size_t) num_bytes;
Src-> pub. bytes_in_buffer-= (size_t) num_bytes;
}
}

4) Add the implementation function of "1__stdio_mem_src:
/***************** Add by lixintao, 2012.4.10 ***************************/
/************ Memory src object manager functiont ********************* */
Global (void)
Performance_stdio_mem_src (j_decompress_ptr Cinfo, char * indata, int nsize)
{
My_src_ptr SRC;

/* The source object and input buffer are made permanent so that a series
* Of JPEG images can be read from the memory by calling mem_1__stdio_src
* Only before the first one. (If we discarded the buffer at the end
* One image, we 'd likely lose the start of the next one .)
* This makes it unsafe to use this manager and a different source
* Manager serially with the same JPEG object. Caveat programmer.
*/
If (Cinfo-> src = NULL) {/* First time for this jpeg object? */
Cinfo-> src = (struct cmd_source_mgr *)
(* Cinfo-> mem-> alloc_small) (j_common_ptr) Cinfo, jpool_permanent,
Sizeof (my_source_mgr ));
Src = (my_src_ptr) Cinfo-> SRC;
Src-> buffer = (joctet *)
(* Cinfo-> mem-> alloc_small) (j_common_ptr) Cinfo, jpool_permanent,
Input_buf_size * sizeof (joctet ));
}

Src = (my_src_ptr) Cinfo-> SRC;
Src-> pub. init_source = init_source;
Src-> pub. fill_input_buffer = fill_input_mem_buffer;
Src-> pub. skip_input_data = skip_input_mem_data;
Src-> pub. resync_to_restart = pai_resync_to_restart;/* use default method */
Src-> pub. term_source = term_source;
Src-> indata = indata;
Src-> nsize = nsize;
Src-> ninoffset = 0;
Src-> pub. bytes_in_buffer = 0;/* forces fill_input_buffer on first read */
Src-> pub. next_input_byte = NULL;/* Until buffer loaded */

}
After modifying the source code, recompile the libjpeg library.

The following code converts jpg image data in the memory to iplimage:
/* Author: lixintao data: 2012.4.11
* Function name: ipv2ipl
* Arguments:
* Invalid data: JPEG image data in the memory.
* Maximum size: JPEG image data size in the memory.
* Return:
* Iplimage *: the pointer to the iplimage struct
* Function:
* This function convert the jpg image data in the memory
* Iplimage struct for opencv processing.
**/
Iplimage * limit 2ipl (char * handle data, int limit size)
{
Iplimage * _ pimg = 0;

Struct performance_error_mgr Jerr;
Struct performance_decompress_struct Cinfo;

Cinfo. Err = maid (& Jerr );
Pai_create_decompress (& Cinfo );

1__stdio_mem_src (& Cinfo, 1_data, 1_size );

Pai_read_header (& Cinfo, true );

Pai_start_decompress (& Cinfo );

Int nrowsize = Cinfo. output_width * Cinfo. output_components;
Int W = Cinfo. output_width;
Int H = Cinfo. output_height;

Char * BMP buffer = (char *) malloc (H * w * 3 );
Jsamparray pbuffer = (* Cinfo. mem-> alloc_sarray) (j_common_ptr) & Cinfo, jpool_image, nrowsize, 1 );

While (Cinfo. output_scanline <Cinfo. output_height)
{
Pai_read_scanlines (& Cinfo, pbuffer, 1 );

Int start = nrowsize * (Cinfo. output_scanline-1 );
For (INT I = 0; I <nrowsize; I ++)
{
BMP buffer [start + I] = pbuffer [0] [I];
}
}
Pai_finish_decompress (& Cinfo );
Pai_destroy_decompress (& Cinfo );

_ Pimg = cvcreateimage (cvsize (W, h), 8, 3 );
For (H = 0; H <_ pimg-> height; H ++)
{
For (W = 0; W <_ pimg-> widthstep; W + = 3)
{
* (_ Pimg-> imagedata + H * _ pimg-> widthstep + W + 0) = * (BMP buffer + H * _ pimg-> widthstep + W + 2 );
* (_ Pimg-> imagedata + H * _ pimg-> widthstep + W + 1) = * (BMP buffer + H * _ pimg-> widthstep + W + 1 );
* (_ Pimg-> imagedata + H * _ pimg-> widthstep + W + 2) = * (BMP buffer + H * _ pimg-> widthstep + W + 0 );
}
}

Free (BMP buffer );

BMP buffer = NULL;
Return _ pimg;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.