最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenCV 輪廓周圍繪制矩形框和圓形框的方法

 更新時間:2022年01月27日 11:09:25   作者:流楚丶格念  
這篇文章主要介紹了OpenCV 輪廓周圍繪制矩形框和圓形框,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

輪廓周圍繪制介紹

沒什么概念,就是給得出來的輪廓繪制周圍圖形,例如下圖給左側(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)文章

最新評論

阿拉善右旗| 三台县| 循化| 宕昌县| 淮阳县| 黔南| 固始县| 马龙县| 阿拉善左旗| 溆浦县| 岑巩县| 吕梁市| 清镇市| 南投市| 康保县| 古丈县| 清丰县| 云南省| 丹阳市| 华坪县| 富民县| 乌拉特后旗| 锡林郭勒盟| 江孜县| 肥乡县| 龙游县| 文登市| 白山市| 徐闻县| 西和县| 河北省| 临泽县| 海安县| 临西县| 辽宁省| 花垣县| 辰溪县| 盈江县| 璧山县| 双城市| 河曲县|