图像处理 - 实现Sobel滤波器 [英] Image Processing - Implementing Sobel Filter

查看:1749
本文介绍了图像处理 - 实现Sobel滤波器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务来实现Sobel滤波器,正如您所知,这是一种用于边缘检测的图像处理滤波器。
但不幸的是,我没有图像处理领域的经验,因为我甚至不知道图像在计算机中的表现方式。完全没有这方面的知识。



我已经阅读了一些论文和PDF,但他们关注的很多主题我觉得我可能不需要它来完成我的任务。 / p>

我很乐意知道您的建议,或者是否有任何特定的论文,PDF,教程或快速指南。



谢谢



编辑:



谢谢大家:)
我们的工作成果可以从这里下载。

解决方案

这很简单,您只需要使用Sobel滤镜对图像进行卷积。 Sobel滤波器有两个内核,x方向内核和y方向内核。 x方向内核检测水平边缘,y方向内核检测垂直边缘。



x方向内核(大小为3x3)

  float kernelx [3] [3] = {{-1,0,1},
{-2,0,2},
{-1,0,1}};

y-direction kernel

  float kernely [3] [3] = {{-1,-2,-1},
{0,0,0},
{1,2,1 }};

要计算像素(x,y)处的卷积,请定义一个大小等于内核的窗口大小(以x计算幅度的源代码和y中的幅度相同):

  double magX = 0.0; //这是你的幅度

for(int a = 0; a< 3; a ++)
{
for(int b = 0; b< 3; b ++ )
{
int xn = x + a - 1;
int yn = y + b - 1;

int index = xn + yn * width;
magX + = image [index] * kernelx [a] [b];
}
}

请注意,输入是灰度图像,它可以表示为1D的double数组(这只是一个技巧,因为坐标(x,y)中的像素值可以使用index = [x + y * width]来访问)



在给定magX和magY的情况下计算像素(x,y)的大小:



mag = sqrt(magX ^ 2 + magY ^ 2)


I've got a task to implement Sobel filter which is, as you know, an image processing filter for edge detection. But unfortunately, I've got no experience in image processing field, to the extent that I don't even know how images are represented in computer. Totally no knowledge in this field.

I've read some papers and PDFs but they focus on many topics which I feel that I may not need them for my task.

I'd be happy to know your suggestions or if there is any particular paper, PDF, tutorial or quick guide for this purpose.

Thank you

EDIT:

Thank you all :) The result of our work can be downloaded from here.

解决方案

It's pretty easy, you just need to convolve your image with a Sobel filter. A Sobel filter has two kernels, x-direction kernel and y-direction kernel. The x-direction kernel detects horizontal edges, and y-direction kernels detects vertical edges.

x-direction kernel (the size is 3x3)

float kernelx[3][3] = {{-1, 0, 1}, 
                       {-2, 0, 2}, 
                       {-1, 0, 1}};

y-direction kernel

float kernely[3][3] = {{-1, -2, -1}, 
                        {0,  0,  0}, 
                        {1,  2,  1}};

To calculate the convolution at pixel (x,y), define a window of size equal to the kernel size (source code to calculate magnitude in x and magnitude in y are identical):

double magX = 0.0; // this is your magnitude

for(int a = 0; a < 3; a++)
{
    for(int b = 0; b < 3; b++)
    {            
        int xn = x + a - 1;
        int yn = y + b - 1;

        int index = xn + yn * width;
        magX += image[index] * kernelx[a][b];
    }
 }

Note that the input is a grayscale image and it can be represented as 1D array of double (This is just a trick, since a pixel value in coordinate (x,y) can be accessed with index = [x + y * width] )

To calculate magnitude in pixel (x,y) given magX and magY :

mag = sqrt( magX^2 + magY^2 )

这篇关于图像处理 - 实现Sobel滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆