OpenCV 輪廓周圍繪制矩形框和圓形框的方法
輪廓周圍繪制介紹
沒什么概念,就是給得出來的輪廓繪制周圍圖形,例如下圖給左側(cè)得出的輪廓去繪圖得到右側(cè)圖像:
相關(guān)API
減少多邊形輪廓點數(shù):approxPolyDP
函數(shù)作用:基于RDP算法實現(xiàn),目的是減少多邊形輪廓點數(shù)
函數(shù)原型:
//減少多邊形輪廓點數(shù) approxPolyDP( InputArray curve, // 一般是由圖像的輪廓點組成的點集 Mat(vector) OutputArray approxCurve, // 表示輸出的多邊形點集 double epsilon, // 主要表示輸出的精度,就是兩個輪廓點之間最大距離數(shù),5,6,7,,8,,,, bool closed // 表示輸出的多邊形是否封閉 )
RDP算法介紹:
- 判斷起始點(當(dāng)前點)與終點的距離是否小于 epsilon, 若小于,結(jié)束,不小于執(zhí)行2
- 選取起始點(當(dāng)前點)A的后兩個位置的點C,判斷它們之間的距離是否小于 epsilon, 若小于,點C與它們的中間點B都舍棄,若不小于,執(zhí)行3
- 判斷A與B,B與C的距離,若有一者小于 epsilon,則點B舍棄,否則保留。然后點C作為起始點(當(dāng)前點)重復(fù) 1 2 3 步驟,直到終點(這里得出的是一系列符合要求的點)
輪廓周圍繪制矩形:boundingRect、minAreaRect
cv::boundingRect(InputArray points) 得到輪廓周圍最小矩形左上交點坐標(biāo)和右下角點坐標(biāo),繪制一個矩形
cv::minAreaRect(InputArray points) 得到一個旋轉(zhuǎn)的矩形,返回旋轉(zhuǎn)矩形
輪廓周圍繪制圓和橢圓:minEnclosingCircle、fitEllipse
// 得到輪廓周圍最小橢圓 cv::minEnclosingCircle( InputArray points, // 得到最小區(qū)域圓形 Point2f& center, // 圓心位置 輸出參數(shù) float& radius // 圓的半徑 輸出參數(shù) ) // 得到輪廓周圍最小橢圓 cv::fitEllipse(InputArray points)
繪制步驟
- 將圖像變?yōu)槎祱D像
- 發(fā)現(xiàn)輪廓,找到圖像輪廓
- 通過相關(guān)API在輪廓點上找到最小包含矩形和圓,旋轉(zhuǎn)矩形與橢圓
- 繪制周圍
代碼示例
#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;
到此這篇關(guān)于OpenCV 輪廓周圍繪制矩形框和圓形框的文章就介紹到這了,更多相關(guān)OpenCV 繪制矩形框和圓形框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用VC++6.0的控制臺實現(xiàn)2048小游戲的程序
本文是作者拜讀劉地同學(xué)的《C語言控制臺版2048》之后感覺非常不錯,添加了注釋之后分享給大家的,方便更多的初學(xué)者閱讀學(xué)習(xí),有需要的小伙伴參考下。2015-03-03C語言SetConsoleCursorPosition函數(shù)使用方法
這篇文章介紹了C語言SetConsoleCursorPosition函數(shù)的使用方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12