Creating a white outline of an image

5 次查看(过去 30 天)
I have this image (below and attached) that I'd like to create a white-line outline around the gray portion, preferably one that I can overlay onto other images in the same manner without the gray portion seen. Any thoughts on how I could do that? Happy to clarify as needed.

采纳的回答

Image Analyst
Image Analyst 2021-3-19
Try this:
% Demo by Image Analyst, March, 2021.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'Example_03192021.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
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels == 3
grayImage = rgb2gray(grayImage);
end
% Display the test image full size.
subplot(2, 3, 1);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Original Gray Scale Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Get rid of huge white surrounding frame by cropping out the central portion.
grayImage = grayImage(41 : 466, 72 : 483);
%--------------------------------------------------------------------------------------------------------
% Get histogram
subplot(2, 3, 2);
counts = histcounts(grayImage);
bar(counts, 1);
grid on;
title('Log Histogram of gray scale image', 'FontSize', fontSize);
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
% Threshold image.
mask = grayImage > 2;
% Display mask image.
subplot(2, 3, 3);
imshow(mask, []);
axis('on', 'image');
title('Mask (Binary Image)', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 4);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 3, 4);
imshow(coloredLabelsImage);
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('%d Labeled Blobs', numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, grayImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
% Get the image of the perimeters.
mask = bwperim(mask);
% Display mask image.
subplot(2, 3, 5);
imshow(mask, []);
axis('on', 'image');
title('Final Boundaries', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
fprintf('Done running %s.m\n', mfilename);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by