OpenCV 閉合輪廓檢測

2022-05-14 19:54:12 字數 3490 閱讀 1380

這個好像是骨頭什麼的,但是要求輪廓閉合,於是對進行一下膨脹操作,再次檢測輪廓就好了。

// a closed contour.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

// findrotation-angle.cpp : 定義控制台應用程式的入口點。

//// findcontours.cpp : 定義控制台應用程式的入口點。

//#include "stdafx.h"

#include #include #include #include #include #include //#include "highlight"

//#include "highgui.h"

#pragma comment(lib,"opencv_core2410d.lib")

#pragma comment(lib,"opencv_highgui2410d.lib")

#pragma comment(lib,"opencv_imgproc2410d.lib")

#define pi 3.1415926

using namespace std;

using namespace cv;

int main()

// draw black contours on white image

cv::mat result(image.size(),cv_8u,cv::scalar(255));

cv::drawcontours(result,contours,

-1, // draw all contours

cv::scalar(0), // in black

2); // with a thickness of 2

cv::namedwindow("contours");

cv::imshow("contours",result);

// eliminate too short or too long contours

/* int cmin= 100; // minimum contour length

int cmax= 1000; // maximum contour length

std::vector>::const_iterator itc= contours.begin();

while (itc!=contours.end())

*/// draw contours on the original image

cv::mat original= cv::imread(image_name);

cv::drawcontours(original,contours,

-1, // draw all contours

cv::scalar(255,255,0), // in white

2); // with a thickness of 2

cv::namedwindow("contours on animals");

cv::imshow("contours on animals",original);

// let's now draw black contours on white image

result.setto(cv::scalar(255));

cv::drawcontours(result,contours,

-1, // draw all contours

cv::scalar(0), // in black

1); // with a thickness of 1

image= cv::imread("binary.bmp",0);

// testing the bounding box

std::vector>::const_iterator itc_rec= contours.begin();

while (itc_rec!=contours.end())

/*// testing the enclosing circle

float radius;

cv::point2f center;

cv::minenclosingcircle(cv::mat(contours[1]),center,radius);

cv::circle(result,cv::point(center),static_cast(radius),cv::scalar(0),2);

// cv::rotatedrect rrect= cv::fitellipse(cv::mat(contours[1]));

// cv::ellipse(result,rrect,cv::scalar(0),2);

std::vectorpoly;

std::cout << "polygon size: " << poly.size() << std::endl;

// iterate over each segment and draw it

std::vector::const_iterator itp= poly.begin();

while (itp!=(poly.end()-1))

// last point linked to first point

cv::line(result,*(poly.begin()),*(poly.end()-1),cv::scalar(20),2);

// testing the convex hull

std::vectorhull;

cv::convexhull(cv::mat(contours[3]),hull);

// iterate over each segment and draw it

std::vector::const_iterator it= hull.begin();

while (it!=(hull.end()-1))

// last point linked to first point

cv::line(result,*(hull.begin()),*(hull.end()-1),cv::scalar(20),2);

// testing the moments

// iterate over all contours

itc= contours.begin();

while (itc!=contours.end())

*/ cv::namedwindow("some shape descriptors");

cv::imshow("some shape descriptors",result);

cv::waitkey();

return 0;

}

實現效果:

OpenCV 閉合輪廓檢測

這個好像是骨頭什麼的,但是要求輪廓閉合,於是對進行一下膨脹操作,再次檢測輪廓就好了。a closed contour.cpp 定義控制台應用程式的入口點。include stdafx.h findrotation angle.cpp 定義控制台應用程式的入口點。findcontours.cpp 定義...

OpenCV輪廓檢測

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!opencv入門指南 第三篇canny邊緣檢測 中介紹了邊緣檢測,本篇介紹輪廓檢測,輪廓檢測的原理通俗的說就是掏空內部點,比如原圖中有3 3的矩形點。那麼就可以將中間的那一點去掉。在opencv中使用輪廓檢測是非常方便。直接使用cvfindcont...

OpenCV學習 輪廓檢測與重繪

出處 提取模式.cv retr external 只提取最外層的輪廓 cv retr list 提取所有輪廓,並且放置在 list 中 cv retr ccomp 提取所有輪廓,並且將其組織為兩層的 hierarchy 頂層為連通域的外圍邊界,次層為洞的內層邊界。cv retr tree 提取所有輪...