1
我已經生成了一個函數來對圖像應用分段線性變換。基於這個功能,我不確定它是否正確執行。有時整個圖像顯示爲白色,所有像素列爲255.其他時間顯示輸出圖像,但結果不同。由於結果不同,我相信我的功能存在問題。該函數的代碼是在這裏:MATLAB分段線性變換函數
function T = piecewise_transform(f,n,r1,s1,r2,s2,r3,s3)
% breakpoints: (0,0),(r1,s2),(r2,s2),(r3,s3),((L-1),(L-1))
% to get alpha, beta, etc, don't suppress echoing (remove ;)
% find the parameters of each line
L = 2^n;
% s = a*r+b
a1 = ((s1-0)/(r1-0));
b1 = ((s1/r1));
a2 = ((s2-s1)/(r2-r1));
b2 = ((s1*r2)-(s2*r1)/(r2-r1));
a3 = ((s3-s2)/(r3-r2));
b3 = (((s3-s2)/(r3-r2))*(s2/r2));
a4 = (((L-1)-s3)/((L-1)-r3));
b4 = (((L-1)-s3)/((L-1)-r3)*(s3/r3));
% put all parameters in two vectors
A = [a1,a2,a3,a4]
B = [b1,b2,b3,b4]
[row,col] = size(f);
T = f;
for i = 1:row
for j=1:col
% find which line to use
%line 1
if f(i,j) < r1
flag = 1;
end
% line 2
if f(i,j) > r1 && f(i,j)<r2
flag = 2;
end
% line 3
if f(i,j) > r2 && f(i,j)<r3
flag = 3;
end
% line 4
if f(i,j) > r3 && f(i,j)<(L-1)
flag = 4;
end
T(i,j) = (A(flag)*f(i,j) + B(flag));
end
end
T = uint8(T);
imshow(T)
下面是我的一些測試結果:
根據我的代碼,爲什麼我會收到不同的結果?