Question

I applied the srm Algorithm on images to smooth the colors for each object, in the image in order to extract it , then I called the function extractLabel to exctract each colored region in a separate frame . For now , this works fine , but the problem is that when I want to get the original part that matches the exctracted colored region , and I'm not succeeding in this .

e.g: this is the original image

enter image description here

and this is the result aftr applying srm algorithm:

enter image description here

Extraction results :

enter image description here

Now, How can I get these parts from the original image ??? and do am I using the right technique??

the code I applied to extract each colored region :

    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
Was it helpful?

Solution

I think the statistical region merging algorithm must have include the extraction part from the original image. You may need to improve your smr code to achieve that. Here I just proposed an extraction method based on the estimation of pixel values. The region with the closest average pixel values to the template will be extracted. I added something in your function, just showing you the example of yellow box extraction. Hopefully you can do the rest yourself.

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

The result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top