我给出了灰度图像,我想从中创建梯度幅度图像。 我知道有一个叫imgradient
功能,但我不知道该怎样修改幅度的想法,以便它的计算方式如下:如何在MATLAB中创建梯度幅度图像
∇f ≈ abs(z7+ 2z8+ z9- z1- 2z2- z3) + abs(z3+ 2z6+ z9- z1- 2z4- z7)
我给出了灰度图像,我想从中创建梯度幅度图像。 我知道有一个叫imgradient
功能,但我不知道该怎样修改幅度的想法,以便它的计算方式如下:如何在MATLAB中创建梯度幅度图像
∇f ≈ abs(z7+ 2z8+ z9- z1- 2z2- z3) + abs(z3+ 2z6+ z9- z1- 2z4- z7)
你必须实现自己的梯度功能,并将其应用到图像I中的每个像素:
//example of a 4x5 image I
I = [1 3 2 6 5; 6 1 0 18 21; 13 12 13 14 11; 16 15 28 9 20]
G = I; //set your gradient matrix to be equal to I so that the sides have a value
//get the height and width of image I
[h, w] = size(I);
//loop through each pixel that is not on the side
for x=2:w-1
for y=2:h-1
//for each pixel:
z1 = I(y-1, x-1); //e.g z1 is the pixel on the upper left
z2 = I(y, x-1);
z3 = I(y+1, x-1);
z4 = I(y-1, x);
z6 = I(y+1, x);
z7 = I(y-1, x+1);
z8 = I(y, x+1);
z9 = I(y+1, x+1);
G(y,x) = abs(z7+ 2*z8+ z9- z1- 2*z2- z3) + abs(z3+ 2*z6+ z9- z1- 2*z4- z7);
end
end
它几乎可以完美地工作。我必须启动一个双矩阵来存储值。 wtf –
我得到你想要使用sobel mask(按照你的等式)进行空间滤波。所以,我建议你使用下面的命令:BW = edge(I,'Sobel');
,或者如果你想精确的输出按你所给出那就试试这个公式:
I = imread('cameraman.tif');
I = padarray(I,[1 1],'symmetric');
sobel_mask_x = [-1 -2 -1; 0 0 0; 1 2 1];
sobel_x = filter2(sobel_mask_x ,I);
sobel_mask_y = sobel_mask_x';
sobel_y = filter2(sobel_mask_y ,I);
sobel_responce = abs(sobel_x)+abs(sobel_y);
figure,imshow(sobel_responce,[]); title('Sobel gradient');
这是更快,而不是使用循环。
L1梯度大小,你不经常看到:) – mainactual
我不明白你发布的公式。另外为什么你需要“修改”imgradient所返回的数量? – Suever
我知道他们已经实现了几种方法,我只想知道如何实现自定义方法 –