我获得了灰度图像,我想从中创建渐变幅度图像。
我知道有一个名为pig -x local -4 nolog.conf
的函数,但我不知道如何修改幅度,以便按以下方式计算:
imgradient
答案 0 :(得分:0)
您必须实现自己的渐变功能,并将其应用于图像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
答案 1 :(得分:0)
我知道您希望使用sobel掩码进行空间滤波(根据您的等式)。所以,我建议你使用以下命令: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');
这比使用for循环更快。