// 绘制同心圆
void DrawConcentricCircle(cv::Mat mask, const cv::Point2i center, int radius1,int radius2, const cv::Scalar color, int thickness,int linetype)
{
// 创建画布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
// 计算内径和外径
int inradius = min(radius1, radius2);
int outradius = max(radius1, radius2);
// 分情况讨论
// 当thickness大于0时,绘制的是两个圆型线条组成的同心圆,不需填充
if (thickness > 0)
{
cv::circle(mask, center, outradius, color, thickness, linetype);
cv::circle(mask, center, inradius, color, thickness, linetype);
}
// 当thickness小于0,一般为-1,绘制的是填充同心圆,内圆不能有填充色
else {
cv::circle(canvas, center, outradius, cv::Scalar(255), -1, linetype);
cv::circle(canvas, center, inradius, cv::Scalar(0), -1, linetype);
int row = mask.rows;
int col = mask.cols;
for (int i = 0; i row; ++i)
{
for (int j = 0; j col; ++j)
{
uchar *m = canvas.ptruchar>(i);
if (m[j] == 255)
{
mask.atVec3b>(i, j)[0] = static_castuchar>(color[0]);
mask.atVec3b>(i, j)[1] = static_castuchar>(color[1]);
mask.atVec3b>(i, j)[2] = static_castuchar>(color[2]);
}
}
}
}
}
#include iostream>
#include opencv2/opencv.hpp>
#include stdio.h>
using namespace std;
using namespace cv;
void DrawConcentricCircle(cv::Mat mask, const cv::Point2i center, int radius1, int radius2, const cv::Scalar color, int thickness, int linetype);
int main()
{
cv::Mat src = imread("test.jpg");
cv::Mat result = src.clone();
DrawConcentricCircle(result, cv::Point(result.cols / 2, result.rows / 2), 300, 500, cv::Scalar(255, 255, 0),-10, 16);
imshow("original", src);
imshow("result", result);
waitKey(0);
system("pause");
return 0;
}
// 绘制同心圆
void DrawConcentricCircle(cv::Mat mask, const cv::Point2i center, int radius1,int radius2, const cv::Scalar color, int thickness,int linetype)
{
// 创建画布
cv::Mat canvas = cv::Mat::zeros(mask.size(), CV_8UC1);
// 计算内径和外径
int inradius = min(radius1, radius2);
int outradius = max(radius1, radius2);
// 分情况讨论
// 当thickness大于0时,绘制的是两个圆型线条组成的同心圆,不需填充
if (thickness > 0)
{
cv::circle(mask, center, outradius, color, thickness, linetype);
cv::circle(mask, center, inradius, color, thickness, linetype);
}
// 当thickness小于0,一般为-1,绘制的是填充同心圆,内圆不能有填充色
else {
cv::circle(canvas, center, outradius, cv::Scalar(255), -1, linetype);
cv::circle(canvas, center, inradius, cv::Scalar(0), -1, linetype);
int row = mask.rows;
int col = mask.cols;
for (int i = 0; i row; ++i)
{
for (int j = 0; j col; ++j)
{
uchar *m = canvas.ptruchar>(i);
if (m[j] == 255)
{
mask.atVec3b>(i, j)[0] = static_castuchar>(color[0]);
mask.atVec3b>(i, j)[1] = static_castuchar>(color[1]);
mask.atVec3b>(i, j)[2] = static_castuchar>(color[2]);
}
}
}
}
}
到此这篇关于OpenCV 绘制同心圆的示例代码的文章就介绍到这了,更多相关OpenCV 绘制同心圆内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!