Beginner, Java uses watershed algorithm for image segmentation (I.)

Source: Internet
Author: User
Tags image filter

Recently by the image division of the whole weeping, here to thank my old friend Zhou Yang to me about the watershed algorithm guidance! Originally intended to wait for the color image segmentation 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 segmentation blog, so first write this article.

That's a lot of nagging! First look at the effect:


The effect is general, there are many over-segmentation phenomenon, but the effect is much better before the filter is used, over-segmentation is the common problem of the watershed algorithm. This follow-up post will continue to be resolved.

This paper uses Java to implement the watershed algorithm based on the automatic seed region, note that this paper is based on monochrome segmentation, so the input image is first grayscale processing, this is relatively simple, not much, so, for color images, there will be some deviation, this in the subsequent blog post I will solve. For the concept of watershed algorithm, please Baidu, this series of how to achieve and achieve results.

The algorithm is broadly divided into four steps: Image preprocessing, seed region acquisition, immersion process (region growth), and combined processing of segmentation results. In addition, the algorithm in the watershed process of the region representation, using the data structure of the dataset representation of the region block, the advantage of this is to simplify the growth process of merging, about and check set, see my blog: http://blog.csdn.net/abcd_d_/article/ details/40316455.

1. Image preprocessing process:

In this paper, the smoothing operation of Image (SIGMA=3.0) is performed by two Gaussian filtering, and the smoothing effect is better than median filter, and the reader can experiment with other filters. 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 calculation of these pixels can be ignored. * Usually, the image processing program only needs to calculate the matrix to guarantee the relevant 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 preset 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 the region grows, using the same region as the one-and-check method to classify the area with a grayscale value of 0 (this information is stored in blockdata). , each black block belongs to a class (that is, a seed region).



3. Water immersion process (i.e. region growth process):

Refer to the method that Zhou Yang told me: 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 a eight-domain judgment method, if a point that is not marked as 0 (unknown) has only one point (seed) that is marked zero, then the point is merged into that seed region, and if there are more than two labeled points and belong to different regions, then the point is marked as a ridge (contour). For each gradient value of the outer loop, the stop condition for the area growth: no new points are incorporated into an area. This actually has a four-layer loop, because a layer is needed to determine if a new point is incorporated into the seed area.

As follows:


As you can see, the benefit of the watershed algorithm is that you can get a closed area, the general two-valued contour image is not available.

In addition, it is found that Gaussian filtering has an ideal effect on reducing over-segmentation, but it can not eliminate the segmentation, so it is necessary to have a fourth step-region merging. I will continue to talk about this part in the next blog post.

Because it is not finished, so the code may seem messy, such as the program has a map member variable, actually not much significance, but considering the test, but also hope forgive me!

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






Beginner, Java uses watershed algorithm for image segmentation (I.)

Related Article

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.