How do I compute an equation that uses values from pixels in an image?
Afficher commentaires plus anciens

1 commentaire
DGM
le 7 Nov 2022
R is the mean of the absolute difference between each pixel of x and its 4-connected neighbors. That can be calculated with a simple sliding window filter using the given index ranges. Two nested loops.
rho can be directly calculated from R, no loops required.
Réponses (2)
Image Analyst
le 7 Nov 2022
Hint:
kernel1 = [0,-1,0; 0,1,0; 0,0,0]/4;
img1 = conv2(double(x), kernel1, 'same');
kernel2 = [0,0,0; 0,1,0; 0,-1,0]/4;
img2 = conv2(double(x), kernel2, 'same');
% etc for the remaining two
finalImage = abs(img1) + abs(img2) + .......
imshow(finalImage, [])
You should be able to complete it.
DGM
le 17 Nov 2022
I'm going to assume this homework is overdue now. I'm just going to dump this to get it out of my queue.
As I said, this can be calculated rather simply using loops.
X = imread('cameraman.tif');
fk = [0 1 0; 1 0 1; 0 1 0];
sz = size(X);
R = zeros(sz);
X = im2double(X);
fk = logical(fk);
for row = 2:sz(1)-1
for col = 2:sz(2)-1
sample = X(row-1:row+1,col-1:col+1);
sample = abs(X(row,col) - sample(fk));
R(row,col) = mean(sample(:));
end
end
rho = 1./(1+R);
montage({R rho})

Alternatively, you can use conv2() as IA suggests
X = imread('cameraman.tif');
X = im2double(X);
fk1 = [0,-1,0; 0,1,0; 0,0,0]/4;
R1 = conv2(X, fk1, 'same');
R2 = conv2(X, rot90(fk1,-1), 'same');
R3 = conv2(X, rot90(fk1,-2), 'same');
R4 = conv2(X, rot90(fk1,-3), 'same');
R = abs(R1) + abs(R2) + abs(R3) + abs(R4);
rho = 1./(1+R);
montage({R rho})
Or you could use nlfilter().
X = imread('cameraman.tif');
X = im2double(X);
R = nlfilter(X,[3 3],@windowfun);
rho = 1./(1+R);
montage({R rho})
function Rpx = windowfun(sample)
fk = logical([0 1 0; 1 0 1; 0 1 0]);
sample = abs(sample(2,2) - sample(fk));
Rpx = mean(sample(:));
end
Note that the latter two examples treat edges differently than the first. The first follows the given convention of avoiding the edges, whereas the latter two handle edges by array padding.
Catégories
En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!