Image processing-by-the-art edge detection and image processing-by-the-art Edge Detection

Source: Internet
Author: User
Tags float max hypot

Image processing-by-the-art edge detection and image processing-by-the-art Edge Detection

Image processing-edge moderation

I. History

In 1986, John F. Well developed an edge detection algorithm based on image gradient computing.

Detection Algorithm. At the same time, he also made a lot of contributions to the development of the subject of image edge extraction. Do

Although it has been around for many years, this algorithm is still one of the classic algorithms for image edge detection.

Ii. Canny edge detection algorithm

The classic-ECS Edge Detection Algorithms generally start with Gaussian blur and end EDGE connection based on dual-threshold.

. However, in practical engineering applications, considering that the input images are color images

The binarization output is displayed, so the complete implementation steps of the Canny edge detection algorithm are as follows:

1. convert a color image to a gray image

2. Perform Gaussian blur on the Image

3. Calculate the image gradient and calculate the edge amplitude and Angle Based on the gradient.

4. Non-maximum signal suppression (edge refinement)

5. Dual-threshold EDGE connection Processing

6. binarization image output results

3. Detailed steps and code implementation

1. convert a color image to a gray image

Formula for Converting color image RGB to gray: gray = R * 0.299 + G * 0.587 + B * 0.114

The code for converting each RGB pixel in a color image into a gray value is as follows:

int gray = (int) (0.299 * tr + 0.587 * tg + 0.114 * tb);

2. Perform Gaussian blur on the Image

First, determine the Gaussian variance and window size based on the input parameters. Here, we set the default Square

The difference window size is 16x16. The following code generates a Gaussian convolution algorithm based on the two parameters:

float kernel[][] = new float[gaussianKernelWidth][gaussianKernelWidth];for(int x=0; x<gaussianKernelWidth; x++){for(int y=0; y<gaussianKernelWidth; y++){kernel[x][y] = gaussian(x, y, gaussianKernelRadius);}}

After obtaining the Gaussian convolution operator, we can perform Gaussian convolution blur on the image.

For more information, see http://blog.csdn.net/jia20003/article/details/7210841implementation.

The image Gaussian convolution fuzzy code is as follows:

// Gaussian Blur-grayscale image int krr = (int) gaussianKernelRadius; for (int row = 0; row 

3. Calculate the gradient in the X and Y directions of the image, and calculate the image edge amplitude and Angle Based on the gradient.

The purpose of Gaussian Blur is to reduce image noise as a whole and to calculate the image gradient and edge more accurately.

Amplitude. You can select the Robot, Sobel, and Prewitt operators to calculate the image gradient. About

For more information about image gradient computing, see the following:

Http://blog.csdn.net/jia20003/article/details/7664777.

Here we use a simpler and clearer 2x2 operator. Its mathematical expression is as follows:


// Calculate the gradient-gradient, X and Y direction data = new float [width * height]; magnitudes = new float [width * height]; for (int row = 0; row 

After obtaining the edge amplitude and angle of each pixel of the image

4. Non-maximum signal Suppression

Signal suppression is often used in digital signal processing. Here the main purpose of Non-maximum signal suppression is to achieve edge

This step is used to further reduce edge pixels. The main idea of non-maximum signal suppression is assumed to be 3x3.

In the pixel area, the center pixel P (x, y) calculates the edge angle value angle Based on the calculation in the previous step.

The classification principles for four discrete values 0, 45, 90, and 135 are as follows:

The value range of the yellow area is 0 ~ 22.5 and 157.5 ~ 180

The value range of the green area is 22.5 ~ 67.5

The value range of the blue area is 67.5 ~ 112.5

The value range of the red area is 112.5 ~ 157.5

The value ranges of the preceding four discrete angles. After obtaining the angle, the angle of the center pixel is adjacent.

Two pixels. If the center pixel is smaller than any of them, the edge pixel is discarded; otherwise, the edge pixel is retained. I

A simple example is as follows:


// Non-maximum signal suppression algorithm 3x3Arrays. fill (magnitudes, 0); for (int row = 0; row 

1. Dual-threshold EDGE connection

After the non-maximum signal is squashed, if the output amplitude is directly displayed, a small number of non-edge pixels may be wrapped.

Therefore, you need to select the threshold value for selection. If the traditional method based on a threshold value is

The smaller threshold does not apply to non-edge filtering. If the selected threshold is too large, the real image edge is easily lost.

By using the Fuzzy threshold method

The dual-threshold value also plays a role in EDGE connection in applications. The dual-Threshold Selection and EDGE connection methods are assumed by two thresholds

One of them is high threshold TH, and the other is low threshold TL.

A. Discard any edge pixels smaller than TL

B. retain any edge pixels higher than TH

C. If any edge pixel value between TL and TH can be connected to a pixel greater

TH and all pixels on the edge are retained if the value of TL is greater than the minimum threshold. Otherwise, the edge is discarded. The code is implemented as follows:

Arrays.fill(data, 0);int offset = 0;for (int row = 0; row < height; row++) {for (int col = 0; col < width; col++) {if(magnitudes[offset] >= highThreshold && data[offset] == 0){edgeLink(col, row, offset, lowThreshold);}offset++;}}
The code for the recursive edge search method edgeLink is as follows:

private void edgeLink(int x1, int y1, int index, float threshold) {int x0 = (x1 == 0) ? x1 : x1 - 1;int x2 = (x1 == width - 1) ? x1 : x1 + 1;int y0 = y1 == 0 ? y1 : y1 - 1;int y2 = y1 == height -1 ? y1 : y1 + 1;data[index] = magnitudes[index];for (int x = x0; x <= x2; x++) {for (int y = y0; y <= y2; y++) {int i2 = x + y * width;if ((y != y1 || x != x1)&& data[i2] == 0 && magnitudes[i2] >= threshold) {edgeLink(x, y, i2, threshold);return;}}}}

6. The result is displayed in binarization mode.-If you don't want to talk about it, just click it. It's too simple.

// Binarization display for (int I = 0; I <inPixels. length; I ++) {int gray = clamp (int) data [I]); outPixels [I] = gray> 0? -1: 0xff000000 ;}
Final running result:


Iv. complete source code of the Canny algorithm

Package com. gloomyfish. filter. study; import java. awt. image. bufferedImage; import java. util. arrays; public class CannyEdgeFilter extends {private float gaussianKernelRadius = 2f; private int gaussianKernelWidth = 16; private float lowThreshold; private float highThreshold; // image width, heightprivate int width; private int height; private float [] data; private float [] magnitudes; publ Ic CannyEdgeFilter () {lowThreshold = 2.5f; highThreshold = 7.5f; gaussianKernelRadius = 2f; bandwidth = 16;} public float handle () {return gaussianKernelRadius;} public void handle) {this. gaussianKernelRadius = gaussianKernelRadius;} public int getGaussianKernelWidth () {return gaussianKernelWidth;} public void setGaussianKernelWidth (I Nt gaussianKernelWidth) {this. gaussianKernelWidth = gaussianKernelWidth;} public float getLowThreshold () {return lowThreshold;} public void setLowThreshold (float lowThreshold) {this. lowThreshold = lowThreshold;} public float getHighThreshold () {return highThreshold;} public void setHighThreshold (float highThreshold) {this. highThreshold = highThreshold;} @ Overridepublic BufferedImage filter (BufferedImag E src, BufferedImage dest) {width = src. getWidth (); height = src. getHeight (); if (dest = null) dest = createCompatibleDestImage (src, null); // grayscale int [] inPixels = new int [width * height]; int [] outPixels = new int [width * height]; getRGB (src, 0, 0, width, height, inPixels); int index = 0; for (int row = 0; row 

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.