使用MATLAB比较图像的颜色变化

时间:2014-11-13 13:37:23

标签: image matlab

我打算选择图像的一个点并比较其他点'使用此点的RGB值,然后更改类似的点'颜色,但我失败了。以下是我的代码:

function exchangecolor()
I1=imread('F:\28.jpg');   
I2=imread('F:\29.jpg'); 
[m1,n1,x1]=(size(I1));
[m2,n2,x2]=size(I2);  //[399,400,3]=size(I1)=size(I2)
r1=I1(200,200,1);  
r2=I2(200,200,1);

g1=I1(200,200,2);
g2=I2(200,200,2);

b1=I1(200,200,3);
b2=I2(200,200,3);

for i=1:m1
    for j=1:n1
        if abs(I1(i,j,1)-r1)<=10
            if abs(I1(i,j,2)-g1)<=10
                if abs(I1(i,j,3)-b1)<=10
                    I1(i,j,1)=r2;
                    I1(i,j,2)=g2;
                    I1(i,j,3)=b2;
                end
            end
        end
    end
  end
imwrite(I1,'F:\89.jpg','jpg');

1 个答案:

答案 0 :(得分:0)

我不确定代码的正确性,但是嵌套循环和if子句不是Matlab-ish的做法。

您是否考虑过这种方法:

rgb1 = I1(200,200,:);
rgb2 = I2(200,200,:);
sel = all( abs( bsxfun(@minus, I1, rgb1) ) <= 10, 3 );%//need care when subtracting `uint` type!
% replace
for ci=1:3
    tmp = I1(:,:,ci);
    tmp(sel) = rgb2(ci);
    I1(:,:,ci) = tmp;
end

我唯一能想到的可能是问题,I1可能属于uint8类型,由于某种原因,Matlab很难从{{1}中减去rgb1 }}。尝试将I1投射到I1并查看是否有帮助。


PS,
最好not to use i and j as variable names in Matlab

相关问题