How can I improve my segmented image?
1 view (last 30 days)
Show older comments
Hi, I wrote the code below in order to segment blood vessels of the retina. But the result is not really good. Even I tried another way that I put it here to but it didn't make it better too. Can anyone give me a suggestion about how can I improve it? My images are from STARE database (<http://cecas.clemson.edu/~ahoover/stare/>) here is the example of my image:
FIRST WAY:
clc; clear all; close all;
%%Specifying path folders
%%%ex.d_img2 - F:\folder1\folder2\...\foldern\*.format %%%
%%%ex.di_img2 - F:\folder1\folder2\...\foldern\ %%%
d_img2 = 'F:\Uni\Project\MATLAB\myMatlab\prepImages\*.ppm';
di_img2 = 'F:\Uni\Project\MATLAB\myMatlab\prepImages\';
%%Reading images
srcFiles_prepImage = dir(d_img2);
N_img_prep = length(srcFiles_prepImage);
% i = 1; %%50
% i = 2; %%150
i = 3; %%200
filename2 = strcat(di_img2, srcFiles_prepImage(i).name);
prepImage = imread(filename2);
prepImage = prepImage(:,:,2); %?% the picture had been saved in rgb format?
figure
imshow(prepImage, 'InitialMagnification', 'fit')
%%Histogram
[row,column] = size(prepImage);
figure
imhist(prepImage);
%%Creating & Applying binary mask in order to making the background (outside of eye) black
%Using threshold
threshold_value = 100;
mask = prepImage > threshold_value; % Bright objects will be chosen.
mask = imfill(mask, 'holes'); % Do a "hole fill" to get rid of any background pixels or "holes".
%%%%check if there is any black part inside the white part
figure
imshow(mask, 'InitialMagnification', 'fit')
% Applying binary mask to image
maskedImage = bsxfun(@times, prepImage, cast(mask,class(prepImage)));
figure
imshow(maskedImage, 'InitialMagnification', 'fit')
figure
subplot(1,3,1)
imhist(maskedImage)
%%Thresholding histogram
[pixelCount,grayLevels] = imhist(maskedImage,256);
peaks = findpeaks(pixelCount);
peaks = transpose(peaks);
maxBin = max(peaks);
maxBinLocation = find(pixelCount == maxBin);
maxI = find(peaks == maxBin);
% Find low threshold
TlowLocation = min(find(pixelCount == 0)); % Find first zero
% Place vertical bar on histogram to show Tlow
subplot(1,3,3)
imhist(maskedImage)
hold on
y2 = ylim();
line([TlowLocation, TlowLocation], [y2(1), y2(2)], ...
'Color', 'r', 'LineWidth', 2);
% Find high threshold
minBinL = min(peaks); %%Find the minimum on left side of peak
minBinLocationL = find(pixelCount == minBinL);
%%%%
% if minBinLocation>maxBinLocation
%%%%need an IF to make sure that it is on left side
%%%%
% Find the minimum on left side of peak
minIL = find(peaks == minBinL);
leftBins = peaks(minIL:maxI);
leftMean = mean(leftBins);
subLeft = maxBin - leftMean;
% Find the closest value to subLeft
[subLeftClosest,indexLeftClosest] = min(abs(leftBins - subLeft));
ThighBin = leftBins(indexLeftClosest);
ThighLocation = find(pixelCount == ThighBin);
% Place vertical bar on histogram to show Thigh
subplot(1,3,2)
imhist(maskedImage)
hold on
yl = ylim();
line([ThighLocation, ThighLocation], [yl(1), yl(2)], ...
'Color', 'r', 'LineWidth', 2);
% Place vertical bars on histogram to show Tlow&Thigh
figure
imhist(maskedImage)
hold on
yl = ylim();
line([TlowLocation, TlowLocation], [yl(1), yl(2)], ...
'Color', 'r', 'LineWidth', 2);
hold on
y2 = ylim();
line([ThighLocation, ThighLocation], [y2(1), y2(2)], ...
'Color', 'r', 'LineWidth', 2);
% Find pixels between the thresholds
betweenThresholds = maskedImage<ThighLocation & maskedImage>TlowLocation;
Vs = betweenThresholds;
Ts = length(Vs);
figure
imshow(betweenThresholds, 'InitialMagnification', 'fit');
SECOND WAY:
clc; clear all; close all;
%%Specifying path folders
%%%ex.d_img2 - F:\folder1\folder2\...\foldern\*.format %%%
%%%ex.di_img2 - F:\folder1\folder2\...\foldern\ %%%
d_img2 = 'F:\Uni\Project\MATLAB\myMatlab\prepImages\*.ppm';
di_img2 = 'F:\Uni\Project\MATLAB\myMatlab\prepImages\';
%%Reading images
srcFiles_prepImage = dir(d_img2);
N_img_prep = length(srcFiles_prepImage);
% i = 1; %%50
% i = 2; %%150
i = 3; %%200
filename2 = strcat(di_img2, srcFiles_prepImage(i).name);
prepImage = imread(filename2);
IG = uint8(prepImage*255);
prepImage = prepImage(:,:,2); %?% the picture had been saved in rgb format?
figure
imshow(prepImage, 'InitialMagnification', 'fit')
%%Histogram
[row,column] = size(prepImage);
figure
imhist(prepImage,256);
%%Thresholding histogram
[pixelCounts,grayLevels] = imhist(prepImage,256);
cdf = cumsum(pixelCounts); % Sum histogram counts to get cumulative distribution function
cdf = cdf / cdf(end); % Normalize
% Get data value where ?%&?% is.
data30 = find(cdf>= 0.2, 1, 'first');
data70 = find(cdf>= 0.4, 1, 'first');
% Place vertical bars on histogram to show Tlow&Thigh
figure
imhist(prepImage)
hold on
y1 = ylim();
line([data30, data30], [y1(1), y1(2)], ...
'Color', 'r', 'LineWidth', 2);
hold on
y2 = ylim();
line([data70, data70], [y2(1), y2(2)], ...
'Color', 'r', 'LineWidth', 2);
% Find pixels between the thresholds
betweenThresholds = prepImage<data70 & prepImage>30;
Vs = betweenThresholds;
Ts = length(Vs);
figure
imshow(betweenThresholds, 'InitialMagnification', 'fit');
%%Creating & Applying binary mask in order to making the background (outside of eye) black
%Using threshold
threshold_value = 100;
mask = prepImage > threshold_value; % Bright objects will be chosen.
mask = imfill(mask, 'holes'); % Do a "hole fill" to get rid of any background pixels or "holes".
%%%%check if there is any black part inside the white part
figure
imshow(mask, 'InitialMagnification', 'fit')
% Applying binary mask to image
maskedImage = bsxfun(@times, betweenThresholds, cast(mask,class(betweenThresholds)));
figure
imshow(maskedImage, 'InitialMagnification', 'fit')
0 Comments
Accepted Answer
Image Analyst
on 8 May 2018
Basically you're just doing a primitive global histogram. No matter how you decide on a global threshold, it won't get all the vessels, no matter which threshold you pick.
You'd be better off using one of the successful published algorithms here: http://www.visionbib.com/bibliography/contentsmedical.html#Medical%20Applications,%20CAT,%20MRI,%20Ultrasound,%20Heart%20Models,%20Brain%20Models
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!