OpenCV 輪廓周圍繪制矩形框和圓形框的方法是什么,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
沒什么概念,就是給得出來的輪廓繪制周圍圖形,例如下圖給左側得出的輪廓去繪圖得到右側圖像:

函數作用:基于RDP算法實現,目的是減少多邊形輪廓點數
函數原型:
//減少多邊形輪廓點數 approxPolyDP( InputArray curve, // 一般是由圖像的輪廓點組成的點集 Mat(vector) OutputArray approxCurve, // 表示輸出的多邊形點集 double epsilon, // 主要表示輸出的精度,就是兩個輪廓點之間最大距離數,5,6,7,,8,,,, bool closed // 表示輸出的多邊形是否封閉 )
RDP算法介紹:
判斷起始點(當前點)與終點的距離是否小于 epsilon, 若小于,結束,不小于執行2
選取起始點(當前點)A的后兩個位置的點C,判斷它們之間的距離是否小于 epsilon, 若小于,點C與它們的中間點B都舍棄,若不小于,執行3
判斷A與B,B與C的距離,若有一者小于 epsilon,則點B舍棄,否則保留。然后點C作為起始點(當前點)重復 1 2 3 步驟,直到終點(這里得出的是一系列符合要求的點)
cv::boundingRect(InputArray points) 得到輪廓周圍最小矩形左上交點坐標和右下角點坐標,繪制一個矩形
cv::minAreaRect(InputArray points) 得到一個旋轉的矩形,返回旋轉矩形
// 得到輪廓周圍最小橢圓 cv::minEnclosingCircle( InputArray points, // 得到最小區域圓形 Point2f& center, // 圓心位置 輸出參數 float& radius // 圓的半徑 輸出參數 ) // 得到輪廓周圍最小橢圓 cv::fitEllipse(InputArray points)
將圖像變為二值圖像
發現輪廓,找到圖像輪廓
通過相關API在輪廓點上找到最小包含矩形和圓,旋轉矩形與橢圓
繪制周圍

#include <iostream>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
using namespace std;
using namespace cv;
Mat src, gray_src, drawImg;
int threshold_v = 170;
int threshold_max = 255;
const char* output_win = "rectangle-demo";
RNG rng(12345);
void Contours_Callback(int, void*);
int main(int argc, char** argv) {
src = imread("./test2.jpg");
if (!src.data) {
printf("could not load image...\n");
return -1;
}
cvtColor(src, gray_src, CV_BGR2GRAY);
blur(gray_src, gray_src, Size(3, 3), Point(-1, -1));
const char* source_win = "input image";
namedWindow(source_win, CV_WINDOW_AUTOSIZE);
namedWindow(output_win, CV_WINDOW_AUTOSIZE);
imshow(source_win, src);
createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback);
Contours_Callback(0, 0);
waitKey(0);
return 0;
}
void Contours_Callback(int, void*) {
Mat binary_output;
vector<vector<Point>> contours;
vector<Vec4i> hierachy;
threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY);
imshow("binary image", binary_output);
findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));
vector<vector<Point>> contours_ploy(contours.size());
vector<Rect> ploy_rects(contours.size());
vector<Point2f> ccs(contours.size());
vector<float> radius(contours.size());
vector<RotatedRect> minRects(contours.size());
vector<RotatedRect> myellipse(contours.size());
for (size_t i = 0; i < contours.size(); i++) {
approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);
ploy_rects[i] = boundingRect(contours_ploy[i]);
minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]);
if (contours_ploy[i].size() > 5) {
myellipse[i] = fitEllipse(contours_ploy[i]);
minRects[i] = minAreaRect(contours_ploy[i]);
}
// draw it
drawImg = Mat::zeros(src.size(), src.type());
Point2f pts[4];
for (size_t t = 0; t < contours.size(); t++) {
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//rectangle(drawImg, ploy_rects[t], color, 2, 8);
//circle(drawImg, ccs[t], radius[t], color, 2, 8);
if (contours_ploy[t].size() > 5) {
ellipse(drawImg, myellipse[t], color, 1, 8);
minRects[t].points(pts);
for (int r = 0; r < 4; r++) {
line(drawImg, pts[r], pts[(r + 1) % 4], color, 1, 8);
}
imshow(output_win, drawImg);
return;看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。