How can I crop the image such that it will only contain the leaf and not have the noise part?
Asked
Active
Viewed 294 times
-3
![](../../users/profiles/3372061.webp)
Dev-iL
- 22,722
- 7
- 53
- 89
-
Do you have the image processing toolbox? Which MATLAB version are you using? There seems to be a discrepancy between the title of the topic and what you're asking. Do you want to have an image with a smaller size, or will the same size, only keeping the leaf, will do? Is the object you want to keep always the largest? – Dev-iL Mar 17 '17 at 07:00
1 Answers
4
If you have the Image Processing Toolbox, you can do something like this:
- Morphologically open the image (to remove salt and pepper noise).
- Convert the image to binary.
- Detect the largest contiguous area (presumably the object-of-interest).
- Set all other pixels to an "ignored value" (in this case,
255
). - "Crop" the original image by means of indexing the bounding box.
function out = q42849445
img = imread('https://i.stack.imgur.com/hTtqz.jpg');
bw = ~imopen(logical(img),strel('disk',10));
stats = regionprops(bw,'Area','SubarrayIdx');
[~,I] = max([stats.Area]);
for ind = setdiff(1:numel(stats),I)
img(stats(ind).SubarrayIdx{:}) = 255;
end
out = img(stats(I).SubarrayIdx{:});
The result (using imshow
):