在第一次使用cvRectangle绘制矩形的时候遇到一个问题:Error:不存在从“cv::Mat”到"CvArr*"的适当转换函数,就特意查了查资料,总结如下。
cvRentangle和cv::rectangle函数原型对比:
C: void cvRectangle(CvArr* img,
CvPoint pt1, CvPoint pt2,
CvScalar color, int thickness=1,
int line_type=8, int shift=0
)
-
C++: void rectangle(Mat& img,
Point pt1,Point pt2,const Scalar& color, int thickness=1, int lineType=8, int shift=0)
-
C++: void rectangle(Mat& img,
Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
参数介绍:
#include <iostream> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; int main() { char *imageSrc = "I:\\OpenCV Learning\\picture\\sumpalace.jpg"; Mat matImage = imread(imageSrc,-1); IplImage *iplImage = cvLoadImage(imageSrc,-1); if(matImage.data==0||iplImage->imageData ==0) { cout<<"图片加载失败"<<endl; return -1; } cv::rectangle(matImage,cvPoint(20,200),cvPoint(200,300),Scalar(255,0,0),1,1,0); //Rect(int a,int b,int c,int d)a,b为矩形的左上角坐标,c,d为矩形的长和宽 cv::rectangle(matImage,Rect(100,300,20,200),Scalar(0,0,255),1,1,0); cvRectangle(iplImage,cvPoint(20,200),cvPoint(200,300),Scalar(0,255,255),1,1,0); imshow("matImage",matImage); cvShowImage("IplImage",iplImage); waitKey(); return 0; }
结果:
如果需要在Mat类型的图上绘制矩形,选择cv::trctangle()
在IplImage*类型的图上绘制矩形,选择cvRectangle()
时间: 2024-10-13 02:06:28