欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用c++实现OpenCV绘制圆端矩形

程序员文章站 2022-03-10 10:45:55
功能函数// 绘制圆端矩形(药丸状,pill)void drawpill(cv::mat mask, const cv::rotatedrect &rotatedrect, const cv:...

功能函数

// 绘制圆端矩形(药丸状,pill)
void drawpill(cv::mat mask, const cv::rotatedrect &rotatedrect, const cv::scalar &color, int thickness, int linetype)
{
	cv::mat canvas = cv::mat::zeros(mask.size(), cv_8uc1);
	// 确定短边,短边绘制圆形
	cv::rotatedrect rect = rotatedrect;
	float r = rect.size.height / 2.0f;
	if (rect.size.width > rect.size.height) {
		rect.size.width -= rect.size.height;
	}
	else {
		rect.size.height -= rect.size.width;
		r = rect.size.width / 2.0f;
	}
	cv::point2f ps[4];
	rect.points(ps);
 
	// 绘制边缘
	std::vector<std::vector<cv::point>> tmpcontours;
	std::vector<cv::point> contours;
	for (int i = 0; i != 4; ++i) {
		contours.emplace_back(cv::point2i(ps[i]));
	}
	tmpcontours.insert(tmpcontours.end(), contours);
	drawcontours(canvas, tmpcontours, 0, cv::scalar(255),5, linetype);  // 填充mask
 	// 计算常长短轴
	float a = rotatedrect.size.width;
	float b = rotatedrect.size.height;
 	int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
	int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
	int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
	int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
	int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
	int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
	int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
	int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
 	cv::point c0 = a < b ? cv::point(point12_x, point12_y) : cv::point(point23_x, point23_y);
	cv::point c1 = a < b ? cv::point(point03_x, point03_y) : cv::point(point01_x, point01_y);
 	// 长轴两端以填充的方式画圆,直径等于短轴
	cv::circle(canvas, c0, (int)r, cv::scalar(255), 5, linetype);
	cv::circle(canvas, c1, (int)r, cv::scalar(255), 5, linetype);
 	// 绘制外围轮廓,如果不这样操作,会得到一个矩形加两个圆形,丑。。。
	std::vector<std::vector<cv::point>> excontours;
	cv::findcontours(canvas,excontours,cv::retr_external, chain_approx_simple);
	drawcontours(mask, excontours, 0, color, thickness,linetype);  // 填充mask
}

测试代码

#include <iostream>
#include <opencv2/opencv.hpp> 
using namespace std;
using namespace cv;
void drawpill(cv::mat mask, const cv::rotatedrect &rotatedrect, const cv::scalar &color, int thickness, int linetype); 
int main()
{
	cv::mat src = imread("test.jpg");
	cv::mat result = src.clone();
	cv::rotatedrect rorect(cv::point(src.cols / 2, src.rows / 2), cv::size(1000, 800), 50);
	drawpill(result, rorect, cv::scalar(0, 255, 255),8,16);
	imshow("original", src);
	imshow("result", result);
	waitkey(0);
	return 0;
}
 // 绘制圆端矩形(药丸状,pill)
void drawpill(cv::mat mask, const cv::rotatedrect &rotatedrect, const cv::scalar &color, int thickness, int linetype)
{
	cv::mat canvas = cv::mat::zeros(mask.size(), cv_8uc1);
	// 确定短边,短边绘制圆形
	cv::rotatedrect rect = rotatedrect;
	float r = rect.size.height / 2.0f;
	if (rect.size.width > rect.size.height) {
		rect.size.width -= rect.size.height;
	}
	else {
		rect.size.height -= rect.size.width;
		r = rect.size.width / 2.0f;
	}
	cv::point2f ps[4];
	rect.points(ps);
 	// 绘制边缘
	std::vector<std::vector<cv::point>> tmpcontours;
	std::vector<cv::point> contours;
	for (int i = 0; i != 4; ++i) {
		contours.emplace_back(cv::point2i(ps[i]));
	}
	tmpcontours.insert(tmpcontours.end(), contours);
	drawcontours(canvas, tmpcontours, 0, cv::scalar(255),5, linetype);  // 填充mask
 	// 计算常长短轴
	float a = rotatedrect.size.width;
	float b = rotatedrect.size.height;
 	int point01_x = (int)((ps[0].x + ps[1].x) / 2.0f);
	int point01_y = (int)((ps[0].y + ps[1].y) / 2.0f);
	int point03_x = (int)((ps[0].x + ps[3].x) / 2.0f);
	int point03_y = (int)((ps[0].y + ps[3].y) / 2.0f);
	int point12_x = (int)((ps[1].x + ps[2].x) / 2.0f);
	int point12_y = (int)((ps[1].y + ps[2].y) / 2.0f);
	int point23_x = (int)((ps[2].x + ps[3].x) / 2.0f);
	int point23_y = (int)((ps[2].y + ps[3].y) / 2.0f);
	cv::point c0 = a < b ? cv::point(point12_x, point12_y) : cv::point(point23_x, point23_y);
	cv::point c1 = a < b ? cv::point(point03_x, point03_y) : cv::point(point01_x, point01_y);
 	// 长轴两端以填充的方式画圆,直径等于短轴
	cv::circle(canvas, c0, (int)r, cv::scalar(255), 5, linetype);
	cv::circle(canvas, c1, (int)r, cv::scalar(255), 5, linetype);
 	// 绘制外围轮廓,如果不这样操作,会得到一个矩形加两个圆形,丑。。。
	std::vector<std::vector<cv::point>> excontours;
	cv::findcontours(canvas,excontours,cv::retr_external, chain_approx_simple);
	drawcontours(mask, excontours, 0, color, thickness,linetype);  // 填充mask
}

测试效果 

使用c++实现OpenCV绘制圆端矩形 

图1 原图

使用c++实现OpenCV绘制圆端矩形

图2 绘制圆端矩形

绘制圆端矩形其实就是绘制了一个旋转矩形,然后分析哪个轴更长,就在哪个轴上的两端画圆,再取外围轮廓,大功告成,通俗来讲就画了一个矩形两个圆,如图3所示。

使用c++实现OpenCV绘制圆端矩形 

图3 绘制逻

不过注意,这个图形最好不要超过图像边界,因为超过后再分析外围轮廓,它认为的外围就到了内部,如图4所示。

使用c++实现OpenCV绘制圆端矩形

 图4 外围线

然后,你就会得到一个奇葩图形,如图5所示。

使用c++实现OpenCV绘制圆端矩形 

图5 示意图

以上就是使用c++实现opencv绘制圆端矩形的详细内容,更多关于c++实现opencv绘制圆端矩形的资料请关注其它相关文章!