Drawing a circle around optic disk

3 views (last 30 days)
AJay
AJay on 4 Aug 2018
Commented: AJay on 4 Aug 2018
Hi,
I'm a new bee to Matlab. In my project, I'm supposed to draw a circle along the border of the Optic disk found in the retinal image. (Actually, I want to detect optic disk). I did some work and the
I = imread('I.png');
% gradient filter applied
[Gmag Gdir] = imgradient(I, 'prewitt');
figure, imshow(Gmag, []), title('Gradient magnitude')
figure, imshow(Gdir, []), title('Gradient direction')
% Binary image gerated with threshold
bi = Gmag > 70;
Gx = [1 +2 +1; 0 0 0; -1 -2 -1]; Gy = Gx';
temp_x = conv2(bi, Gx, 'same');
temp_y = conv2(bi, Gy, 'same');
b2 = sqrt(temp_x.^2 + temp_y.^2);
figure, imshow(b2);
%image opened horizontally and vertically
se90 = strel('line',3,90);
se0 = strel('line',3,0);
BWdil = imdilate(b2, [se90 se0]);
figure, imshow(BWdil);
% Boarders are cleared
BWnobo = imclearborder(BWdil,4);
figure, imshow(BWnobo);
% Refined the edges
SeD = strel('diamond',1);
BWf = imerode(BWnobo, SeD);
BWfinal = imerode(BWf,SeD);
figure, imshow(BWf),
% dege detection done with im2bw
x = im2bw(BWf,0.5);
figure, imshow(x),title('X Image');
Now I'm having a dotted line around the optic disk. Could anyone help me to draw a proper circle along the border of the optic disk and to get the coordinates of the centre point with the radius of the circle drawn?
Images attached I = original grayscale image
<<
<<
>> x = final output of the code
>>
Thanks

Accepted Answer

Image Analyst
Image Analyst on 4 Aug 2018
I would not do an edge detection. That's a typical "new bee" thing to do and rarely works. Since your optic disk is a bright blob on the left part of the image and known/assumed to be a perfect circle, you can just use that knowledge to your advantage and threshold the image to find the disc's region (or at least most of it), then fit the blob's "good" right side to a circle using code from the FAQ. Here is the code:
% Routine to find the optic disc in one particular image.
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 = 20;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'i.jpg';
% Get the full filename, with path prepended.
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);
% 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(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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;
% Optic disk is known to be on the left, so erase the image on the right.
clippedImage = grayImage; % Initialize.
clippedImage(:, 1200:end) = grayImage(1,1); % Set equal to surrounding template.
% Show the histogram
subplot(2, 2, 2);
[counts, grayLevels] = histcounts(clippedImage(:));
counts(counts == max(counts)) = 0; % Suppress spike
bar(grayLevels(1:end-1), counts);
grid on;
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Binarize the image
binaryImage = clippedImage >= 245;
% Take largest blob
binaryImage = bwareafilt(binaryImage, 1);
% Take convex hull
binaryImage = bwconvhull(binaryImage);
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% It is known/assumed that the optic disk's "good part" is on the right,
% so find pixels on the right side.
% First find boundaries
boundaries = bwboundaries(binaryImage);
xBoundary = boundaries{1}(:, 2);
yBoundary = boundaries{1}(:, 1);
% Scan down finding right edge
xRight = zeros(1, columns);
yRight = zeros(1, columns);
for row = 1 : rows
x = find(binaryImage(row, :), 1, 'last');
if ~isempty(x)
xRight(row) = x;
yRight(row) = row;
end
end
% Get rid of rows with no object in them.
noObjectRows = xRight == 0;
xRight(noObjectRows) = [];
yRight(noObjectRows) = [];
% Fit xRight and yRight to a circle
[xCenter, yCenter, R, a] = circfit(xRight,yRight);
% Plot over image.
% Display the image.
subplot(2, 2, 4);
imshow(grayImage, []);
title('Original Image with Disc Circled', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
viscircles([xCenter, yCenter], R);
% Circle fitting routine from the FAQ.
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;sum(xy) sum(yy) sum(y);sum(xx) sum(xy) sum(x)];
B=[-sum(xx+yy) ; -sum(xx.*y+yy.*y) ; -sum(xx.*x+xy.*y)];
a=A\B;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
  2 Comments
jonas
jonas on 4 Aug 2018
+1, guess I misunderstood which part the optic disc is
AJay
AJay on 4 Aug 2018
Thank you very much. This is an excellent answer. I learnt a lot form your code. (Still, there are few parts that I could not understand. I'll catch them up very quickly.) I got the guts to improve my skills in Matlab and image processing.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!