How can I delete part of a binary image?

Hello,
I have several images with different shape and sizes. I am trying to make a code to delete the wider part (lower region) in each of the images. So far, I have been able to change the original images to a binary image.
The part of the image I want to delete is the area within red box in the second picture (Image2.JPG) I attached, i.e. the part that appears like fingers.
Thank you.

 Accepted Answer

DGM
DGM on 25 Jul 2022
Edited: DGM on 25 Jul 2022
If all you want to do is put a black region over that part of the image:
myimage(310:end,150:300,:) = 0;
Otherwise, you'll have to describe how the region is to be replaced.
If this is supposed to be automated, you'll have to describe what image features can be relied upon and define the criteria used to locate the region that needs to be replaced.

4 Comments

Thank you very much for responding.
I was hoping that there might be a feature that would let me define a limit for the region I wanted to delete. This is due to the fact that I have many photos whose object have various lengths. Some parts that are necessary for the following image will be removed if I use "mask(310:end,150:300) = 0;".
Please is it possible to provide a conditional statement like:
  1. Identify first row
  2. Trace each row from the top to bottom
  3. Remove the rows that are 5 % higher than the first row?
  4. Return new image
The "first row" at the narrow end is 3px wide.
You have to define some objective criteria that can be used.
Since I've only seen one image, I have no idea what a safe assumption is to make. If this image is the only thing to go on, I might assume that one could use the narrowest spot as some kind of breakpoint.
This might be a start:
A = imread('image.jpeg');
A = rgb2gray(A);
sz = size(A);
mk = imbinarize(A);
mk = bwareaopen(mk,100);
mk = imfill(mk,'holes');
mk = imclose(mk,strel('disk',15));
imshow(mk)
w = sum(mk,1); % get width of object (vertical)
w = smooth(w,10); % smooth data
plot(w); hold on
% crop off everything outside the region of interest
% by replacing with NaN
[~,mxidx] = max(w);
w(1:mxidx+20) = NaN;
w(round(sz(2)/2):end) = NaN;
plot(w)
% find location of first local minimum within ROI
[~,mnidx] = findpeaks(-w);
mnidx = mnidx(1);
% use that index and the mask
% to find a box around the bad part
mk = imdilate(mk,strel('disk',15));
mk(:,mnidx:end) = 0;
mk(any(mk,2),any(mk,1)) = 1;
% create output
B = A;
B(mk) = 0;
% show output
figure
imshow(B)
% overlay original and censored copy for comparison
figure
imshow(imfuse(A,B))
Of course, if the narrowest part isn't always in that area, then this won't work. You might be able to use the example as a starting point though.
Thank you very much.
This is helpful.

Sign in to comment.

More Answers (1)

NN
NN on 25 Jul 2022
Hi,
As per my understanding, you want to delete some part of an image.
Please find some of the existing solutions to your problem :
  1. https://in.mathworks.com/matlabcentral/answers/293816-how-to-delete-some-part-of-an-image?s_tid=answers_rc1-1_p1_MLT
  2. https://in.mathworks.com/matlabcentral/answers/154845-how-to-delete-selective-part-of-image?s_tid=answers_rc1-2_p2_MLT
Hope this helps. Please let me know if the problem still persists.
Regards,
Narvik

Products

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!