In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "C++ Opencv imfill hole filling function how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "C++ Opencv imfill hole filling function how to use" this article.
The central idea of function realization
Binary graph
For a binary graph, this program finds the connected domain in which the pixel value is 0, saves all the pixels of the connected domain separately, and sets the pixel value of the qualified connected domain to 255.
The key to finding connected domains
For the realization of the hole filling function, that is, the process of setting 0 to 255, we need to look for four connections as the basic point.
Determination of seed point
To find a seed point is to find a point with a pixel value of 0 in a binary graph. we can directly traverse the pixels in the binary graph and determine the first point with a pixel value of 0 as the seed point of the first connected domain. At this time, some friends may be confused, because according to me, in the process of traversal, the nth pixel with a value of 0 is the seed point of the nth connected domain. Furthermore, in the whole traversal process, the number of pixels with a pixel value of 0 is the number of connected fields.
Right!
Of course, if we want to achieve this, then we need to set all the points found to 255 immediately during the search for each connected domain (it doesn't have to be 255 here, as long as it's not 0). When we traverse the binary graph again, all the pixels in the connected domain we have found have a value of 255, when we find a pixel with a pixel value of 0 again. This pixel must be the seed point of the next connected domain to be found.
The process of finding connected domains
First, create a four-connected vector. Vector upp; is used to store the top and bottom, front and back four points, and create a vector lenm. Used to store all the connected fields, as for why to create a three-dimensional Point array, you can first take a look at the notes on this three-dimensional array (the following formula is, the program also has corresponding notes), understand the meaning of each dimension, and then combined with the program, I think you should be able to understand, and briefly repeat, lenm.size () for the number of connected fields.
As shown in the figure; the function is the sum of the number of pixels in the I-th connected domain.
Condition setting
After the above search process, the result must be an all-white image, and we only want to fill the hole, so we need to remove the non-conforming connected domain. The so-called hole is actually a connected domain surrounded by points with a pixel value of 255, but there are some connected fields that are directly connected to the boundary of the image, which is not what we want, at least not what I want, (if you have different needs, the program is also easy to change). So, I need a marker bit, and when the pixel in the connected domain touches the boundary, give the connected field a mark. In the following program, I use vector Flag; to store marked points, where Flagg [I] represents the marked point of the first connected domain. In the program, after finding the seed point, first set the Flag [I] [0] = 1 of the I connected domain, and then Flag [I] [0] = 0 if there is a boundary point in the connected domain. (in the program, there seems to be a small BUG here, so I won't change it.)
Final assignment
Of all the connected domains found, Flag [I] [0] = = 1; {the connected domain where I belongs to [0dFlag.size ())} is a connected domain that meets the requirements, so all the pixels in lenm [I]; can be assigned a value of 255.
Don't say much and go straight to the function code.
Input binary graph
Returns a binary graph
Mat imfill (Mat cop) {Mat fcop; cop.copyTo (fcop); vector upp;// defines a set of four-connected points, which can be eight-connected upp.push_back (Point (- 1,0)); upp.push_back (Point (0,1)); upp.push_back (Point (0,1)); upp.push_back (Point (1,0)) / / upp.push_back (Point (1, 1)); / / upp.push_back (Point (- 1 int impixel_sum 1)); / / upp.push_back (Point (- 1, 1)); / / upp.push_back (Point (1 int impixel_sum 1)); vector lenm;// 3D point vector lenm.size () is the number of connected domains / * int impixel_sum = 0 For (int j = 0j0) {/ / ce.push_back (1); / / Flag.push_back (ce); / / vector ssinum For (int I = 0; I
< numim[nums].size(); i++) { for (int j = 0; j < upp.size(); j++) { int X = numim[nums][i].x + upp[j].x; int Y = numim[nums][i].y + upp[j].y; if (X >= 0 & Y > = 0 & X
< fcop.cols && Y < fcop.rows) { if (fcop.at(Y, X) == 0) { ssinum.push_back(Point(X, Y)); fcop.at(Y, X) = 255; } } if (X == 0 || Y == 0 || X == fcop.cols - 1 || Y == fcop.rows - 1) { Flag[nmss][0] = 0; } } } //Flag.push_back(ce); numim.push_back(ssinum); s1 = ssinum.size(); nums++; ssinum.clear(); /*ce.clear();*/ } nums = 0; lenm.push_back(numim); numim.clear(); nmss++; ce.clear(); } } } //imshow("1",fcop); Mat ffcop; cop.copyTo(ffcop); //ffcop = Mat::zeros(cop.size(),cop.type()); for (int i = 0; i < Flag.size(); i++) { if (Flag[i][0] == 1) { for (int j = 0; j < lenm[i].size(); j++) { for (int k = 0; k < lenm[i][j].size(); k++) { int X = lenm[i][j][k].x; int Y = lenm[i][j][k].y; ffcop.at(Y, X) = 255; } } } } return ffcop;}主函数代码#include#include#include"imfill.h"using namespace std;using namespace cv;Mat src;vector lunk;vector level;//RNG rn;int main(){ src = imread("5.jpg"); //imshow("万丈高楼第一步",src); Mat dst, gray, erzhi; blur(src, dst, Size(3, 3), Point(-1, -1)); //imshow("均值滤波",dst); cvtColor(dst, gray, COLOR_BGR2GRAY); //imshow("灰度图",gray); Canny(gray, erzhi, 100, 200, 3, false); //imshow("边缘检测",erzhi); Mat holef; holef = imfill(erzhi); imshow("填洞", holef); waitKey(0); return 0;}代码框截图Example picture
Running result
These are all the contents of this article entitled "how to use the imfill hole filling function of Opencv in C++". 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.