在matlab中,怎么用函数处理图片?例如:找到某一颜色区域,并将其他区域设为其他颜色?
展开全部
function [r,p] = colorreplace(fn,c1,c2,cc)
% 将名为 fn 的图片的RGB颜色 c1 替换为 c2
% fn: 带路径的图片文件名
% c1,c2: RGB颜色 [r g b];
% cc: 容差,c1 颜色的容许漂移范围
% p: 源图片
% r: 替换后图片
p = imread(fn);
c10 = fix(c1-(1-cc)*c1);
c11 = c1+(1-cc)*c1;
c11(c11==0) = (1-cc)*255;
c11(c11>255) = 255;
r = p;
[m,n,k] = size(p);
if k==3
L = logical(ones(m,n));
for i = 1:k
a = r(:,:,i);
l = a>=c10(i) & a<=c11(i);
L = L & l;
end
for i = 1:k
a = r(:,:,i);
a(L) = c2(i);
r(:,:,i) = a;
end
end
% 调用并显示
fn = '000.jpg';
c1 = [255 0 0];
c2 = [0 0 0];
cc = 0.5;
[r,p] = colorreplace(fn,c1,c2,cc);
subplot(121); imshow(p);
subplot(122); imshow(r);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询