国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

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

2020-05-23 13:25:42
字體:
來源:轉載
供稿:網友

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

要對下面的圖像進行字符的邊緣檢測。

opencv,輪廓,矩形框

程序中具體的步驟為:

(1)灰度化、二值化

(2)圖像膨脹

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

實現代碼如下:

#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); ///局部自適應二值化函數  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);   //外部加框 //檢測連通域,每一個連通域以一系列的點表示,FindContours方法只能得到第一個域 vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(dilate_img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//CV_RETR_EXTERNAL只檢測外部輪廓,可根據自身需求進行調整  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所在位置表示輪廓線條粗細度,如果為負值(如thickness==cv_filled),繪制在輪廓內部 // 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();}

相應的結果圖:

膨脹圖:

opencv,輪廓,矩形框

連通域檢測圖:

opencv,輪廓,矩形框

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 香格里拉县| 通许县| 宿迁市| 百色市| 余庆县| 南投县| 师宗县| 方城县| 长治市| 阿克苏市| 农安县| 龙门县| 中方县| 淮北市| 淳安县| 调兵山市| 深泽县| 准格尔旗| 牟定县| 电白县| 冕宁县| 左贡县| 汝阳县| 方城县| 南康市| 中江县| 谢通门县| 隆林| 南和县| 油尖旺区| 合作市| 蓬安县| 铜鼓县| 沙田区| 龙江县| 盐边县| 沧州市| 冀州市| 大余县| 杭锦旗| 琼中|