fitting a curve on a binary image and interpolating to produce a continuous graph
7 views (last 30 days)
Show older comments
EYAD SALEH
on 14 Oct 2022
Answered: Image Analyst
on 15 Oct 2022
This is first time I have ever used mathworks, so your help is greatly appreciated. I took a m-mode ultrasound image and imputed it into imagej in which I removed the noise and binarized the image. This resulted in a graph like image, but it is not as smooth as I want. I want to able to curve fit it and potentially use interpollation to give a filled in graph. linked below is my binarized image.
4 Comments
Accepted Answer
Image Analyst
on 15 Oct 2022
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'sinusoid ultrasound.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)
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
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 = grayImage(:, :, 3);
end
% 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;
%--------------------------------------------------------------------------------------------------------
% Display the histogram.
subplot(2, 2, 2);
histogram(grayImage(grayImage>0), 256);
grid on;
title('Histogram of Image Gray Levels', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold the image to get the dark stuff.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 64;
highThreshold = 255;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Take 5 largest blobs
mask = bwareafilt(mask, [10, inf]);
% Fill in the bubble so that it will have only 3 sides.
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask)
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
impixelinfo
% Sum the first 30 rows
horizontalProfile = sum(grayImage(1:30, :));
% Display summed lines.
subplot(2, 2, 4);
hold on;
plot(horizontalProfile, 'b-', 'LineWidth',2);
grid on;
title('Horizontal Profile with Peaks Shown', 'FontSize', fontSize, 'Interpreter', 'None');
% Threshold at 3000 to find peaks
horizontalProfile(horizontalProfile<3000) = 0; % Suppress small peaks
[peakValues, indexesOfPeaks] = findpeaks(horizontalProfile)
hold on
plot(indexesOfPeaks, peakValues, 'rv', 'LineWidth', 2);
% Find the average peak to peak wavelength
meanPeriod = mean(diff(indexesOfPeaks))
% Show sine wave over image
hfig = figure;
imshow(grayImage, []);
title('Image With Sine Wave', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
impixelinfo
hold on
amplitude = rows/2;
x = 1 : columns;
phasex = mean([x(indexesOfPeaks(1)), x(indexesOfPeaks(2))])
y = amplitude * sin(2 * pi * (x - phasex/2) / meanPeriod) + rows/2;
plot(x, y, 'r-', 'LineWidth', 3);
hfig.WindowState = 'maximized'


0 Comments
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!