Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does C++ use OpenCV to get the number of items in a picture?

Shulou Source: shulou.com Published: 2022-06-02 05:17:10 09月12日 Update

This article mainly explains "C++ how to use OpenCV to get the number of items in the picture", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "C++ how to use OpenCV to obtain the number of items in the picture" it!

Realize the effect

We see the picture above and use the OpenCV technology we have learned to calculate how many dates there are in this picture.

Realization idea

Binary image segmentation

Morphological operation (corrosion + expansion)

Distance transformation

Connected region calculation

Find the outline and calculate the number

Code demonstration

Let's create a new project named opencv--count, follow the configuration properties (VS2017 configure OpenCV generic properties), and then write # include and main methods in the source file.

First of all, the picture is loaded and displayed.

Binary image segmentation

First of all, resize the picture to make it easier for us to display and watch.

Then the image is blurred by Gaussian and converted to a grayscale image.

Then binarize the image.

Let's run it here to see the display effect.

In the image above, you can see the picture on the right. We have already done the binarization process.

Morphological operation

Next, we are going to carry out morphological operations. first of all, we can see the right picture above. There will be white dots in each of our dates, which is the white spots that we binarize after the light is hit, so we have to do a corrosion operation here to remove the white spots from the dates.

It looks like it's running.

You can see that all the white spots in the picture on the right have been disposed of

Next, we carry out the expansion operation, which is used to divide the black area. The expansion operation here can be done several times in a row to reduce the black area as much as possible. I have done five expansions in my parameters.

Take another look at the effect.

The picture on the left shows the effect of dividing all the black areas after five expansions.

Distance transformation

First of all, reverse the black and white of our binary image.

And then start the transformation of distance.

Then it is normalized and displayed.

The effect displayed is

Connected region calculation

First of all, let's build a picture of 8u and convert it.

Then we do the local threshold binarization according to the white points in the above picture.

The display effect is

You can see that the white connecting areas are basically separated on the right, but there are also some white spots in different places.

And then we deal with it twice by morphological expansion.

The areas where the white dots are disconnected are expanded and connected together.

Find and calculate contours

According to the figure above, we look for the outline based on that, the CV_RETR_EXTERNAL for the parameters, here we only look for the outermost outline.

Draw a new picture according to the found outline and output the result. The penultimate parameter in drawContours is-1, which represents that the outline is drawn and filled directly.

Running effect

Take a look at the output value again:

Going back to our source picture, it is indeed 12 dates.

Thank you for your reading, the above is the content of "how C++ uses OpenCV to get the number of items in the picture". After the study of this article, I believe you have a deeper understanding of how C++ uses OpenCV to obtain the number of items in the picture, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Pictures effects areas contours images white dots processing quantity items Clearing morphology next parameters white black transformation learning running content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple Redmi Shulou Information Huawei