【问题标题】:Matlab get vector of specific pixelsMatlab获取特定像素的向量
【发布时间】:2014-12-26 16:38:25
【问题描述】:

我是 Matlab 的新手,在处理图像时遇到了问题。 我想在下图中获得一个特定颜色(蓝色)的像素: image 我当前的代码如下所示:

    function p = mark(image)
    %// display image I in figure
    imshow(image);

    %// first detect all blue values higher 60
    high_blue = find(image(:,:,3)>60);
    %cross elements is needed as an array later on, have to initialize it with 0
    cross_elements = 0;
    %// in this iteration the marked values are reduced to the ones
    %where the statement R+G < B+70 applies
    for i = 1:length(high_blue)
    %// my image has the size 1024*768, so to access the red/green/blue values
    %// i have to call the i-th, i+1024*768-th or i+1024*768*2-th position of the "array"
        if ((image(high_blue(i))+image(high_blue(i)+768*1024))<...
                image(high_blue(i)+2*768*1024)+70)
            %add it to the array
            cross_elements(end+1) = high_blue(i);
        end
    end
    %// delete the zero element, it was only needed as a filler
    cross_elements = cross_elements(cross_elements~=0);

    high_vector = zeros(length(cross_elements),2);
    for i = 1:length(cross_elements)
            high_vector(i,1) = ceil(cross_elements(i)/768);
            high_vector(i,2) = mod(cross_elements(i), 768);
    end

    black = zeros(768 ,1024);
    for i = 1:length(high_vector)
        black(high_vector(i,2), high_vector(i,1)) = 1;
    end
    cc = bwconncomp(black);
    a = regionprops(cc, 'Centroid');
    p = cat(1, a.Centroid);
    %// considering the detection of the crosses:
    %// RGB with B>100, R+G < 100 for B<150
    %// consider detection in HSV?
    %// close the figure
    %// find(I(:,:,3)>150)

    close;
end

但显然它没有针对 Matlab 进行优化。 所以我想知道是否有办法搜索具有特定值的像素, 其中蓝色值大于 60(使用 find 命令不难, 但同时红色和绿色区域的值不会太高。 有没有我遗漏的命令? 由于英语不是我的母语,如果你给我一些合适的关键词进行谷歌搜索,它甚至可能会有所帮助;) 提前致谢

【问题讨论】:

    标签: matlab image-processing


    【解决方案1】:

    根据你在代码末尾的问题,你可以在一行中得到你想要的:

    NewImage = OldImage(:,:,1) < SomeValue & OldImage(:,:,2) < SomeValue & OldImage(:,:,3) > 60;
    imshow(NewImage);
    

    例如,当您看到使用逻辑运算符为每个通道提供限制时,您当然可以自定义(例如,使用 | 作为逻辑 OR)。这是你想要的?根据您的代码,您似乎正在寻找图像中的特定区域,例如十字架或硬币,是这样吗?如果我给你的代码完全偏离轨道,请提供更多细节:)

    简单示例:

    A = imread('peppers.png');
    B = A(:,:,3)>60 & A(:,:,2)<150 & A(:,:,1) < 100;
    figure;
    subplot(1,2,1);
    imshow(A);
    subplot(1,2,2)
    imshow(B);
    

    给出这个:

    【讨论】:

    • 本可以发誓我在此过程中尝试过类似的方法,但仍然非常感谢你 :) 它工作得很好
    • 哈哈是的,我有时也会遇到这种情况!很高兴它奏效了! :)
    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多