OpenCV中IplImage*转化为cv::mat时出现的问题

  最近调试时,需要把工业相机在内存中的图像提取出来做处理,网上的办法是将内存中的图像转化为IplInage*格式,在OpenCV处理中要用到cv::mat格式,这就要有一个转换的问题,一开始参照网上大部分的例子用如下转换:


cv::Mat img2(qImg1,0);


在实际中img2已经在头文件中定义过一次,按照这样的转换方法,貌似要重新定义一次。而且调试中看这个变量没有转换成功,纠结。于是在网上看到了另一种转换方法:

//之前先定义IplImage *iplimg,并且分配空间
//cv::Mat Mimg已经定义
Mat imgMat(iplimg); //Construct an Mat image "img" out of an IplImage
Mimg = iplimg; //Or just set the header of pre existing cv::Mat
//Ming to iplimg's data (no copying is done)

实际调试发现这个是可以用的,哈哈。特此把那篇笔记也粘贴过来:


//在C++中用Mat时可以先包含命名空间,如下
//using namespace cv;
//using namespace std;
 
//之前先定义IplImage *iplimg,并且分配空间
//cv::Mat Mimg已经定义
Mat imgMat(iplimg); //Construct an Mat image "img" out of an IplImage
Mimg = iplimg; //Or just set the header of pre existing cv::Mat
//Ming to iplimg's data (no copying is done)
 
//转化到IplImage或CvMat,不需要数据复制
IplImage ipl_img = img;
CvMat cvmat = img; //转化cv::Mat –> CvMat
A very simple way to operate on a rectanglular sub-region of an image (ROI – “Region of Interest”):
//Make a rectangle
Rect roi(10, 20, 100, 50);
//Point a cv::Mat header at it (no allocation is done)
Mat image_roi = image(roi);
A bit advanced, but should you want efficiently to sample from a circular region in an image (below, instead of sampling, we just draw into a BGR image) :
// the function returns x boundary coordinates of
// the circle for each y. RxV[y1] = x1 means that
// when y=y1, -x1 <=x<=x1 is inside the circle
void getCircularROI(int R, vector < int > & RxV)
{
RxV.resize(R+1);
for( int y = 0; y <= R; y++ )
RxV[y] = cvRound(sqrt((double)R*R - y*y));
}
 
// This draws a circle in the green channel
// (note the "[1]" for a BGR" image,
// blue and red channels are not modified),
// but is really an example of how to *sample* from a circular region.
void drawCircle(Mat &image, int R, Point center)
{
vector<int> RxV;
getCircularROI(R, RxV);
 
Mat_<Vec3b>& img = (Mat_<Vec3b>&)image; //3 channel pointer to image
for( int dy = -R; dy <= R; dy++ )
{
int Rx = RxV[abs(dy)];
for( int dx = -Rx; dx <= Rx; dx++ )
img(center.y+dy, center.x+dx)[1] = 255;
}
}



你可能感兴趣的:(VS2010,opencv,IplImage,CvMat)