Fill a circle enclosed by it's circumference (of type double) with white.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Meghana Dinesh
el 14 de Mzo. de 2015
Editada: Meghana Dinesh
el 16 de Mzo. de 2015
I want to fill a specific region on an image with white (value 255). The specific region forms a circle. (whose co-ordinates of centre and circumference are calculated)
1. I used imfill but the co-ordinates of circle are of type double. Hence I got an error. When I convert the co-ordinates to to uint16, the co-ordinates don't form a contour (closed figure). Hence I can't use imfill.
2. I used roifill which accepts circumference co-ordinates of type double. It fills the values with a grey shade and not exactly 255.
Is there a way by which I can use roifill but fill it with white (instead of smoothing and filling with a shade of gray)? Or can I use the imfill command itself so that it accepts input of type double? Or is there anything else I can do?
I have attached the co-ordinates of circle's circumference which I have calculated and the centre of circle. I have also attached the image on which I am plotting the circle (if required). I want to make the pixels which lie inside this circle white and the others black.
0 comentarios
Respuesta aceptada
Image Analyst
el 14 de Mzo. de 2015
Try this:
clc;
workspace; % Make sure the workspace panel with all the variables is showing.
format long g;
format compact;
fontSize = 20;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%==========================================================================
baseFileName = 'sanofi.png';
folder = 'C:\Users\meghana\Documents\Temporary';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
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);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the original gray scale image.
subplot(2, 3, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 2);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Find mask
mask = grayImage > 100;
% Fill holes
mask = imfill(mask, 'holes');
% Erode it to make it smaller
mask = imerode(mask, true(90));
% Display the mask image.
subplot(2, 3, 3);
imshow(mask, []);
axis on;
title('Mask Image', 'FontSize', fontSize);
% Mask the image.
maskedImage = grayImage .* uint8(mask);
% Display the original gray scale image.
subplot(2, 3, 4);
imshow(maskedImage, []);
title('Masked Image', 'FontSize', fontSize);
% Mask again finding dark things
binaryImage = maskedImage > 0 & maskedImage < 175;
% Get rid of small specks.
binaryImage = bwareaopen(binaryImage, 100);
% Display the binary image.
subplot(2, 3, 5);
imshow(binaryImage, []);
title('Masked Image of Dark Things', 'FontSize', fontSize);
% Put it on a white background
finalImage = 255 * ones(rows, columns, 'uint8');
% Replace the mask pixels in the binary image of the final image
% with the pixels from the original image.
finalImage(binaryImage) = grayImage(binaryImage);
% Display the final image.
subplot(2, 3, 6);
imshow(finalImage, []);
title('Final Image of Logo', 'FontSize', fontSize);
1 comentario
Meghana Dinesh
el 16 de Mzo. de 2015
Editada: Meghana Dinesh
el 16 de Mzo. de 2015
Más respuestas (1)
Image Analyst
el 14 de Mzo. de 2015
Depends on exactly what you want to do. Do you want to fill the words and logo on the cap with the surrounding gray levels "smeared in"? Or is replacing those pixels with the average gray level of the entire cap good enough? And do you have the latest MATLAB R2015a - the roifill command has been improved. Prior to this version you'll have to dilate the binary image of the letters a little bit before you call roifill(), so that the outer perimeter of the mask actually lies on the white surround rather than on the letters, otherwise you'll get the gray like you said.
1 comentario
Ver también
Categorías
Más información sobre Convert Image Type en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!