Recently by the image cutting the whole weeping, in this thank old friend Zhou Yang to me on the watershed algorithm guidance! Originally intended to wait for color image cutting has a satisfactory result and then write this article, but considering that this step is also considered a stage, so intends to do a series of image cutting blog, so first write this article.
That's a lot of nagging! First look at the effect:
The effect is general, there are a lot of cutting phenomenon, but the effect is much better than not using the filter before, cutting is a common problem of watershed algorithm. This blog post will continue to be solved.
This paper uses Java to implement the watershed algorithm based on its own active seed region, note that this paper is based on monochrome cutting, so the input image is first grayscale processing, this is relatively simple, not much, and therefore, for color images, there will be some deviation, this in perhaps blog I will solve. On the concept of watershed algorithm, please Baidu, this series how to achieve and achieve results.
The algorithm is broadly divided into four steps: Image preprocessing, seed region acquisition, soaking process (region growth), and combined processing of cutting results. In addition, the algorithm in the watershed process of the regional representation, the use of the data structure of the collection to represent the region block, the advantages of this is to simplify the growth process of the merger, about and check set, see me this blog: http://blog.csdn.net/abcd_d_/article/ details/40316455.
1. Image preprocessing process:
In this paper, the smooth operation of image (SIGMA=3.0) is performed by Ivigos filtering, and the smoothing effect is better than median filter, and the reader can experiment with other filtering. In addition, in order to simplify the operation, the Ivigos template can be approximated to two one-dimensional Gaussian template, that is, the left filtering process is approximate to the right filtering process.
The core code is:
/** * * Gaussian blur of the image: First, the Gaussian template matrix is computed using the fuzzy function, and then convolution is performed. * *@ Gaussian Blur: Gaussian Blur is an image filter that uses the normal distribution (Gaussian function) to calculate the fuzzy template and uses the template to do convolution operations with the original image to achieve the purpose of blurring the image. * In practical applications, when calculating the discrete approximation of a Gaussian function, pixels outside the approximate 3σ distance can be considered as ineffective, and the calculations of these pixels can be ignored. * Usually, the image processing program only needs to calculate the matrix can guarantee the correlation pixel influence. * * @param source * @param index indicates different sigma corresponding template * @return double[][] blurred image information matrix */public static double[][] Gausstran (do Uble[][] Source,int index) {int Height=source.length;int width=source[0].length;///save Gaussian filter result double[][] Result=new Double[height][width];d ouble[] template=gausstemplate1d.gettemplatex_y (index), int twh=template.length;///template dimension for (int i=0;i
The effect is (the image on the left is the original):
2, Seed Region selection:First, the gradient image is obtained: The Gaussian filter image uses the Sobel operator to obtain the gradient image.
Then, the seed region gets as well as tagged: in the gradient image, the grayscale value of the pixel points that are less than the pre-set threshold value of threshold is set to 0, while the grayscale values of the other pixels are equal to their gradient values. In this way, each pixel is marked and then started to grow in the region, using the same region to belong to a set of methods, the gray value of 0 of the region to classify mark (this information is stored in the Blockdata). , each black block belongs to a class (that is, a seed region).
3. Water immersion process (i.e. region growth process):Zhou Yang tell me. Method: Three-layer loop, the first layer is the gradient value from the threshold to the maximum value, the two-dimensional image of the second-level data traversal. Using the inference method of the eight domain, assuming that a point that is not marked as 0 (unknown) has only one point (seed) that is marked zero, then the point is merged into the seed region, assuming that there are more than two labeled points, and that they belong to different regions, then the point is marked as a ridge (contour line). For each gradient value of the outer loop, the stop condition for region growth: No new points are incorporated into an area. In fact, there is a four-layer cycle, which requires a layer to infer whether new points are incorporated into the seed region.
For example, the following:
It can be seen that the advantage of the watershed algorithm is that it can get a closed area, the general two-valued contour image is not available.
In addition, it is found that Gaussian filtering has a better effect on reducing cutting, but it can not eliminate cutting, so it is necessary to have a fourth step-region merging. I'll keep talking about this part of the blog.
Since not finished, so the code may appear to be more chaotic, for example, there is a map member variable, the actual does not make much sense, but considering the test, but also hope forgive!
Attach the source code for the first version of the program: http://download.csdn.net/detail/abcd_d_/8169869
Beginner, Java uses watershed algorithms for image cutting (I.)