iplimage* 和uchar*互转,mat 和uchar*

来源:互联网 发布:迅雷for mac 编辑:程序博客网 时间:2024/06/05 08:12
#include "opencv2/opencv.hpp"
using namespace cv;
int main()

 //图片路径

string image_name = "H:\\vegData\\1\\yuanshi\\dongua\\IMG_20170407_132802_BURST1.jpg";

Mat img = imread(image_name);

IplImage *pImg;

int frameWidth = img.cols;
int frameHeight = img.rows;

int framenChannels = 3;

pImg = &(IplImage)img;

int framewidthStep = pImg->widthStep;

uchar *buf, *src;

 //Mat 转uchar

buf=img.data;

    

//uchar 转iplimage

//----------------------------------------------------------------------------------------------------------------------------------
IplImage* _pImg = cvCreateImage(cvSize(frameWidth, frameHeight), 8, framenChannels);
for (int i = 0; i < frameHeight; i++)
{
for (int j = 0; j < frameWidth; j++)
{
_pImg->imageData[(i*frameWidth + j) * 3 + 0] = buf[i*framewidthStep + j * 3 + 0];
_pImg->imageData[(i*frameWidth + j) * 3 + 1] = buf[i*framewidthStep + j * 3 + 1];
_pImg->imageData[(i*frameWidth + j) * 3 + 2] = buf[i*framewidthStep + j * 3 + 2];
}
}

//------------------------------------------------------------------------------------------------------------------------------------------


//iplimage转uchar 
uchar* data = new uchar[IplImage->width * IplImage->height];
memcpy_s(data, IplImage->width * IplImage->height, IplImage->imageData, IplImage->width * IplImage->height);

//uchar 转mat
Mat image(frameHeight, frameWidth, CV_8UC3, buf);
imshow("image", image);
waitKey();




}
0 0