Use gdal to construct the image format of opencv

Source: Internet
Author: User

Opencv provides a large number of functions, but opencv supports too few image data formats. At the same time, gdal supports many image formats, but gdal does not have as many Algorithm functions as opencv. The two are really not both the fish and the bear's paw. In order to end this tangle, we want to support multiple image formats and use the rich functions of opencv, so let's look at them: the basic principle is, use gdal to open the image, read the image data, and reconstruct the iplimage type of opencv.

// Convert the data read from gdal to the storage format void cimagetemplatematch: copydatafromgdaltoopencv (void * pimage, dt_8u * pdata, int ibandcount) {cvmat * image = (cvmat *) pimage; int Index = 0; For (INT I = 0; I <image-> rows; I ++) {dt_8u * rrow = (dt_8u *) (image-> data. PTR + I * image-> step); For (Int J = 0; j <image-> Cols * ibandcount; j + = ibandcount) {for (int K = 0; k <ibandcount; k ++) rrow [J + k] = pdata [index + k]; index + = ibandcount ;}for (int K = 0; k <ibandcount; k ++) rrow [Image-> Cols * ibandcount + k] = 205 ;}/// Note: it can only be applied to small images, but not big images, in addition, the number of bandwidths can only be one or three. For more than three bandwidths, modify the code void covertgdal2opencv (const char * pszfile, iplimage ** ppsrcimage) {gdaldataset * posrc = (gdaldataset *) gdalopen (pszfile, ga_readonly); int iwidth = posrc-> getrasterxsize (); int iheight = posrc-> getrasterysize (); int ibandcount = posrc-> getrastercount (); gdaldatatype idatatype = posrc-> getrasterband (1)-> getdatatype (); int idepth = gdalgetdatatypesize (gdaldatatype) idatatype ); // convert the original image format cvsize sourcesize = cvsize (iwidth, iheight) to opencv; * ppsrcimage = cvcreateimage (sourcesize, idepth, ibandcount); cvmat stub, * image = (cvmat *) psrcimage; image = cvgetmat (image, & stub); dt_8u * psrcdata = new dt_8u [iwidth * iheight * ibandcount]; If (ibandcount = 3) {int panbandmap [3] = {3, 2, 1}; // follow the BGR... to read data organization posrc-> rasterio (gf_read, istartcol, istartrow, iwidth, iheight, psrcdata, iwidth, iheight, \ (gdaldatatype) idatatype, ibandcount, panbandmap, idatatype * ibandcount, idatatype * iwidth * ibandcount, idatatype); // read all} else if (ibandcount = 1) {int panbandmap [1] = {1 }; posrc-> rasterio (gf_read, istartcol, istartrow, iwidth, iheight, psrcdata, iwidth, iheight, \ (gdaldatatype) idatatype, ibandcount, panbandmap, idatatype * ibandcount, idatatype * iwidth * ibandcount, idatatype); // read all} else {release (psrcdata); gdalclose (gdaldataseth) posrc); * ppsrcimage = NULL; return ;} copydatafromgdaltoopencv (image, psrcdata, ibandcount); release (psrcdata); gdalclose (gdaldataseth) posrc );}

First, because opencv stores the entire image in memory, this function cannot process large images. In addition, because opencv can only process three-band and single-band images, this function can only process three-band and single-band images. For images in more than three bands, you can modify the program, select three bands for reading. For the Organization format of data in opencv, refer to the relevant documentation of opencv.

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.