绘制逻辑
奥运五环由5个同心圆套接组成,有蓝、黄、黑、绿、红5种颜色。环从左到右互相套接,上面是蓝、黑、红环,下面是是黄、绿环。整个造形为一个底部小的规则梯形。大多数人绘制一般只将五个同心圆按顺序排布,但是真正的五环是套接而成,所以本文绘制了带套接效果的奥运五环。
首先输入绘制的区域信息和五环的环宽,限制下区域宽高和环宽尺寸,避免程序崩溃;其次,正常绘制五个不同颜色的同心圆,以及各自的掩膜图;将交叉的两个环取交集建立交集掩膜图,共有4个交集,分别是蓝色和黄色、黄色和黑色、黑色和绿色、绿色和红色;按照交集区域颜色的优先级,清除交集掩膜图中的非操作区域,对操作区域进行颜色重绘,完毕。
话不多说,下方为具体实现函数和测试代码。
功能函数代码
// 绘制奥运五环 cv::Mat DrawOlympicRings(cv::Mat src,cv::Rect rect,int w) { if (rect.width < 400) rect.width = 400; if (rect.height < 400) rect.height = 400; // 设置五环半径 int r = min(rect.width, rect.height) / 7; if (w > r / 5) w = r / 5; cv::Mat result = src.clone(); int row = result.rows; int col = result.cols; // 蓝色为1,黑色为2,红色为3,黄色为4,绿色为5 cv::Point point2 = cv::Point(int(rect.x + rect.width / 2), int(rect.y + rect.height / 2 - 0.55*r)); cv::Point point4 = cv::Point(int(rect.x + rect.width / 2 - 1.1*r), int(rect.y + rect.height / 2 + 0.55*r)); cv::Point point5 = cv::Point(int(rect.x + rect.width / 2 + 1.1*r), int(rect.y + rect.height / 2 + 0.55*r)); cv::Point point1 = cv::Point(int(rect.x + rect.width / 2 - 2.2*r), int(rect.y + rect.height / 2 - 0.55*r)); cv::Point point3 = cv::Point(int(rect.x + rect.width / 2 + 2.2*r), int(rect.y + rect.height / 2 - 0.55*r)); // 初步绘制同心圆 DrawConcentricCircle(result, point1, r - w, r, cv::Scalar(255, 0, 0), -1, 16); DrawConcentricCircle(result, point2, r - w, r, cv::Scalar(0, 0, 0), -1, 16); DrawConcentricCircle(result, point3, r - w, r, cv::Scalar(0, 0, 255), -1, 16); DrawConcentricCircle(result, point4, r - w, r, cv::Scalar(0, 255, 255), -1, 16); DrawConcentricCircle(result, point5, r - w, r, cv::Scalar(0, 255, 0), -1, 16); // 绘制五环掩膜位置 cv::Mat temp = result.clone(); cv::Mat mask1 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask1, point1, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask2 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask2, point2, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask3 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask3, point3, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask4 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask4, point4, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask5 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask5, point5, r - w, r, cv::Scalar(255, 255, 255), -1, 16); // 五环交集 cv::Mat blury = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat blacky = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat blackg = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat greenr = cv::Mat::zeros(result.size(), CV_8UC1); for (int i = rect.y; i < (rect.y+rect.height); ++i) { for (int j = rect.x; j < (rect.x+rect.width); ++j) { if (mask1.at<Vec3b>(i, j)[0] == 255 && mask4.at<Vec3b>(i, j)[0] == 255) blury .at<uchar>(i, j) = 255; if (mask2.at<Vec3b>(i, j)[0] == 255 && mask4.at<Vec3b>(i, j)[0] == 255) blacky.at<uchar>(i, j) = 255; if (mask2.at<Vec3b>(i, j)[0] == 255 && mask5.at<Vec3b>(i, j)[0] == 255) blackg.at<uchar>(i, j) = 255; if (mask3.at<Vec3b>(i, j)[0] == 255 && mask5.at<Vec3b>(i, j)[0] == 255) greenr.at<uchar>(i, j) = 255; } } // 目前黄环和绿环压在蓝黑红三环上面 // 按照五环交叠的规律,消除对应的重叠区,使上面的三环在对应位置反压下面两环 clearArea(blury , 0); clearArea(blacky, 1); clearArea(blackg, 0); clearArea(greenr, 1); for (int i = rect.y; i < (rect.y + rect.height); ++i) { for (int j = rect.x; j < (rect.x + rect.width); ++j) { uchar *m1 = blury .ptr<uchar>(i); uchar *m2 = blacky.ptr<uchar>(i); uchar *m3 = blackg.ptr<uchar>(i); uchar *m4 = greenr.ptr<uchar>(i); if (m1[j] == 255) { result.at<Vec3b>(i, j)[0] = 255; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 0; } if (m2[j] == 255|| m3[j] == 255) { result.at<Vec3b>(i, j)[0] = 0; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 0; } if (m4[j] == 255) { result.at<Vec3b>(i, j)[0] = 0; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 255; } } } return result; } // 绘制同心圆 void DrawConcentricCircle(cv::Mat mask, const cv::Point2i ¢er, 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.ptr<uchar>(i); if (m[j] == 255) { mask.at<Vec3b>(i, j)[0] = static_cast<uchar>(color[0]); mask.at<Vec3b>(i, j)[1] = static_cast<uchar>(color[1]); mask.at<Vec3b>(i, j)[2] = static_cast<uchar>(color[2]); } } } } } // 清除第id个连通区 void clearArea(cv::Mat& mask, int id) { std::vector<std::vector<cv::Point> > contours; // 创建轮廓容器 std::vector<cv::Vec4i> hierarchy; cv::findContours(mask, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point()); if (!contours.empty() && !hierarchy.empty()) { if (id<contours.size()) { cv::Rect rect = cv::boundingRect(cv::Mat(contours[id])); for (int i = rect.y; i < rect.y + rect.height; i++) { uchar *output_data = mask.ptr<uchar>(i); for (int j = rect.x; j < rect.x + rect.width; j++) { // 将连通区的值置0 if (output_data[j] == 255) { output_data[j] = 0; } } } } } }
C++测试代码
#include <iostream> #include <opencv2/opencv.hpp> #include <time.h> using namespace std; using namespace cv; cv::Mat DrawOlympicRings(cv::Mat src, cv::Rect rect, int w); void DrawConcentricCircle(cv::Mat mask, const cv::Point2i ¢er, int radius1, int radius2, const cv::Scalar &color, int thickness, int linetype); void clearArea(cv::Mat& mask, int id); int main() { cv::Mat src = imread("test.jpg"); cv::Mat result = src.clone(); cv::Rect rect = cv::Rect(10, 10, 800, 800); int width = 20; clock_t start, end; start = clock(); result = DrawOlympicRings(result,rect,width); end = clock(); double diff = end - start; cout << "time:" << diff / CLOCKS_PER_SEC << endl; imshow("original", src); imshow("result", result); waitKey(0); system("pause"); return 0; } // 绘制奥运五环 cv::Mat DrawOlympicRings(cv::Mat src,cv::Rect rect,int w) { if (rect.width < 400) rect.width = 400; if (rect.height < 400) rect.height = 400; // 设置五环半径 int r = min(rect.width, rect.height) / 7; if (w > r / 5) w = r / 5; cv::Mat result = src.clone(); int row = result.rows; int col = result.cols; // 蓝色为1,黑色为2,红色为3,黄色为4,绿色为5 cv::Point point2 = cv::Point(int(rect.x + rect.width / 2), int(rect.y + rect.height / 2 - 0.55*r)); cv::Point point4 = cv::Point(int(rect.x + rect.width / 2 - 1.1*r), int(rect.y + rect.height / 2 + 0.55*r)); cv::Point point5 = cv::Point(int(rect.x + rect.width / 2 + 1.1*r), int(rect.y + rect.height / 2 + 0.55*r)); cv::Point point1 = cv::Point(int(rect.x + rect.width / 2 - 2.2*r), int(rect.y + rect.height / 2 - 0.55*r)); cv::Point point3 = cv::Point(int(rect.x + rect.width / 2 + 2.2*r), int(rect.y + rect.height / 2 - 0.55*r)); // 初步绘制同心圆 DrawConcentricCircle(result, point1, r - w, r, cv::Scalar(255, 0, 0), -1, 16); DrawConcentricCircle(result, point2, r - w, r, cv::Scalar(0, 0, 0), -1, 16); DrawConcentricCircle(result, point3, r - w, r, cv::Scalar(0, 0, 255), -1, 16); DrawConcentricCircle(result, point4, r - w, r, cv::Scalar(0, 255, 255), -1, 16); DrawConcentricCircle(result, point5, r - w, r, cv::Scalar(0, 255, 0), -1, 16); // 绘制五环掩膜位置 cv::Mat temp = result.clone(); cv::Mat mask1 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask1, point1, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask2 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask2, point2, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask3 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask3, point3, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask4 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask4, point4, r - w, r, cv::Scalar(255, 255, 255), -1, 16); cv::Mat mask5 = cv::Mat::zeros(result.size(), CV_8UC3); DrawConcentricCircle(mask5, point5, r - w, r, cv::Scalar(255, 255, 255), -1, 16); // 五环交集 cv::Mat blury = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat blacky = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat blackg = cv::Mat::zeros(result.size(), CV_8UC1); cv::Mat greenr = cv::Mat::zeros(result.size(), CV_8UC1); for (int i = rect.y; i < (rect.y+rect.height); ++i) { for (int j = rect.x; j < (rect.x+rect.width); ++j) { if (mask1.at<Vec3b>(i, j)[0] == 255 && mask4.at<Vec3b>(i, j)[0] == 255) blury .at<uchar>(i, j) = 255; if (mask2.at<Vec3b>(i, j)[0] == 255 && mask4.at<Vec3b>(i, j)[0] == 255) blacky.at<uchar>(i, j) = 255; if (mask2.at<Vec3b>(i, j)[0] == 255 && mask5.at<Vec3b>(i, j)[0] == 255) blackg.at<uchar>(i, j) = 255; if (mask3.at<Vec3b>(i, j)[0] == 255 && mask5.at<Vec3b>(i, j)[0] == 255) greenr.at<uchar>(i, j) = 255; } } // 目前黄环和绿环压在蓝黑红三环上面 // 按照五环交叠的规律,消除对应的重叠区,使上面的三环在对应位置反压下面两环 clearArea(blury , 0); clearArea(blacky, 1); clearArea(blackg, 0); clearArea(greenr, 1); for (int i = rect.y; i < (rect.y + rect.height); ++i) { for (int j = rect.x; j < (rect.x + rect.width); ++j) { uchar *m1 = blury .ptr<uchar>(i); uchar *m2 = blacky.ptr<uchar>(i); uchar *m3 = blackg.ptr<uchar>(i); uchar *m4 = greenr.ptr<uchar>(i); if (m1[j] == 255) { result.at<Vec3b>(i, j)[0] = 255; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 0; } if (m2[j] == 255|| m3[j] == 255) { result.at<Vec3b>(i, j)[0] = 0; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 0; } if (m4[j] == 255) { result.at<Vec3b>(i, j)[0] = 0; result.at<Vec3b>(i, j)[1] = 0; result.at<Vec3b>(i, j)[2] = 255; } } } return result; } // 绘制同心圆 void DrawConcentricCircle(cv::Mat mask, const cv::Point2i ¢er, 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.ptr<uchar>(i); if (m[j] == 255) { mask.at<Vec3b>(i, j)[0] = static_cast<uchar>(color[0]); mask.at<Vec3b>(i, j)[1] = static_cast<uchar>(color[1]); mask.at<Vec3b>(i, j)[2] = static_cast<uchar>(color[2]); } } } } } // 清除第id个连通区 void clearArea(cv::Mat& mask, int id) { std::vector<std::vector<cv::Point> > contours; // 创建轮廓容器 std::vector<cv::Vec4i> hierarchy; cv::findContours(mask, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point()); if (!contours.empty() && !hierarchy.empty()) { if (id<contours.size()) { cv::Rect rect = cv::boundingRect(cv::Mat(contours[id])); for (int i = rect.y; i < rect.y + rect.height; i++) { uchar *output_data = mask.ptr<uchar>(i); for (int j = rect.x; j < rect.x + rect.width; j++) { // 将连通区的值置0 if (output_data[j] == 255) { output_data[j] = 0; } } } } } }
测试效果
图1 原图(北京奥运会)
图2 绘制奥运五环
图3 奥运五环
奥运五环完成啦,和图3是不是很接近,颜色可能有些许差异,只需要改变对应的RGB颜色即可。奥运五环的环上还有白边,可以通过重写同心圆函数DrawConcentricCircle来实现,我就不做啦,感兴趣的兄弟可以自行研究~
如果函数有什么可以改进完善的地方,非常欢迎大家指出,一同进步何乐而不为呢~
如果文章帮助到你了,可以点个赞让我知道,我会很快乐~加油!