Matlab Martix逻辑比较
在matlab中,我在一个矩阵中有色调平面,在另一个矩阵(L)中有最常见的颜色(色调的前5%)。我想创建一个只有稀有颜色的二值图像 色调平面为253X320矩阵,L矩阵为6X1 l=1时的Matlab Martix逻辑比较,matlab,image-processing,matrix,hsv,Matlab,Image Processing,Matrix,Hsv,在matlab中,我在一个矩阵中有色调平面,在另一个矩阵(L)中有最常见的颜色(色调的前5%)。我想创建一个只有稀有颜色的二值图像 色调平面为253X320矩阵,L矩阵为6X1 l=1时的:尺寸(HuePlane,1) m=1时:尺寸(HuePlane,2) 对于n=1:尺寸(L,1) 如果L(n,1)=HuePlane(L,m) H(l,m)=0; 否则 H(l,m)=1; 结束 结束 结束 结束 这导致矩阵仅为1s。如Daniel所述,使用ismember是最佳解决方案,您应该使用它: H
:尺寸(HuePlane,1)
m=1时:尺寸(HuePlane,2)
对于n=1:尺寸(L,1)
如果L(n,1)=HuePlane(L,m)
H(l,m)=0;
否则
H(l,m)=1;
结束
结束
结束
结束
这导致矩阵仅为1s。如Daniel所述,使用
ismember
是最佳解决方案,您应该使用它:
H = ~ismember(HuePlane, L)
然而,我想我应该告诉你在循环解决方案中哪里出了问题。基本上,您总是按顺序将HuePlane
中的每个颜色与L
中的每个元素进行比较,这意味着您只存储上次比较的结果。换句话说,您只是在检查L(end)
。这就是我认为你想要做的:
H = ones(size(HuePlane)); %// This pre-allocation is essential in Matlab! Your code will be terribly inefficient if you don't do it.
for l = 1 : size(HuePlane,1)
for m = 1 : size(HuePlane,2)
for n = 1:size(L,1)
if L(n,1) == HuePlane(l,m)
H(l,m) = 0;
break; %// No point checking against the rest of L once we've found a match!
end
end
end
end
但这是一种非常低效的方法。(我正试图遵循您的原始想法)
看看
ismember
,它完全实现了您想要的功能。
h = 0*H;
for ii = 1:length(L)
hh = H==L(ii);
h = max(hh,h);
end