(长期更新)
1、Mat中数据传递、赋值,操作clone() 与copyto()的区别。
Mat imageSrc = imread("1.png" , 0) ;
Mat imageDst(imageSrc) ;//仅是创建了Mat的头部分,共享数据区
Mat imageDst = imageSrc ;//仅是创建了Mat的头部分,共享数据区
Mat imageDst = imageSrc.clone() ;//完全拷贝,把imageSrc中的所有信息拷贝到imageDst中
Mat imageDst ;imageSrc.copyTo(imageDst) ;//拷贝imageSrc的数据区到imageDst中,在拷贝数据前会有一步:imageDst.create(this->size , this->type)
2、vector<Point2f>转存到Mat:
Mat VecToMat(vector<Point2f> vecPoint)
{
int nSize = vecPoint.size();
Mat matOut = Mat(nSize, 2, CV_64F);
for (int i = 0; i < matOut.rows; i++)
{
matOut.at<double>(i, 0) = vecPoint[i].x;
matOut.at<double>(i, 1) = vecPoint[i].y;
}
return matOut
}
3、画闭合点连线
Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
line(test_image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0), 2);
4、捕获异常
try
{
}
catch (cv::Exception& e)
{
char* msg_e = e.what();
return -1;
}
5、绘制带方向箭头的直线
void drawAxis(Mat& img, Point p, Point q, Scalar colour, const float scale = 0.2)
{
//! [visualization1]
double angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); // angle in radians
double hypotenuse = sqrt( (double) (p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
// Here we lengthen the arrow by a factor of scale
q.x = (int) (p.x - scale * hypotenuse * cos(angle));
q.y = (int) (p.y - scale * hypotenuse * sin(angle));
line(img, p, q, colour, 1, LINE_AA);
// create the arrow hooks
p.x = (int) (q.x + 9 * cos(angle + CV_PI / 4));
p.y = (int) (q.y + 9 * sin(angle + CV_PI / 4));
line(img, p, q, colour, 1, LINE_AA);
p.x = (int) (q.x + 9 * cos(angle - CV_PI / 4));
p.y = (int) (q.y + 9 * sin(angle - CV_PI / 4));
line(img, p, q, colour, 1, LINE_AA);
//! [visualization1]
}