How do I remove the colored area on a part of the map?

6 views (last 30 days)
How can I remove the colored fill (make it white) outside the selected polygon?

Answers (1)

Image Analyst
Image Analyst on 5 Apr 2025
The whole image, even the gray stuff and tick mark labels? Or just the colored parts?
Try this:
% Demo by Image Analyst
% Initialization steps:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'heatmap.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
rows = 687
columns = 507
numberOfColorChannels = 3
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------
% Threshold on black to get lines.
[r, g, b] = imsplit(rgbImage);
mask = (r == 0) & (g == 0) & (b == 0);
% Display the mask image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------
% GET THE AREAS SO WE CAN SEE HOW BIG A LINE WE WANT TO KEEP.
% props = regionprops(mask, 'Area');
% allAreas = sort([props.Area])
% Only keep blobs bigger than 500 pixels.
mask = bwareaopen(mask, 500);
% Get the convex hull.
mask = bwconvhull(mask, "union");
% Display the standard deviation filtered image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Filled Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------
% Mask the image to black outside the mask
% Mask image by multiplying each channel by the mask.
maskedRgbImage = rgbImage .* cast(mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.
subplot(2, 2, 4);
imshow(maskedRgbImage, [])
impixelinfo;
axis('on', 'image');
caption = sprintf('Masked RGB Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;

Community Treasure Hunt

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

Start Hunting!