Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the method of recognizing and extracting rectangles in a drawing by opencv3.0

Shulou Source: shulou.com Published: 2022-06-02 06:34:21 09月20日 Update

This article will explain in detail what is the method of identifying and extracting rectangles in graphics by opencv3.0. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Use opencv to identify the rectangle in the picture.

The main problem encountered is that the shape inside the rectangle causes the contour not to be closed when identifying the contour.

1. Gaussian filtering is performed on the input grayscale images. Do gray histogram, extract threshold, do binarization processing 3. Extract image outline 4. Identify the rectangle in the picture 5. Extract the rectangle in the picture

1. Gaussian filtering of input grayscale images

Cv::Mat src = cv::imread ("F:\\ t13.bmp", CV_BGR2GRAY); cv::Mat hsv; GaussianBlur (src,hsv,cv::Size (5), 0)

two。 Do grayscale histogram, extract threshold, do binarization

Because the background of the given picture is black, the background color of the rectangle is gray, and some other shapes in the rectangle are white, you can refer to: when the outline is extracted, the outer outline of the rectangle is not closed. Therefore, we need to do the gray histogram of the whole picture, find the threshold and binarize it.

Deal with it. That is, if the pixel value (black) is less than the threshold, it is set to 0 (pure black); if the pixel value (gray and white) is greater than the threshold, set

Set to 255 (white)

/ / Quantize the gray scale to 30 levels int gbins = 16; int histSize [] = {gbins}

/ / gray scale varies from 0 to 256 float granges [] = {0256}

Const float* ranges [] = {granges}; cv::MatND hist

/ / we compute the histogram from the 0-th and 1-st channels int channels [] = {0}

/ / calculate hist calcHist (& hsv, 1, channels, cv::Mat ()

/ / do not use mask hist, 1, histSize, ranges, true

/ / the histogram is uniform false)

/ / find the max value of hist double maxVal=0

MinMaxLoc (hist, 0, & maxVal, 0,0); int scale = 20

Cv:Mat histImg

HistImg.create (500 gbinspowered scaleReclamation 8UC3)

/ / show gray scale of hist image for (int glossary g 1000 & & isContourConvex (Mat (approx) {double maxCosine = 0

For (int j = 2; j < 5; double cosine +) {double cosine = fabs (angle (approx [j% 4], approx [j-2], approx [j-1]))

MaxCosine = MAX (maxCosine, cosine);}

If (maxCosine < 0.3) squares.push_back (approx)

}}

About opencv3.0 recognition and extraction of rectangles in the drawing method is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Rectangle picture outline grayscale threshold processing white histogram black graphics method pixel content shape effect article more gray knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Microsoft Shulou Technology Xiaomi