如何从原始图像中提取彩色区域

时间:2013-12-27 20:56:04

标签: matlab image-processing extraction image-segmentation

我在图像上应用srm算法来平滑每个对象的颜色,在图像中为了提取它,然后我调用函数 extractLabel 来在单独的帧中提取每个颜色区域。现在,这很好用,但问题是,当我想得到与exctracted有色区域匹配的原始部分时,我没有成功。

例如:这是原始图像

enter image description here

这是应用srm算法的结果:

enter image description here

提取结果:

enter image description here

现在,我如何从原始图像中获取这些部分??? 我是否使用正确的技术??

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

    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

1 个答案:

答案 0 :(得分: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

结果:

enter image description here