【问题标题】:how to extract colored regions from original image如何从原始图像中提取彩色区域
【发布时间】:2013-12-27 20:56:04
【问题描述】:

我在图像上应用了 srm 算法来平滑图像中每个对象的颜色,以便提取它,然后我调用函数 extractLabel 来提取单独帧中的每个彩色区域。目前,这工作正常,但问题是当我想获得与提取的彩色区域匹配的原始部分时,我没有成功。

例如:这是原始图像

这是应用 srm 算法后的结果:

提取结果:

现在,我怎样才能从原始图像中获取这些部分??? 我是否使用了正确的技术?

我用于提取每个彩色区域的代码:

    function extractLabel(originalImage,I)

    % I is the srm result image 

[Iu,ia,iu] = unique(reshape(I,[],3),'rows');
counts = accumarray(iu,1);
[counts,sortinds] = sort(counts,'descend');

N = 10;
largestLabels = sortinds(1:N);

for i= 1:6
    mapi = reshape(iu == largestLabels(i),size(I,1),size(I,2));

    [L,Total] = bwlabel(mapi);
    Sdata=regionprops(L,'BoundingBox');

    for j=1:Total
    Img=imcrop(I,Sdata(j).BoundingBox);
    Name=strcat('Object Number:',num2str(j));
    end

    figure(1)
    subplot(2,3,i);
    imshow(Img, 'border', 'tight');

    figure(2)
    subplot(2,3,i);
    imshow(L==0, 'border', 'tight');

    im_name=strcat('image',num2str(i),'.png'); 

    imwrite(L==0,im_name)
end

【问题讨论】:

    标签: matlab image-processing extraction image-segmentation


    【解决方案1】:

    我认为statistical region merging algorithm 必须包含从原始图像中提取的部分。您可能需要改进您的 smr 代码来实现这一点。这里我只是提出了一种基于像素值估计的提取方法。将提取具有最接近模板的平均像素值的区域。我在你的函数中添加了一些东西,只是向你展示了黄框提取的例子。希望您可以自己完成其余的工作。

    for i= 2:2  % yellow box in your image
        mapi = reshape(iu == largestLabels(i),size(I,1),size(I,2));
    
        [L,Total] = bwlabel(mapi);
        Sdata=regionprops(L,'BoundingBox');
    
        for j=1:Total
            Img=imcrop(I,Sdata(j).BoundingBox);
            im1=double(Img(:,:,1));
            im2=double(Img(:,:,2));
            im3=double(Img(:,:,3));
            R=mode(im1(:)); % pixel values in the template
            G=mode(im2(:));
            B=mode(im3(:));
        end
    
        % same procedure on your original image
        bw=im2bw(originalImage);   % please note that sometimes a threshold is needed to include the items you are interested but with a dark intensity
        L= regionprops(bw,'BoundingBox');
        N=length(L);
        di = Inf;
        for t=1:N
            tmp=imcrop(originalImage,L(t).BoundingBox);
    
            tmp1=tmp(:,:,1);
            tmp2=tmp(:,:,2);
            tmp3=tmp(:,:,3);
            R1=mean(tmp1(:));
            G1=mean(tmp2(:));
            B1=mean(tmp3(:));
            if sqrt((R-R1)^2+(G-G1)^2+(B-B1)^2)<di
                di=sqrt((R-R1)^2+(G-G1)^2+(B-B1)^2);
                best=t;
            end
       end 
    
       tmp=imcrop(originalImage,L(best).BoundingBox);
    
       figure,imagesc(tmp) 
    end
    

    结果:

    【讨论】:

    • 谢谢你的回答,但是如果我想要蓝框我应该改变什么??
    • 我尝试了 i= 3:3 但没有奏效,我还需要改变什么吗??
    • 我认为你需要检查 bw=im2bw(originalImage);确保每次将图像转换为二进制图像后要提取的对象仍然存在。可以尝试一个适当的阈值来实现这一点。
    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 2016-01-01
    • 2022-01-18
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多