How to remove annotations in an image

34 views (last 30 days)
Warid Islam
Warid Islam on 19 Sep 2023
Answered: DGM on 20 Sep 2023
I want to remove annotations from the image below. I couldn't find any appropriate method. Any suggestions would be appreciated.

Answers (2)

Shubham
Shubham on 20 Sep 2023
Hi Warid,
To remove annotations from an image using MATLAB, you can try using the roipoly function. This function allows you to manually select and remove regions of interest (ROIs) from an image.
Here's an example of how you can use roipoly to remove annotations:
  1. Read the image using the imread function:
image = imread('path_to_image');
2. Display the image using the imshow function:
imshow(image);
3. Use the roipoly function to interactively select the regions you want to remove. Simply click around the annotations to create a closed polygonal shape. Press Enter when you are done selecting the ROI:
mask = roipoly(image);
4. Apply the mask to the image to remove the selected regions:
image(mask) = 0; % Set the selected regions to black
5. Display the modified image:
imshow(image);
By using roipoly, you can manually select and remove the annotations from the image.
I hope this helps!

DGM
DGM on 20 Sep 2023
Using manual selection is one way. I'm going to try to do this programmatically.
% this is an indexed-color image
[inpict CT] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1487477/image.png');
% find the index of the most common color (one-based indexing)
% note that since inpict is integer-class, its values are zero-based indices
[~,modeidx] = max(histcounts(inpict,'binmethod','integers'));
% find CT indices which are not gray (one-based)
CThsv = rgb2hsv(CT);
coloridx = find(CThsv(:,2) ~= 0);
% get initial masks
colormk = ismember(inpict,coloridx-1); % all color content
fgmk = inpict ~= modeidx-1; % all non-BG content
% find a region which should include the subject
S = regionprops(colormk,'area'); % use the colored text as a model
letterarea = max(vertcat(S.Area)); % to get an idea for the area of characters
roi = bwareaopen(fgmk & ~colormk,letterarea*3); % get rid of blobs the size of characters
roi = imdilate(roi,strel('disk',20)); % dilate
roi = bwareafilt(roi,1); % select the largest blob
roi = bwconvhull(roi); % make sure the interior is filled just in case
% show the extent of the ROI mask
F = imfuse(roi,ind2rgb(inpict,CT));
imshow(F,'border','tight')
% construct the output
% including colormk makes sure we get rid of all color content
% even if the dilated roi happens to overlap with the nearest annotations
outpict = inpict;
outpict(~roi | colormk) = modeidx-1; % zero-based indexing
% show the result
imshow(outpict,CT,'border','tight')

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!