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;
}
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值