In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how OpenCV removes a small area of connected domains, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Scene requirement
When using OpenCV, we often encounter this kind of scene: we need to clear the relatively small noise area in the target image and retain the main area information.
I would like to share a simple function to clear a small area of connected domain written by myself. The logic is relatively simple, leaving enough room for development and adjusting according to the needs of my own scene.
The principle can be simply summarized as follows: searching the outline of the connected area of the image-> traversing each connected area-> deleting the connected area with smaller area based on the threshold.
In terms of running speed, I have not tested this unit alone. If you are too slow after trying, please comment and let me know.
Anyway, I usually work to run the 2000-2000 image, and the time-consuming of this function is almost negligible.
C++ implementation code / * * @ brief Clear_MicroConnected_Areas clears small area connectivity function * @ param src input image matrix * @ param dst output result * @ minimum area clearance threshold set by return min_area * / void Clear_MicroConnected_Areas (cv::Mat src Cv::Mat & dst, double min_area) {/ / backup replication dst = src.clone () Std::vector contours; / / create silhouette container std::vector hierarchy / / the function to find the contour / / the fourth parameter CV_RETR_EXTERNAL, which means to find the outermost contour / / the fifth parameter CV_CHAIN_APPROX_NONE, which means to save all continuous contour points on the boundary of the object into the contours vector cv::findContours (src, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point ()). If (! contours.empty () & &! hierarchy.empty ()) {std::vector::const_iterator itc = contours.begin () / / traverse all contours while (itc! = contours.end ()) {/ / locate the location of the current contour cv::Rect rect = cv::boundingRect (cv::Mat (* itc)) / / contourArea function calculates the connected area double area = contourArea (* itc) / / if the area is less than the set threshold if (area < min_area) {/ / traverse all pixels for (int I = rect.y; I < rect.y + rect.height) where the outline is located. Uchar +) {uchar * output_data = dst.ptr (I); for (int j = rect.x; j < rect.x + rect.width) {/ / set the value of the connectivity area to 0 if (output_ data [j] = = 255) { Output_ data [j] = 0 }} itc++;} Test Code # include#include using namespace std;using namespace cv Void Clear_MicroConnected_Areas (cv::Mat src, cv::Mat & dst, double min_area); int main (void) {Mat A = Mat::zeros (500,500, CV_8UC1); circle (A, Point2i (100,100), 50,255,-1); circle (A, Point2i (300,400), 15,255,-1); Mat B; Clear_MicroConnected_Areas (A, B, 1000) Imshow ("before:A", A); imshow ("after:B", B); waitKey (0); system ("pause"); return 0;} void Clear_MicroConnected_Areas (cv::Mat src, cv::Mat & dst, double min_area) {/ / backup replication dst = src.clone (); std::vector contours / / create outline container std::vector hierarchy / / the function to find the contour / / the fourth parameter CV_RETR_EXTERNAL, which means to find the outermost contour / / the fifth parameter CV_CHAIN_APPROX_NONE, which means to save all continuous contour points on the boundary of the object into the contours vector cv::findContours (src, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point ()). If (! contours.empty () & &! hierarchy.empty ()) {std::vector::const_iterator itc = contours.begin () / / traverse all contours while (itc! = contours.end ()) {/ / locate the location of the current contour cv::Rect rect = cv::boundingRect (cv::Mat (* itc)) / / contourArea function calculates the connected area double area = contourArea (* itc) / / if the area is less than the set threshold if (area < min_area) {/ / traverse all pixels for (int I = rect.y; I < rect.y + rect.height) where the outline is located. Uchar +) {uchar * output_data = dst.ptr (I); for (int j = rect.x; j < rect.x + rect.width) {/ / set the value of the connectivity area to 0 if (output_ data [j] = = 255) { Output_ data [j] = 0 }} itc++;}
Test effect
The above is all the contents of the article "how to clear small connected domains by OpenCV". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.