Realization of Sobel edge detection algorithm based on MATLAB

Source: Internet
Author: User
Tags square root

The edge of the image is where the gray value of the image is mutated, that is, the pixel value of the image changes very quickly in that part, such as a curve on the axis has just begun smooth suddenly a big turn, in the variation of the derivative is very large.

The Sobel operator is mainly used for edge detection, which is a discrete difference operator, which is used to calculate the approximate value of the image luminance function gray.

The edge is the set of pixels that have a sharp change in the grayscale of the surrounding pixels. The edge exists between the target, the background and the region, so the edge is the most important basis for image segmentation. Because the edge is the symbol of position, it is not sensitive to the change of gray scale, so the edge is also an important feature of image matching.

The core of Sobel edge detection is the convolution of pixel matrix, which is very important for digital image processing, and many image processing algorithms are done by convolution. The essence of the convolution operation is the weighted summation of the pixel values of the specified image area, which is calculated by multiplying each pixel value in the image area with each element of the convolution template, summing the result of the convolution, and calculating the result of the convolution operation.

The convolution formula for the matrix is as follows.

The convolution operation of the 3x3 window m and convolution template C is as follows.

The GX and GY are Sobel convolution factors, and the two factors and the original image are made as follows convolution.

Sobel convolution factor

Where a represents the original image.

Get the horizontal longitudinal gray value of each point in the image GX, Gy. Finally, the size of the gray scale is calculated by the following formula.

But usually in order to improve efficiency, using an approximate value of not open squares, although this will lose precision,

The implementation of the Sobel operator is divided into five steps:

(1) Calculates the product of the GX and the GY with each line of the template.

(2) The convolution of two 3x3 matrices will multiply each column of each row and then add.

(3) The GX and Gy after 3*3 template operation are obtained.

(4) The square root of the gx^2 + gy^2 or the absolute value of the GX and the Gy are calculated directly after summing.

(5) Set a threshold value, after the operation of the pixel value is greater than the threshold output is full 1, less than the threshold output is full 0.

Square root and absolute value functions

SQRT (x) calculates the square root
ABS (x) takes the absolute value of the value and the amplitude of the complex number

In the operation of image data, it is better to convert the image data into double type to avoid the loss of precision.

Sobel Edge detection MATLAB implementation

Sobel Edge Detect

1%RGB_YCBCR2 CLC;3 clear All;4 close all;5 6Rgb_data = Imread ('lena.jpg');%7 8R_data = Rgb_data (:,:,1);9G_data = Rgb_data (:,:,2);TenB_data = Rgb_data (:,:,3); One  A%imshow (rgb_data); -  -[Row,col, DIM] =size (rgb_data); the  -Y_data =zeros (row,col); -Cb_data =zeros (row,col); -Cr_data =zeros (row,col); +Gray_data =Rgb_data; -  +  forR =1: ROW A      forc =1: COL atY_data (r, c) =0.299*r_data (R, C) +0.587*g_data (R, c) +0.114*B_data (R, c); -Cb_data (r, c) =-0.172*r_data (R, C)-0.339*g_data (R, c) +0.511*b_data (R, c) + -; -Cr_data (r, c) =0.511*r_data (R, C)-0.428*g_data (R, c)-0.083*b_data (R, c) + -; - End - End -  inGray_data (:,:,1)=Y_data; -Gray_data (:,:,2)=Y_data; toGray_data (:,:,3)=Y_data; +  - Figure ; the imshow (gray_data); *  $%Median FilterPanax NotoginsengIMGN = Imnoise (Gray_data,'Salt & Pepper',0.02);  -  the Figure ; + imshow (IMGN); A  theMedian_img =Gray_data; +  forR =2: row-1 -      forc =2: col-1 $median3x3 =[IMGN (R-1, C-1) IMGN (R1, c) IMGN (R-1, c+1) $IMGN (r,c-1) IMGN (r,c) IMGN (r,c+1) -IMGN (r+1, C-1) IMGN (r+1, c) IMGN (r+1, c+1)]; -Sort1 = sort (median3x3,2,'Descend'); theSort2 = Sort ([Sort1 (1), Sort1 (4), Sort1 (7)],'Descend'); -Sort3 = Sort ([Sort1 (2), Sort1 (5), Sort1 (8)],'Descend');WuyiSort4 = Sort ([Sort1 (3), Sort1 (6), Sort1 (9)],'Descend'); theMid_num = Sort ([Sort2 (3), Sort3 (2), Sort4 (1)],'Descend'); -Median_img (r,c) = Mid_num (2); Wu End - End About  $ Figure ; - imshow (median_img); -  -%Sobel_edge_detect A  +Median_img =Double(median_img); theSobel_threshold = Max; -Sobel_img =zeros (row,col); $  forR =2: row-1 the      forc =2: col-1 thesobel_x = median_img (R-1, c+1) +2*median_img (r,c+1) + median_img (r+1, c+1)-Median_img (R1, C-1) -2*median_img (r,c-1)-Median_img (r+1, C-1); theSobel_y = median_img (R-1, C-1) +2*median_img (R1, c) + median_img (R-1, c+1)-Median_img (r+1, C-1) -2*median_img (r+1, c)-Median_img (r+1, c+1); theSobel_num = ABS (sobel_x) +ABS (sobel_y); -%sobel_num = sqrt (sobel_x^2+ sobel_y^2); in         if(Sobel_num >sobel_threshold) theSobel_img (r,c) =0; the         Else AboutSobel_img (r,c) =255; the End the End the End +  - Figure ; theImshow (SOBEL_IMG);
Picture effects after processing

Lena after median filtering

Lena after Sobel edge detection

In order to make the image edge more clear, can be on the basis of Sobel corrosion expansion treatment, corrosion expansion treatment, the next article continues to share.

Reprint Please specify source: Ninghechuan (Ning River)

Personal Subscription Number: Open Source FPGA

If you want to receive a personal blog post in time, you can scan the QR code on the left (or long press the QR code) to follow your personal subscription number

Know Id:ninghechuan

Micro Bo Id:ninghechuan

Original address: https://www.cnblogs.com/ninghechuan/p/9529936.html

Realization of Sobel edge detection algorithm based on MATLAB

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.