# Remove unwanted object of image in MATLAB?

I am trying to remove the unwanted object. I have an image that includes a house and the trees. I am trying to remove the trees. I am using 'imsubtract' to remove small edge. But the result is not good. The tree on wall still exist. How can I remove the tree in front of the building?

This is my code:

clear;

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

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)


• Can you please try to clarify the question as, at the moment, what you are asking is a little bit unclear. Would you be interested in simply excluding discovered edges according to some criteria (probably based on implied geometry) or inpainting to try and remove or cover the tree "disturbance" from the original image? Can you please add more information on what you are trying to achieve? – A_A Jul 24 '16 at 21:11
• Yes. I mean I trying to get the edges of tree and remove it. I just want the building behind on my image. With my code, it can remove the tree which not overlap the building. but this case, the tree overlap the building, so I don't know how to remove complete the tree. – Nguyen Linh Jul 25 '16 at 3:35
• Thank you, can I please ask if by "building" you mean the set of windows or are you after other features as well that may not be visible in these examples? A geometry based solution will probably do the job fine here. – A_A Jul 25 '16 at 8:51
• I don't understand your question. I think it is so good if the window which behind tree will be showed after remove tree. But it is difficult problem (inpainting) . Did you see some problem like that? How can I do which my problem? how can I remove the trees? Thank you A_A. – Nguyen Linh Jul 25 '16 at 9:01

What you are asking is probably hopeless.

There are two quite difficult aspects:

• detecting the trees that you want to remove. Given the very poor contrast, this will be hard to achieve by automated means. You need to implement some image segmentation method, then select the regions corresponding to the trees.

• reconstructing the background image under the occluded parts. This is called image inpainting and requires sophisticated approaches.

All of this is highly technical and the standard tools in Matlab will probably not suffice.

If you just have one image to process, photoshopping will be much more efficient.

• Thank you for your answer. I have more than 50 images. All in a video. So you mean Matlab will probably not suffice? – Nguyen Linh Jul 25 '16 at 3:32
• @NguyenLinh: I see two options 1) appoint a good research team in a university; they will spend a month on it and/or start a thesis; 2) have it done by a skilled graphic designer, who will take a few days to do it by hand. – Yves Daoust Jul 25 '16 at 6:40
• Thank you. So with the image which tree is not overlap with the building. (I changed original image). Do you have some idea to solve it? I changed my code like this. But Ir image is binary image. How to convert to gray image? Thank you. BW = edge((I));figure;imshow(BW); 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) – Nguyen Linh Jul 25 '16 at 7:10

You can "filter" out the lines that are introduced because of the tree in the original image, by rejecting "any set of lines / line segments that doesn't compose a rectangular window". In this way and under specific conditions you might even be able to "fill in" the parts of windows that are currently only partially visible.

The technique is based on the detection of rectangular features via the use of the Hough Transform.

The Hough Transform is composed by integrating image profiles along different directions. Therefore, where the original image has dimensions of space (usually $x,y$), the transformation's output has dimensions of displacement, angle. The effect of this transform, over purely binary images, is to convert lines / line segments (characterised by some $x_1,y_1$,$x_2,y_2$ coordinates) to points (that is, "Detected a very high value of the integral along direction $\theta$). The output of the Hough Transform is still "usable" for grayscale images in the presence of noise and in-fact there, depending on the length of a "line" versus the amount of noise in the image, the Hough Transform can discover lines in very bad conditions.

Because of this property, various detectors have been established on the output of the Hough Transform to detect basic geometrical shapes such as circles and rectangles.

The Hough Transform is very relevant to this problem because it can operate over the output of the edge detector to detect the windows.

By creating the model of an "ideal window" and obtaining its Hough Transform, you can establish the set of points (in the Hough space) that correspond to the lines that make up the window.

Then, obtaining an image from the real world, you can mine its Hough space for geometric configurations of points that make up windows.

This step will detect whole windows. Having detected those, you can then mine the Hough Space for sets of points that seem to be making up a window, had there been a few more points at predicted spaces. You can then establish a heuristic to decide what would constitute a good partially discovered window. This could be a "prediction" step.

In this way, you would be rejecting all of these smaller incoherent lines that tend to make up the tree and foliage.

For more information on detecting rectangles using the Hough Transform, please see this publication. For more information on using the Hough Transform to detect shapes, try a search for "Hough Transform, shape detection" or see this link and this link.

Hope this helps.

• Mr A_A. Do you know any algorithm which can remove near object, by distance? Because the tree is near my camera. Maybe 50cm. And the building is so far, maybe 30m? – Nguyen Linh Jul 25 '16 at 19:19
• No, I am not aware of an algorithm that can "remove" the object by distance, especially from a single camera view. Is the proposed solution, based on edge detection, fit for your problem? – A_A Jul 26 '16 at 7:46