矩阵逻辑比较

时间:2014-07-23 14:13:10

标签: matlab image-processing matrix hsv

在matlab中,我在一个矩阵中有色调平面,在另一个矩阵(L)中有最常见的颜色(前5%的色调)。我想创建一个只有罕见颜色的二进制图像。

色调平面为253X320矩阵,L矩阵为6X1。

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; else
H(l,m) = 1; end end end end

这导致矩阵只有1秒。

2 个答案:

答案 0 :(得分:1)

正如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

但这是一种非常低效的方式。

答案 1 :(得分:0)

(我试图遵循你原来的想法)

h = 0*H;
for ii = 1:length(L)
    hh = H==L(ii);
    h = max(hh,h);
end
相关问题