Beginner, java uses the watershed algorithm for image segmentation (I), java Watershed

Source: Internet
Author: User
Tags image filter

Beginner, java uses the watershed algorithm for image segmentation (I), java Watershed

I am grateful to Zhou Yang, an old friend, for giving me some advice on the watershed algorithm! This article was intended to be written again after a complete result of color image segmentation, but considering that this step is also a phase, we plan to create a series of blog posts on image segmentation, so write this article first.

So many times! First look at the effect:

 

The results are average and there are many over-segmentation phenomena, but they are much better than those before filtering is used. Over-segmentation is a common problem of the watershed algorithm. This blog post will continue to be addressed later.

This article uses java to implement the Watershed Algorithm Based on the automatic seed area. Note that this article is based on the monochrome split, so the input image is first grayscale, which is relatively simple and not much to mention; therefore, there will be some deviations in the color image, which will be solved in the subsequent blog. For more information about the concept of watershed algorithms, see Baidu.

The algorithm consists of four steps: Image preprocessing, Seed Region acquisition, immersion (region growth), and merging of segmentation results. In addition, the algorithm uses the data structure of the query set to represent the region block in the watershed process. The advantage of this is to simplify the merge processing during the growth process, see my blog: http://blog.csdn.net/abcd_d_/article/details/40366455.

1. Image preprocessing process:

In this paper, two-dimensional Gaussian filter is used to smooth the image (sigma = 3.0). It has been verified that the smoothing effect is better than the median filter. you can experiment with other filters by yourself. In addition, to simplify the operation, the two-dimensional Gaussian template can be approximately two one-dimensional Gaussian templates, that is, the filter process on the left side is approximately the filter process on the right side.

 

The core code is:

/***** Perform Gaussian blur on the image: first use the fuzzy function to calculate the Gaussian template matrix, and then perform convolution. ** @ Gaussian Blur: Gaussian Blur is an image filter. It uses a normal distribution (Gaussian function) to calculate a fuzzy template and uses this template to perform convolution with the original image, to blur the image. * In practical applications, when calculating the discrete approximation of Gaussian Functions, pixels out of the approximate 3 σ distance can be regarded as ineffective, and the calculation of these pixels can be ignored. * Generally, the image processing program only needs to calculate the Matrix to ensure the influence of relevant pixels. ** @ Param source * @ param index indicates the template corresponding to different sigma * @ return double [] [] blurred image information matrix */public static double [] [] gaussTran (double [] [] source, int index) {int height = source. length; int width = source [0]. length; // Save the result of Gaussian filtering. double [] [] result = new double [height] [width]; double [] template = GaussTemplate1D. gettemplateX_Y (index); int tWH = template. length; // template dimension for (int I = 0; I 

Result ):


2. Seed area selection:

First, obtain the gradient image: Gaussian filtered image uses the sobel operator to obtain the gradient image.

Then, obtain and mark the seed area: In the gradient image, set the gray value of the pixel whose gradient value is less than the preset THRESHOLD to 0, the gray value of other pixels is equal to the gradient value. In this way, after each pixel is marked, the growth of the region begins. The same region belongs to one and the query set method is used, mark areas with a gray value of 0 (this information is stored in blockData )., Each black block belongs to a class (that is, a seed area ).



3. Water immersion process (that is, the regional growth process ):

Refer to Zhou Yang's method: layer-3 circulation. The first layer is the gradient value from the threshold value to the maximum value, and the second layer is the traversal of two-dimensional image data. The eight-field judgment method is used. If a vertex not marked as 0 (unknown) has only one vertex marked as zero (SEED), the vertex is merged into the seed area, if more than two points are marked and belong to different regions, the point is marked as a ridge (contour line ). For each gradient value in the outer loop, the condition for stopping the region Growth: no new vertex is merged into a certain region. In this case, there is actually a four-layer loop, because a layer is required to determine whether a new point is merged into the seed area.

As follows:


We can see that the benefit of the watershed algorithm is that we can get closed areas one by one. This general binarization profile image is not available.

In addition, after comparison, it is found that Gaussian filtering plays an ideal role in reducing over-segmentation, but it cannot eliminate over-segmentation. Therefore, we need to take the fourth step-Regional merge. I will continue to talk about this part in subsequent blog posts.

The Code may seem messy because it has not been written yet. For example, the program has a map member variable, which makes little sense. But considering the test, I still hope you will forgive me!

Attaches the source code of the first version of the program: http://download.csdn.net/detail/abcd_d_/8169869






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.