26

26,第1张

cv::clipLine()函数判断pt1到pt2的直线是否在一个矩形范围内。


函数声明如下:

bool clipLine(  // true if any part of line in imgRect
	cv::Rect imgRect,  // rectangle to clip to
	cv::Point& pt1,  // first endpoint of line overwritten
	cv::Point& pt2  // second endpoint of line, overwritten
);
bool clipLine(  // true if any part of line in image size
	cv::Size imgSize,  // size of image,implies rectangle at 0,0
	cv::Point& pt1,  // first endpoint of line,overwritten
	cv::Point& pt2  // second endpoint of line,overwritten
);

第一种函数的形式使用了cv::Rect,直线和这个矩形比较;第二个函数只有cv::Size,该形式表示矩形的范围是从(0,0)开始的。


只有当直线完全在指定的矩形范围之外时,函数cv::clipLine()才会返回false。


使用示例:

	cv::Mat image(500, 500, CV_8UC3, cv::Scalar(0, 0, 0));
	cv::namedWindow("image");
	

	cv::Rect rect(30, 30, 100, 200);
	cv::Point pt1(20, 20);
	cv::Point pt2(200, 400);

	cv::rectangle(image, rect, CV_RGB(0, 255, 0), 2);
	cv::line(image, pt1, pt2, CV_RGB(255, 255, 0));  // 黄色
	std::cout << cv::clipLine(rect, pt1, pt2) << std::endl;
	
	pt1.x = 20;
	pt1.y = 20;
	pt2.x = 260;
	pt2.y = 460;
	
	cv::line(image, pt1, pt2, CV_RGB(0, 255, 255));  // 青色
	std::cout << cv::clipLine(rect, pt1, pt2) << std::endl;
	
	pt1.x = 240;
	pt1.y = 440;
	pt2.x = 260;
	pt2.y = 460;

	cv::line(image, pt1, pt2, CV_RGB(255, 0, 255));  // 紫色
	std::cout << cv::clipLine(rect, pt1, pt2) << std::endl;
	
	cv::imshow("image", image);
	cv::waitKey(0);

显示结果:

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/564257.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-03
下一篇 2022-04-03

发表评论

登录后才能评论

评论列表(0条)

保存