How to use MATLAB to automatically remove unwant object in an image?

11,535

Solution 1

enter image description here

If this answer looks good enough to you then download the following code: http://www.mathworks.com/matlabcentral/fileexchange/32532 and try the following commands:

I = rgb2gray(imread('BO1NO.jpg'));
th = 0.35 * max(max(I));
[P, J] = regionGrowing(I, [240,390], th, 300, 'true', 'true', 'false');
figure;imshow(J)

Since you can see clear intensity difference between the bird and the wooded branch, its much more logical to try something like region growing approach than anything else. If I were you, the next thing I would try is some color image segmentation algorithm. Since I get a feel than when we convert from RGB-> Gray, we loose some useful information. Bcoz in gray version of the image, wooden branch has a somewhat same values as the bird (in few regions). So better work on the color image directly without converting to gray. Do NOT rush towards using any pattern classifier. It may solve your problem but it ain't smart thing to do if there are easier/cheaper solutions available. There are more than one ways to solve this problem strictly within the boundaries of Image Processing without intruding into Pattern Recognition/Machine Learning.

Solution 2

Try out this code and see if it is what you need:

I = imread('BO1NO.jpg');

% level = graythresh(I); BW = im2bw(I, level);figure;imshow(BW)

BW = im2bw(rgb2gray(I), 0.25);figure;imshow(BW);

% Remove largest connected component (i.e. bird) in the BW image and this gives the branch predominantly. So subtract the resultant image from the original BW image. The difference image is bird.

BW1 = BW;
CC = bwconncomp(BW);


numPixels = cellfun(@numel,CC.PixelIdxList);

[biggest,idx] = max(numPixels);

BW(CC.PixelIdxList{idx}) = 0;

figure, imshow(BW);

figure, imshow(BW1);

Ir = imsubtract(BW1,BW);

figure;imshow(Ir)

Check thresholding using otsu's method for threshold selection also.

Share:
11,535
Marco
Author by

Marco

Updated on June 04, 2022

Comments

  • Marco
    Marco almost 2 years

    I have already done some image segmentation in MATLAB. The attached picture is the result. My question is that how I can automatically remove the tree part (bottom part) from the image? In other words, I need to isolate the bird with the surrounding. I need to write up a method to do that because I have hundreds of those images. Thanks

    The image