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

OpenCV實(shí)現(xiàn)圖像輪廓檢測(cè)以及外接矩形

 更新時(shí)間:2020年07月21日 11:38:32   作者:lindamtd  
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)圖像輪廓檢測(cè)以及外接矩形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前兩篇博文分別介紹了圖像的邊緣檢測(cè)和輪廓檢測(cè),本文接著介紹圖像的輪廓檢測(cè)和輪廓外接矩形:

一、代碼部分:

// extract_contours.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
#include "stdafx.h"
#include<cv.h> 
#include<highgui.h> 
using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 //load src image
 string img_name="..\\image_norm\\71253.jpg";
 Mat image=imread(img_name); 
 imshow("src_image",image);
 cvWaitKey(0);
 //convert into gray image
 Mat gray(image.size(),CV_8U); 
 cvtColor(image,gray,CV_BGR2GRAY);
 imshow("gray",gray);
 cvWaitKey(0);
 //convert into bin image
 threshold(gray,gray,128,255,THRESH_BINARY);//轉(zhuǎn)換成2值圖像 
 imshow("binary",gray); 
 cvWaitKey(0);
 // Detecting contours 
 vector<vector<Point>> contours; //定義輪廓集合 
 vector<Vec4i> hierarchy; 
 findContours(gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//CV_RETR_EXTERNAL只檢測(cè)外部輪廓
 // draw black contours on white image 
 Mat result(gray.size(),CV_8U,Scalar(255)); 
 int index = 0; 
 for (; index >= 0; index = hierarchy[index][0]) //hierarchy[index][0]表示后一個(gè)輪廓
 { 
 Scalar color(rand() & 255, rand() & 255, rand() & 255); 
 drawContours(result, contours, index, Scalar(0), 1, 8, hierarchy);//描繪字符的外輪廓 
 Rect rect = boundingRect(contours[index]);//檢測(cè)外輪廓 
 rectangle(result, rect, Scalar(0,0,255), 3);//對(duì)外輪廓加矩形框 
 } 
 imshow("Contours on white image",result); 
 cvWaitKey(0);
 //draw contours on the original image 
 Mat original=imread(img_name); 
 int index_ori = 0; 
 for (; index_ori >= 0; index_ori = hierarchy[index_ori][0]) 
 {
 Scalar color(rand() & 255, rand() & 255, rand() & 255); 
 //描繪字符的外輪廓
 drawContours(original,contours,index_ori,Scalar(255),1,8, hierarchy); 
 Rect rect = boundingRect(contours[index_ori]);//檢測(cè)外輪廓
 //對(duì)外輪廓加加矩形框 
 rectangle(original, rect, Scalar(0,0,255), 3); 
 }
 //print contours info
 cout<<"The number of external contours:"<<contours.size()<<endl;
 imshow("Contours on original image",original); 
 waitKey(0);
 return 0;
}

二、程序運(yùn)行效果圖:

(1)源圖像:

(2)灰度圖像:

(3)二進(jìn)制圖像:

(4)輪廓在空白圖像上顯示:

(5)在原圖像上畫出圖像的輪廓以及外接矩形:

至此,圖像的輪廓檢測(cè)以及外接矩形已經(jīng)實(shí)現(xiàn),歡迎高人指正。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

长武县| 南木林县| 靖边县| 夏津县| 孝感市| 宁南县| 邵东县| 布拖县| 柳州市| 南召县| 漠河县| 泗洪县| 凌源市| 二连浩特市| 错那县| 涪陵区| 太康县| 大兴区| 连山| 靖远县| 古浪县| 内黄县| 晋中市| 聂拉木县| 漳州市| 清水河县| 兴义市| 汪清县| 关岭| 宜州市| 开原市| 嘉黎县| 扬中市| 梁河县| 汪清县| 新泰市| 伽师县| 塘沽区| 鞍山市| 旌德县| 雅江县|