07 IplImage和Mat轉換

screen-shot-2016-11-14-at-2-21-23-pm

 

 

sample06_ResizeImage_IplImage_Cobvert_Mat

 

/*
<%%KEEPWHITESPACE%%> 柯博文老師
<%%KEEPWHITESPACE%%> www.powenko.com
<%%KEEPWHITESPACE%%> */
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

using namespace cv ;
using namespace std ;
int main() {

<%%KEEPWHITESPACE%%>    IplImage *srcImg = NULL;
<%%KEEPWHITESPACE%%>    IplImage *dstImg = NULL;

<%%KEEPWHITESPACE%%>    srcImg = cvLoadImage( "/Users/powenko/Desktop/1.png", 1);
<%%KEEPWHITESPACE%%>    if ( srcImg == NULL ) {
<%%KEEPWHITESPACE%%>        return -1;
<%%KEEPWHITESPACE%%>    }
<%%KEEPWHITESPACE%%>    dstImg = cvCreateImage( cvSize(srcImg->width / 2,
<%%KEEPWHITESPACE%%>                                   srcImg->height / 2),
<%%KEEPWHITESPACE%%>                           srcImg->depth,
<%%KEEPWHITESPACE%%>                           3 );

<%%KEEPWHITESPACE%%>    dstImg = cvCreateImage( cvSize(srcImg->width / 2,
<%%KEEPWHITESPACE%%>                                   srcImg->height / 2),
<%%KEEPWHITESPACE%%>                           srcImg->depth,
<%%KEEPWHITESPACE%%>                           3 );
<%%KEEPWHITESPACE%%>    cvResize( srcImg, dstImg, CV_INTER_LINEAR );

<%%KEEPWHITESPACE%%>    cvShowImage( "Source", srcImg );
<%%KEEPWHITESPACE%%>    cvShowImage( "PowenKo.com   Result", dstImg );

<%%KEEPWHITESPACE%%>    // IplImage轉換 程式碼相當簡單,如下:
<%%KEEPWHITESPACE%%>    // IplImage to Mat
<%%KEEPWHITESPACE%%>    cv::Mat mat(dstImg, 0);

<%%KEEPWHITESPACE%%>    //需要注意的是,兩者互轉皆為共用記憶體位置!
<%%KEEPWHITESPACE%%>    // Mat to IplImage
<%%KEEPWHITESPACE%%>    //cv::Mat mat;
<%%KEEPWHITESPACE%%>    //IplImage *img = IplImage(mat);

<%%KEEPWHITESPACE%%>    cv::imshow("PowenKo.com  IplImage to Mat  Result", mat) ;

<%%KEEPWHITESPACE%%>    cv::waitKey() ;

<%%KEEPWHITESPACE%%>    return 0;
}

OpenCV
CH01 簡介CH02 OpenCV APICH03 繪圖CH04 應用範例CH06 haarcascadesCH06 surf和siftCH07 OCRCH09 FaceCH5 輪廓(findContours、drawContours)