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

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

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)站空間、營銷軟件、網(wǎng)站建設(shè)、梅河口網(wǎng)站維護(hù)、網(wǎng)站推廣。

一、代碼部分:

// 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)源圖像:

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

(2)灰度圖像:

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

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

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

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

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

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

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

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

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章題目:OpenCV實(shí)現(xiàn)圖像輪廓檢測(cè)以及外接矩形
路徑分享:http://www.muchs.cn/article32/jpjisc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站網(wǎng)站策劃、自適應(yīng)網(wǎng)站網(wǎng)站建設(shè)云服務(wù)器、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化