首页 文章

Matlab获取特定像素的矢量

提问于
浏览
0

我对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命令并不难,但同时红色和绿色区域中的值不会太高 . 有没有我失踪的命令?因为英语不是我的母语,如果你给我一些合适的谷歌搜索关键字,它甚至可能有帮助;)在此先感谢

1 回答

  • 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);
    

    给这个:

    enter image description here

相关问题