In a binary image how do I make set the values of pixels inside a white blob of a certain radius to zero ?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Abhishek Deshpande
am 25 Feb. 2023
Kommentiert: Abhishek Deshpande
am 15 Mär. 2023
I have been doing a project on diabetic retinopathy detection in matlab using image processing.I have been able to figure out most of the things but there is one small issue. There is a circular portion highlighted in the above fundus image( using a white line around the orange part, and an orange line in the binary image) . The same part appears as a white BLOB in the output image.However that BLOB is undesirableand needs to be removed; values of the pixels inside the radius of that blob need to be set to zero .I am unable to figure out how I can remove that. Kindly help me with the same.
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 11 Mär. 2023
@Abhishek Deshpande try this:
% Demo by Image Analyst
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;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'fundus.jpeg';
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);
% Get 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(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale 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(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get mask
mask = logical(grayImage);
% Get rid of top 5 rows which are stripes for some weird reason.
mask(1:5, :) = false;
%--------------------------------------------------------------------------------------------------------
% Merge together blobs with a morphological closing.
se = strel('disk', 5, 0);
mask2 = imclose(mask, se);
% Fill holes
mask2 = imfill(mask2, 'holes');
% imclose makes the mask kind of sharp. Smooth it out by blurring and thresholding.
mask2 = conv2(double(mask2), ones(5)/25, 'same') > 0.5;
subplot(2, 2, 2);
imshow(mask2);
impixelinfo;
axis('on', 'image');
title('Mask Image', '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, 3);
imshow(grayImage); % 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(mask2);
% 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.
2 Kommentare
Weitere Antworten (2)
Shubh Dhyani
am 2 Mär. 2023
Hi Abhishek,
I understand that you want to remove undesirable blobs in your image.
To remove the circular blob in your image, you can use the following steps:
1. Find the center and radius of the circular blob. You can use the “imfindcircles” function to detect the circular blob and obtain its center and radius. Alternatively, if you already have the white line around the orange part, you can use the “regionprops” function to obtain the centroid and the major axis length of the corresponding region. You can refer these links for the functions:
2. Create a binary mask with the same size as the original image, with ones everywhere except inside the circular region. You can use the “meshgrid” function to create a grid of coordinates, compute the distance from each coordinate to the center of the circular region using the sqrt function, and set the values inside the radius of the circular region to zero. You can refer this link for the “meshgrid” function :
Here’s some example code that demonstrates this approach:
% Load the original image and binary image
fundus = imread('fundus.jpg');
binary = imread('binary.jpg');
% Find the centroid and major axis length of the region corresponding to the white line
props = regionprops(binary, 'Centroid', 'MajorAxisLength');
center = props.Centroid;
radius = props.MajorAxisLength / 2;
% Create a binary mask with ones everywhere except inside the circular region
[xx, yy] = meshgrid(1:size(fundus, 2), 1:size(fundus, 1));
dist = sqrt((xx - center(1)).^2 + (yy - center(2)).^2);
mask = dist <= radius;
% Set the values inside the circular region to zero
fundus(~mask) = 0;
% Display the resulting image
imshow(fundus);
In this code, we first load the original image and binary image. Then, we use the “regionprops” function to obtain the centroid and major axis length of the region corresponding to the white line in the binary image. We calculate the radius from the major axis length, assuming that the region is circular. We then create a binary mask using “meshgrid” and “sqrt” and set the values inside the circular region to zero. Finally, we set the corresponding pixels in the original image to zero using logical indexing and display the resulting image.
0 Kommentare
Image Analyst
am 2 Mär. 2023
Once you have binarized your image, if you can assume that the largest blob is the optic disk, then you can use bwareafilt to remove it from your binary image which I call "mask" below.
% Get image of just the largest blob alone.
largestBlob = bwareafilt(mask, 1);
% Erase this blob from the image by setting its pixels to false/black/0.
mask(largestBlob) = false; % Or you could also do mask = mask & ~largestBlob
3 Kommentare
Image Analyst
am 10 Mär. 2023
I don't have the full version. You forgot to post it. So all I have is that line of code that you can insert into your full version. If you need help doing that you can upload your full version and original image with no white circle, tick marks, or other annotations.
Siehe auch
Kategorien
Mehr zu Image Processing and Computer Vision finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!