Opencv commonly used conversion opencv Image Processing

Source: Internet
Author: User
Allocate and release image space

 

  • Allocate image space:

    Iplimage * cvcreateimage (cvsize size, int depth, int channels );

    Size: cvsize (width, height );

    Depth: ipl_depth_8u, ipl_depth_8s, ipl_depth_16u,
    Ipl_depth_16s, ipl_depth_32s, ipl_depth_32f, ipl_depth_64f

    Channels: 1, 2, 3 or 4.
    Note that the data is cross-access. The color image data is arranged as B0 G0 R0 B1 G1 R1...

    Example:

    // Allocate a single-channel byte Image
    Iplimage * img1 = cvcreateimage (cvsize (640,480), ipl_depth_8u, 1 );

    // Allocate a three-channel floating point image
    Iplimage * img2 = cvcreateimage (cvsize (640,480), ipl_depth_32f, 3 );

     

  • Release image space:
    IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); 
    cvReleaseImage(&img);

     

  • Copy image:
    IplImage* img1=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1); 
    IplImage* img2;
    img2=cvCloneImage(img1);

     

  • Set/get interest areas:
    Void cvsetimageroi (iplimage * image, cvrect rect );
    Void cvresetimageroi (iplimage * image );
    Vrect cvgetimageroi (const iplimage * image );

    Most opencv Functions Support ROI.

     

  • Set/get interest channels:
    Void cvsetimagecoi (iplimage * image, int COI); // 0 = all
    Int cvgetimagecoi (const iplimage * image );

    Most opencv functions do not currently support COI.

     

 

Read stored Images

 

  • Load images from files:

       IplImage* img=0; 
       img=cvLoadImage(fileName);
       if(!img) printf("Could not load image file: %s/n",fileName);

       Supported image formats: BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM,
                                SR, RAS, TIFF, TIF

    By default, the loaded image is converted to a 3-channel color image. If not, the flag must be added:

    IMG = cvloadimage (filename, flag );

    Flag:> 0 loaded image to three-channel Color Image
    = 0 load the image into a single channel grayscale image
    <0: Do not convert and load the image (the number of channels is the same as the number of image files ).

     

  • Image Storage is an image file:
       if(!cvSaveImage(outFileName,img)) printf("Could not save: %s/n",outFileName);

    The format of the input file is determined by the file extension.

     

 

Accessing image elements

 

  • Assume that you need to read the K channel of the J-column image point in row I. the range of row I is [0, height-1], the range of column J is [0, width-1], and the range of channel K is [0, nchannels-1].

     

  • Indirect access:(Relatively common, but inefficient, and can read any type of image data)

     

    • For single-channel byte images:

      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
      CvScalar s;
      s=cvGet2D(img,i,j); // get the (i,j) pixel value
      printf("intensity=%f/n",s.val[0]);
      s.val[0]=111;
      cvSet2D(img,i,j,s); // set the (i,j) pixel value

       

    • For multi-channel floating point or byte images:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
      CvScalar s;
      s=cvGet2D(img,i,j); // get the (i,j) pixel value
      printf("B=%f, G=%f, R=%f/n",s.val[0],s.val[1],s.val[2]);
      s.val[0]=111;
      s.val[1]=111;
      s.val[2]=111;
      cvSet2D(img,i,j,s); // set the (i,j) pixel value

       

     

  • Direct access:(High efficiency, but error-prone)

     

    • For single-channel byte images:

      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
      ((uchar *)(img->imageData + i*img->widthStep))[j]=111;

       

    • For multi-channel byte images:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
      ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
      ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
      ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R

       

    • For multi-channel floating point images:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
      ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 0]=111; // B
      ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1]=112; // G
      ((float *)(img->imageData + i*img->widthStep))[j*img->nChannels + 2]=113; // R

       

     

  • Direct access with pointers:(In some cases, it is simple and efficient)

     

    • For single-channel byte images:

      IplImage* img   = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
      int height      = img->height;
      int width       = img->width;
      int step        = img->widthStep/sizeof(uchar);
      uchar* data     = (uchar *)img->imageData;
      data[i*step+j] = 111;

       

    • For multi-channel byte images:
      IplImage* img   = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
      int height      = img->height;
      int width       = img->width;
      int step        = img->widthStep/sizeof(uchar);
      int channels    = img->nChannels;
      uchar* data     = (uchar *)img->imageData;
      data[i*step+j*channels+k] = 111;

       

    • For single-channel floating-point images (4-byte adjustment ):
      IplImage* img   = cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
      int height      = img->height;
      int width       = img->width;
      int step        = img->widthStep/sizeof(float);
      int channels    = img->nChannels;
      float * data     = (float *)img->imageData;
      data[i*step+j*channels+k] = 111;

       

     

     

  • Direct access using C ++ wrapper:(Simple and efficient)

     

    • For single/multi-channel byte images, a multi-channel floating point image is defined as a C ++ wrapper:

      template<class T> class Image
      {
         private:
         IplImage* imgp;
         public:
         Image(IplImage* img=0) {imgp=img;}
         ~Image(){imgp=0;}
         void operator=(IplImage* img) {imgp=img;}
         inline T* operator[](const int rowIndx) {
           return ((T *)(imgp->imageData + rowIndx*imgp->widthStep));}
      };

      typedef struct{
         unsigned char b,g,r;
      } RgbPixel;

      typedef struct{
         float b,g,r;
      } RgbPixelFloat;

      typedef Image<RgbPixel>        RgbImage;
      typedef Image<RgbPixelFloat>   RgbImageFloat;
      typedef Image<unsigned char>   BwImage;
      typedef Image<float>           BwImageFloat;

       

    • Single-channel byte image:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,1);
      BwImage imgA(img);
      imgA[i][j] = 111;

       

    • Multi-channel byte image:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
      RgbImage   imgA(img);
      imgA[i][j].b = 111;
      imgA[i][j].g = 111;
      imgA[i][j].r = 111;

       

    • Multi-channel floating point image:
      IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
      RgbImageFloat imgA(img);
      imgA[i][j].b = 111;
      imgA[i][j].g = 111;
      imgA[i][j].r = 111;

       

     

 

Image Conversion

 

  • Convert to grayscale or color byte image:

    cvConvertImage(src, dst, flags=0);

       src = float/byte grayscale/color image
       dst = byte grayscale/color image
       flags = CV_CVTIMG_FLIP      (flip vertically)
               CV_CVTIMG_SWAP_RB   (swap the R and B channels)

     

  • Convert a color image to a grayscale image:

     

    Use the opencv conversion function:

    cvCvtColor(cimg,gimg,CV_BGR2GRAY); // cimg -> gimg

     

    Direct conversion:

    for(i=0;i<cimg->height;i++) for(j=0;j<cimg->width;j++) 
       gimgA[i][j]= (uchar)(cimgA[i][j].b*0.114 +
                            cimgA[i][j].g*0.587 +
                            cimgA[i][j].r*0.299);

     

  • Color Space conversion:

     

    cvCvtColor(src,dst,code); // src -> dst

       code     = CV_<X>2<Y>
       <X>/<Y> = RGB, BGR, GRAY, HSV, YCrCb, XYZ, Lab, Luv, HLS

    e.g.: CV_BGR2GRAY, CV_BGR2HSV, CV_BGR2Lab

     

 

Drawing command

 

  • Draw a cube:

    // Draw a cube between (100,100) and (200,200) with a red line with a width of 1.
    Cvrectangle (IMG, cvpoint (100,100), cvpoint (200,200), cvscalar (, 0), 1 );

     

  • Circle:
    // Draw a circle with a radius of 20 at (100,100) and use a green line with a width of 1
    Cvcircle (IMG, cvpoint (100,100), 20, cvscalar (0,255, 0), 1 );

     

  • Draw line segments:
    // Draw a green line between (100,100) and (200,200) with a width of 1
    Cvline (IMG, cvpoint (100,100), cvpoint (200,200), cvscalar (0,255, 0), 1 );

     

  • Draw a group of line segments:
    CvPoint   curve1[]={10,10,   10,100,   100,100,   100,10};
    CvPoint   curve2[]={30,30,   30,130,   130,130,   130,30,   150,10};
    CvPoint* curveArr[2]={curve1, curve2};
    int       nCurvePts[2]={4,5};
    int       nCurves=2;
    int       isCurveClosed=1;
    int       lineWidth=1;

    cvPolyLine(img,curveArr,nCurvePts,nCurves,isCurveClosed,cvScalar(0,255,255),lineWidth);

     

  • Filled polygon in the painting:
    cvFillPoly(img,curveArr,nCurvePts,nCurves,cvScalar(0,255,255));

     

  • Add text:
    CvFont font;
    double hScale=1.0;
    double vScale=1.0;
    int     lineWidth=1;
    cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth);

    cvPutText (img,"My comment",cvPoint(200,400), &font, cvScalar(255,255,0));

    Other possible fonts:

    CV_FONT_HERSHEY_SIMPLEX, CV_FONT_HERSHEY_PLAIN,
    CV_FONT_HERSHEY_DUPLEX, CV_FONT_HERSHEY_COMPLEX,
    CV_FONT_HERSHEY_TRIPLEX, CV_FONT_HERSHEY_COMPLEX_SMALL,
    CV_FONT_HERSHEY_SCRIPT_SIMPLEX, CV_FONT_HERSHEY_SCRIPT_COMPLEX,

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.