OpenCV extract the outer contour and add a rectangle outside the frame

Source: Internet
Author: User
Tags rand scalar

This period of time has been using OPENCV image processing problems, found that although the function can be called more, but directly find the corresponding code is still very difficult, on the line to find the connected domain, and in the connection outside the box, for the habit of using the mat rectangle operation of me, I really feel the code is little less, in order to prevent later use, Special record here.


The edge of the character to be detected for the following image.




The specific steps in the program are:

(1) Grayscale, binary

(2) Image expansion

(3) Detect the edges of the swelling image and call the outer rectangle frame


The implementation code is as follows:

#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");//Picture path/*image180.jpg*/Mat Gray_ima
	Ge
	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);

	Local Adaptive binary function imwrite ("erzhi.jpg", binary_image);
        De-noising Mat de_noise = Binary_image.clone ();

	Median filter Medianblur (Binary_image, De_noise, 5);
	Expanded////////////////////Mat dilate_img;
	Mat element = Getstructuringelement (Morph_rect, Size (20, 20/*15, 15*/));
	Dilate (De_noise, dilate_img,element);


	Imwrite ("Dilate.jpg", dilate_img); Externally framed//detect connected domains, each connected domain with a series ofPoint indicates that the Findcontours method can only get the first domain vector<vector<point>> contours;
	Vector<vec4i> hierarchy; Findcontours (dilate_img, contours, hierarchy, cv_retr_external, cv_chain_approx_none);//cv_retr_external detects only external contours,
	Can be adjusted according to their own needs 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 () & 2
		55); For OPENCV 2//CV::d rawcontours (dstimage, contours, index, color, cv_filled, 8, hierarchy); Location of//cv_filled indicates contour line thickness , if negative (such as thickness==cv_filled), draw inside the contour//for OPENCV 3//CV::d rawcontours (contoursimage, contours, index, color, CV::FI

		lled, 8, hierarchy); CV::d rawcontours (contoursimage, contours, index, Scalar (0), 1, 8, hierarchy);//depict the outer outline of the character rect rect = Boundingrect (Contou 

	Rs[index]);//detect outer contour rectangle (contoursimage, rect, Scalar (0,0,255), 3);//external contour plus Rectangle} imwrite ("Zt.jpg", contoursimage);

	cout << "complete inspection"; De_noise.reLease ();
	Element.release ();
	Dilate_img.release ();
	Binary_image.release ();
Gray_image.release ();
 }

The corresponding result diagram:

Expansion chart:


Connected domain detection diagram:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.