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

opencv提取外部輪廓并在外部加矩形框

 更新時(shí)間:2020年03月21日 10:43:58   作者:修煉打怪的小烏龜  
這篇文章主要為大家詳細(xì)介紹了opencv提取外部輪廓并在外部加矩形框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

這段時(shí)間一直在用opencv搞圖像處理的問題,發(fā)現(xiàn)雖然可調(diào)用的函數(shù)多,但是直接找相應(yīng)代碼還是很困難,就行尋找連通域,并在連通域外側(cè)加框,對于習(xí)慣使用Mat矩形操作的我,真心感覺代碼少之又少,為防止以后自己還會用到,特在此記錄一下。

要對下面的圖像進(jìn)行字符的邊緣檢測。

程序中具體的步驟為:

(1)灰度化、二值化

(2)圖像膨脹

(3)檢測膨脹圖像的邊緣并叫外矩形框

實(shí)現(xiàn)代碼如下:

#include "stdafx.h"
#include "stdio.h"
#include "Base_process.h"
#include "opencv/cv.h"
#include "opencv/highgui.h"
#include <opencv2/opencv.hpp>
#include <tchar.h>
#include <iostream>
#include <fstream>
 
using namespace std;
using namespace cv;
 
void main()
{
 Mat src = imread("D:\\Recognize_Form_Project\\test_images\\0.jpg");//圖片路徑/*image180.jpg*/
 
 Mat gray_image;
 cvtColor(src, gray_image, CV_BGR2GRAY);
 imwrite("src.jpg", src);
 
 Mat binary_image;
 adaptiveThreshold(gray_image, binary_image, 255, CV_ADAPTIVE_THRESH_MEAN_C,
 CV_THRESH_BINARY_INV, 25, 10); ///局部自適應(yīng)二值化函數(shù)
 
 imwrite("erzhi.jpg", binary_image);
 
 //去噪
 Mat de_noise = binary_image.clone();
  //中值濾波
 
 medianBlur(binary_image, de_noise, 5);
 
 ///////////////////////// 膨脹 ////////////////////
 Mat dilate_img;
 Mat element = getStructuringElement(MORPH_RECT, Size(20, 20/*15, 15*/));
 dilate(de_noise, dilate_img,element);
 imwrite("dilate.jpg", dilate_img);
 
 
 //外部加框
 //檢測連通域,每一個(gè)連通域以一系列的點(diǎn)表示,F(xiàn)indContours方法只能得到第一個(gè)域
 vector<vector<Point>> contours;
 vector<Vec4i> hierarchy;
 findContours(dilate_img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//CV_RETR_EXTERNAL只檢測外部輪廓,可根據(jù)自身需求進(jìn)行調(diào)整
 
 Mat contoursImage(dilate_img.rows, dilate_img.cols, CV_8U, Scalar(255));
 int index = 0;
 for (; index >= 0; index = hierarchy[index][0]) {
 cv::Scalar color(rand() & 255, rand() & 255, rand() & 255);
 // for opencv 2
 // cv::drawContours(dstImage, contours, index, color, CV_FILLED, 8, hierarchy);//CV_FILLED所在位置表示輪廓線條粗細(xì)度,如果為負(fù)值(如thickness==cv_filled),繪制在輪廓內(nèi)部
 // for opencv 3
 //cv::drawContours(contoursImage, contours, index, color, cv::FILLED, 8, hierarchy);
 
 cv::drawContours(contoursImage, contours, index, Scalar(0), 1, 8, hierarchy);//描繪字符的外輪廓
 
 Rect rect = boundingRect(contours[index]);//檢測外輪廓
 rectangle(contoursImage, rect, Scalar(0,0,255), 3);//對外輪廓加矩形框
 }
 
 
 imwrite("zt.jpg", contoursImage);
 
 cout << "完成檢測";
 
 de_noise.release();
 element.release();
 dilate_img.release();
 binary_image.release();
 gray_image.release();
}

相應(yīng)的結(jié)果圖:

膨脹圖:

連通域檢測圖:

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

相關(guān)文章

最新評論

渝中区| 贵州省| 吉林省| 华蓥市| 卢龙县| 大名县| 玉屏| 古田县| 沅陵县| 兰溪市| 西乡县| 两当县| 漳州市| 泸溪县| 洞口县| 麻城市| 东至县| 高密市| 诸暨市| 库车县| 盈江县| 布拖县| 永福县| 上虞市| 泸水县| 罗田县| 洞头县| 广平县| 瑞昌市| 页游| 额敏县| 高州市| 阿荣旗| 东莞市| 浑源县| 宜城市| 三都| 宁阳县| 达孜县| 虹口区| 文昌市|