How to add border to the figure?

47 visualizzazioni (ultimi 30 giorni)
LL
LL il 16 Nov 2021
Commentato: Image Analyst il 19 Nov 2021
Hi to everyone,
i have these kind of figure and i would like to add border (maybe black) around the pink and green shape, how can i do?
Thanks a lot.
  3 Commenti
LL
LL il 16 Nov 2021
I would like to draw a tight border only around the pink and green shapes.
And later if it were possible to keep only the outlines and then make the inside become white.

Accedi per commentare.

Risposte (2)

Image Analyst
Image Analyst il 16 Nov 2021
Assuming you have digital images, get a mask of the region you want and then call bwperim().
  1 Commento
LL
LL il 18 Nov 2021
Modificato: LL il 18 Nov 2021
Could you please show me a demo because bwperim doesn't work, surely something wrong.
I tried masking the region but i lost all the information about the coordinates of the pixels.
%%%%Contorni
img=imread(string(FinalNameFigura), 'bmp');
hsvmap = rgb2hsv(img);
%figure; imshow(hsvmap);
sImage1 = hsvmap(:, :, 2);
mask1 = sImage1 > 0;
boundaries1 = bwboundaries(mask1);
blendedImage = uint8((double(img))/2);
figure('Color', 'w'); imshow(blendedImage);
for k = 1 : length(boundaries1)
thisBoundary = boundaries1{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
hold on;
plot(x, y, 'm-', 'LineWidth', 4);
%colorbar('Ticks',[1,2,3,4,5,6,7,8,9],...
% 'TickLabels',{'Idrossiapatite','Withlockite','Calcio Carbonato','Tessuto','Necrosi','Acciaio','Calcite','Calcio Ossalato', 'Non definito'})
end
The results are in the attachments.

Accedi per commentare.


Image Analyst
Image Analyst il 18 Nov 2021
@Lidia Frizzi, try this. Let me know if it's what you want:
% Demo by Image Analyst.
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 = 'cattura2.jpg';
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);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
caption = sprintf('Original RGB Image : "%s"\n%d rows by %d columns', baseFileName, rows, columns);
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';
%-----------------------------------------------------------------------------------------------------------------------------------
[mask, maskedRGBImage] = createMask(rgbImage);
mask = ~mask;
% Display the image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Find the size of the smallest blob. Hopefully this is just one tile.
props = regionprops(mask, 'Area')
allAreas = sort([props.Area]) % Smallest seems to be about 430 pixels for this image.
% For other images, that may have noise, get rid of blobs less than 300 pixels in area.
mask = bwareaopen(mask, 300);
% Now there are black lines running through the image. Let's fill them and see if they're all gone
% mask = imfill(mask, 'holes');
% Oops, they're not all gone. Let's do a closing to dilate the image then erode it
% back to its original size to cover up the black lines.
mask = imclose(mask, true(4));
% This may have created holes, so get rid of holes.
mask = imfill(mask, 4, 'holes');
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis('on', 'image');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(mask);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'b-', 'LineWidth', 4); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
msgbox('Done!');
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 18-Nov-2021
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2lab(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 4.839;
channel1Max = 100.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = -53.741;
channel2Max = 72.640;
% Define thresholds for channel 3 based on histogram settings
channel3Min = -53.889;
channel3Max = 79.061;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
  2 Commenti
LL
LL il 19 Nov 2021
I have a question, why the coordinates of the pixel change?
Is it possible to revert to the initial map size?
Thnx a lot.
Image Analyst
Image Analyst il 19 Nov 2021
I don't know what you mean. I just operated on the image you uploaded. If you want just the actual image and not the white frame/padding, tick marks, tick labels, colorbar, etc. then just use the image inside of the axes instead of the whole axes with junk you don't care about. But that's not what you gave me -- you gave me an image with the junk in it.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by