In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.