How do I compute an equation that uses values from pixels in an image?

조회 수: 1(최근 30일)
  댓글 수: 1
DGM 2022년 11월 7일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 11월 7일
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 2022년 11월 17일
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(:));
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(:));
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.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by