How to annotate borders with different colors?

5 visualizaciones (últimos 30 días)
Ahmad Muzaffar Zafriy
Ahmad Muzaffar Zafriy el 13 de Dic. de 2022
Comentada: Image Analyst el 15 de Dic. de 2022
clear all;
close all;
clc;
a=im2bw(imread('taskD_tag1.tif'));
c=im2bw(imread('taskD_tag2.tif'));
L3 = rangefilt(a);
L4 = rangefilt(c);
montage({L3})
How to annotate border with different colors? (e.g. yellow and blue) Here are the images that are binarised and filter.

Respuestas (3)

Image Analyst
Image Analyst el 14 de Dic. de 2022
OK, here's a full demo for you.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'cards.JPEG';
folder = pwd;
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
%===================================================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%=========================================================================================================================
% Threshold the image to create a mask of the blue objects
[blueMask,maskedRGBImage] = createBlueMask(rgbImage);
% Get rid of particles less than 1000 pixels.
% props = regionprops(mask, 'Area'); % Measure areas of our initial mask to determine areas of LED and possible noise.
% allAreas = sort([props.Area])
% mask = bwareaopen(mask, 1000);
% Take the largest blob only.
blueMask = bwareafilt(blueMask, 1);
% Fill any holes in the blobs.
blueMask = imfill(blueMask, 'holes');
% Display the mask image.
subplot(2, 2, 2);
imshow(blueMask, []);
impixelinfo;
axis('on', 'image');
title('Blue Mask Image', 'FontSize', fontSize);
drawnow;
%=========================================================================================================================
% Threshold the image to create a mask of the blue objects
[yellowMask,maskedRGBImage] = createYellowMask(rgbImage);
% Get rid of particles less than 1000 pixels.
% props = regionprops(yellowMask, 'Area'); % Measure areas of our initial mask to determine areas of LED and possible noise.
% allAreas = sort([props.Area])
% mask = bwareaopen(yellowMask, 1000);
% Take the largest blob only.
yellowMask = bwareafilt(yellowMask, 1);
% Fill any holes in the blobs.
yellowMask = imfill(yellowMask, 'holes');
% Display the mask image.
subplot(2, 2, 3);
imshow(yellowMask, []);
impixelinfo;
axis('on', 'image');
title('Yellow Mask Image', 'FontSize', fontSize);
drawnow;
%=========================================================================================================================
% Get the boundaries
% 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); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% Here is where we actually get the boundaries for each blob.
blueBoundaries = bwboundaries(blueMask);
yellowBoundaries = bwboundaries(yellowMask);
% 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(blueBoundaries, 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
% Plot blue boundaries in red.
thisBoundary = blueBoundaries{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, 'r-', 'LineWidth', 2); % Plot boundary in red.
% Plot yellow boundaries in magenta.
thisBoundary = yellowBoundaries{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, 'm-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('Outlines, from bwboundaries()');
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
%=========================================================================================================================
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%=========================================================================================================================
function [BW,maskedRGBImage] = createBlueMask(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 14-Dec-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.453;
channel1Max = 0.817;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.338;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% 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
%======================================================================================================================
function [BW,maskedRGBImage] = createYellowMask(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 14-Dec-2022
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.066;
channel1Max = 0.346;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.113;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.808;
channel3Max = 1.000;
% 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

millercommamatt
millercommamatt el 13 de Dic. de 2022
This example from the Documentation includes a way to do this.
https://www.mathworks.com/help/images/correcting-nonuniform-illumination.html

Image Analyst
Image Analyst el 13 de Dic. de 2022
Here is a snippet to draw boundaries about your blobs in a binary image. You can change the color to whatever you want in the call to plot():
% 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.
imshow(originalImage); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% 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, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
fontSize = 15;
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
  11 comentarios
Ahmad Muzaffar Zafriy
Ahmad Muzaffar Zafriy el 15 de Dic. de 2022
How do you apply median filter to remove the noise?
Image Analyst
Image Analyst el 15 de Dic. de 2022
There is a function medfilt2 which has an example in the help documentation.
I'm also attaching some demos that use it.

Iniciar sesión para comentar.

Categorías

Más información sobre Computer Vision with Simulink en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by