Three methods of edge detection for Roberts,sobel,canny of Unity3d shader

Source: Internet
Author: User

In fact, the method is almost the same, that is, using two filters, processing two components respectively

Sobel operator

First, the Sobel operator.

The GX is the horizontal filter, the Gy is the vertical filter, the vertical filter is the horizontal filter rotates 90 degrees.
The filter is a 3x3 matrix that will be used as a planar convolution of the image.
If there is no edge then the two point color is very close, the filter returns a small value, otherwise you can determine the existence of the edge.
The current point is an intermediate point

The calculation is as follows:


The horizontal and vertical gray values of each pixel of the image are calculated by combining the following formula to calculate the gray size of the point.




This shdaer the G value as the color output

Roberts operator

The similarity of Roberts operator
The filter is a 2x2 matrix
Filters are as follows:

The point at the top left corner of the current point
The calculation is as follows:



Canny operator

The filter is a 2x2 matrix



Take Sobel as an example:
Look at the implementation of shader


In the Frag function


Float3 lum = FLOAT3 (0.2125,0.7154,0.0721);
Variables converted to luminance luminance values

Gets the point around the current point, and with the luminance dot product, to find the luminance value (black and white image)
float mc00 = dot (tex2d (_maintex, I.uv_maintex-fixed2 ()/_size). RGB, Lum);
float MC10 = dot (tex2d (_maintex, I.uv_maintex-fixed2 (0,1)/_size). RGB, Lum);
float MC20 = dot (tex2d (_maintex, I.uv_maintex-fixed2 ( -1,1)/_size). RGB, Lum);
。。。。。。。
Since the CG function Tex2dsize function (to get the image's long-width pixels) is not available in unity, I don't know what function to replace it with an external variable _size for ease of adjustment.
If there is any function to replace the Tex2dsize function you crossing must tell me.

The gray values of the GX horizontal and GY vertical are calculated according to the filter matrix.
float GX =-1 * mc00 + MC20 +-2 * mc01 + 2 * mc21-mc02 + mc22;

Float GY = mc00 + 2 * mc10 + mc20-mc02-2 * MC12-MC22;

G = sqrt (gx*gx+gy*gy);
Standard Grayscale formula
G = ABS (GX) +abs (GY);

Approximate grayscale formula

c = Length (Float2 (gx,gy));
The internal algorithm of length is the algorithm of gray-scale formula, Euclidean length

Float Length (Float3 v)
{
return sqrt (dot (v,v));
}

C is the final output

Let's look at the effect:

You can see that for this simple cartoon, three algorithms are very clear, Sobel and Roberts slightly better.

And look at some more complicated pictures.

The lines of the Roberts are very clear and some noise

Sobel is clearer than Roberts, and the noise is relatively small.

Canny can't see anymore, too much noise, unclear edge judgment.

In conclusion, the Sobel operator has the best effect.

The following gives the shader of Sobel:

Shader "Custom/sobel" {Properties {_maintex ("Maintex", 2D) = "White" {}_size ("Size", Range (1,2048)) = 256//size}subshade R {pass{tags{"Lightmode" = "forwardbase"}cull offcgprogram#pragma vertex vert#pragma fragment Frag#include " Unitycg.cginc "float _size;sampler2d _maintex;float4 _maintex_st;struct v2f {float4 pos:sv_position;float2 uv_MainTex: TEXCOORD0;}; v2f Vert (Appdata_full v) {v2f o;o.pos=mul (unity_matrix_mvp,v.vertex); O.uv_maintex = Transform_tex (v.texcoord,_ Maintex); return o;} FLOAT4 Frag (v2f i): color{float3 lum = FLOAT3 (0.2125,0.7154,0.0721);//Convert to luminance luminance value//To get the points around the current point//And with the luminance dot product, Find the luminance value (black and white) float mc00 = dot (tex2d (_maintex, I.uv_maintex-fixed2 ()/_size). RGB, Lum); float MC10 = dot (tex2d (_maintex, I . UV_MAINTEX-FIXED2 (0,1)/_size). RGB, Lum); float mc20 = dot (tex2d (_maintex, I.uv_maintex-fixed2 ( -1,1)/_size). RGB, Lum) ; Float mc01 = dot (tex2d (_maintex, I.uv_maintex-fixed2 (1,0)/_size). RGB, Lum); float MC11MC = dot (tex2d (_maintex, I.uv_mai Ntex). RGB, Lum), Float mc21 = dot (tex2d (_mAintex, I.uv_maintex-fixed2 ( -1,0)/_size). RGB, Lum); float MC02 = dot (tex2d (_maintex, I.uv_maintex-fixed2 (1,-1)/_size)  . RGB, Lum); float MC12 = dot (tex2d (_maintex, I.uv_maintex-fixed2 (0,-1)/_size). RGB, Lum); float mc22 = dot (tex2d (_maintex, I.uv_maintex-fixed2 ( -1,-1)/_size). RGB, Lum);//The gray value of GX horizontal and GY vertical according to the filter matrix float GX =-1 * mc00 + MC20 +-2 * mc01 + 2 * mc21- MC02 + mc22;float GY = mc00 + 2 * mc10 + mc20-mc02-2 * mc12-mc22;//float g = sqrt (gx*gx+gy*gy);//Standard grayscale formula float G = ABS (GX) +abs (GY);//approximate grayscale formula//float th = atan (GY/GX);//Grayscale direction float4 c = 0;//c = g>th?1:0;//c = g/th*2;c = Length (Float2 (gx,gy)); The internal algorithm of length is the algorithm of the gray-scale formula, and the Euclidean length return c;} endcg}//}}


-----------------------by wolf96

Three methods of edge detection for Roberts,sobel,canny of Unity3d shader

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.