OpenCV 輪廓周圍繪制矩形框和圓形框的方法
輪廓周圍繪制介紹
沒(méi)什么概念,就是給得出來(lái)的輪廓繪制周圍圖形,例如下圖給左側(cè)得出的輪廓去繪圖得到右側(cè)圖像:

相關(guān)API
減少多邊形輪廓點(diǎn)數(shù):approxPolyDP
函數(shù)作用:基于RDP算法實(shí)現(xiàn),目的是減少多邊形輪廓點(diǎn)數(shù)
函數(shù)原型:
//減少多邊形輪廓點(diǎn)數(shù) approxPolyDP( InputArray curve, // 一般是由圖像的輪廓點(diǎn)組成的點(diǎn)集 Mat(vector) OutputArray approxCurve, // 表示輸出的多邊形點(diǎn)集 double epsilon, // 主要表示輸出的精度,就是兩個(gè)輪廓點(diǎn)之間最大距離數(shù),5,6,7,,8,,,, bool closed // 表示輸出的多邊形是否封閉 )
RDP算法介紹:
- 判斷起始點(diǎn)(當(dāng)前點(diǎn))與終點(diǎn)的距離是否小于 epsilon, 若小于,結(jié)束,不小于執(zhí)行2
- 選取起始點(diǎn)(當(dāng)前點(diǎn))A的后兩個(gè)位置的點(diǎn)C,判斷它們之間的距離是否小于 epsilon, 若小于,點(diǎn)C與它們的中間點(diǎn)B都舍棄,若不小于,執(zhí)行3
- 判斷A與B,B與C的距離,若有一者小于 epsilon,則點(diǎn)B舍棄,否則保留。然后點(diǎn)C作為起始點(diǎn)(當(dāng)前點(diǎn))重復(fù) 1 2 3 步驟,直到終點(diǎn)(這里得出的是一系列符合要求的點(diǎn))
輪廓周圍繪制矩形:boundingRect、minAreaRect
cv::boundingRect(InputArray points) 得到輪廓周圍最小矩形左上交點(diǎn)坐標(biāo)和右下角點(diǎn)坐標(biāo),繪制一個(gè)矩形
cv::minAreaRect(InputArray points) 得到一個(gè)旋轉(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ò)相關(guān)API在輪廓點(diǎn)上找到最小包含矩形和圓,旋轉(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Linux環(huán)境下并發(fā)編程中C語(yǔ)言fork()函數(shù)的使用
fork函數(shù)在Linux中可以創(chuàng)建子進(jìn)程即一個(gè)新的進(jìn)程,這里我們根據(jù)實(shí)例來(lái)淺談Linux環(huán)境下并發(fā)編程中C語(yǔ)言fork()函數(shù)的使用,需要的朋友可以參考下2016-06-06
Unity3D實(shí)現(xiàn)經(jīng)典小游戲Pacman
這篇文章主要介紹了基于Unity3D制作一做個(gè)經(jīng)典小游戲Pacman,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Unity3D有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12
C語(yǔ)言?超詳細(xì)梳理總結(jié)動(dòng)態(tài)內(nèi)存管理
動(dòng)態(tài)內(nèi)存是相對(duì)靜態(tài)內(nèi)存而言的。所謂動(dòng)態(tài)和靜態(tài)就是指內(nèi)存的分配方式。動(dòng)態(tài)內(nèi)存是指在堆上分配的內(nèi)存,而靜態(tài)內(nèi)存是指在棧上分配的內(nèi)存,本文帶你深入探究C語(yǔ)言中動(dòng)態(tài)內(nèi)存的管理2022-03-03
c++的glog與spdlog的性能對(duì)比測(cè)試分析
這篇文章主要為大家介紹了c++的glog與spdlog的性能對(duì)比測(cè)試分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
C語(yǔ)言不使用strcpy函數(shù)如何實(shí)現(xiàn)字符串復(fù)制功能
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言不使用strcpy函數(shù)如何實(shí)現(xiàn)字符串復(fù)制功能的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之圖書借閱系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之圖書借閱系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)
C/C++實(shí)現(xiàn)矩陣的轉(zhuǎn)置(示例代碼)需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10
C語(yǔ)言單循環(huán)鏈表的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言單循環(huán)鏈表的表示與實(shí)現(xiàn),對(duì)于學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法的朋友來(lái)說(shuō)很有參考借鑒價(jià)值,需要的朋友可以參考下2014-07-07

