Detect a sinusoid in an image
27 views (last 30 days)
Show older comments
Hi all,
I am fairly new to image processing in MATLAB and I'm trying to learn as much as possible. So my aim is to extract a sinusoid that is drawn on graph paper which has been scanned in as an image. As i said, I am fairly new to image processing and trying very hard to learn as much as possible.
So the image has a sinusoid which is time varying and the background is that of a graph paper.
Could anyone point me in the right direction to go about accomplishing this task please?
0 Comments
Accepted Answer
Image Analyst
on 1 Jun 2012
Edited: Image Analyst
on 27 May 2022
Try this code:
clc;
clearvars;
close all;
workspace;
fontSize = 20;
ullFileName = 'C:\Users\Hasnain\Documents\Temporary\single_sine_wave.png';
indexedImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(indexedImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(indexedImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find the sine wave. This is where the image = 93.
redChannel = indexedImage == 93;
subplot(2, 2, 2);
imshow(redChannel, []);
title('Sine Wave Image', 'FontSize', fontSize);
% Then find row # for each column
numberOfColumns = size(redChannel, 2);
x = zeros(numberOfColumns, 1);
y = zeros(numberOfColumns, 1);
for col = 1 : numberOfColumns
x(col, 1) = col; % x value
thisy = find(redChannel(:, col), 1, 'first'); % y value
if isempty(thisy)
y(col) = nan; % y value
else
y(col) = thisy; % y value
end
end
% Get rid of nan's
nanIndexes = isnan(y);
y(nanIndexes) = [];
x(nanIndexes) = [];
% Plot what we have so far.
subplot(2, 2, 3);
plot(x, y, 'b', 'LineWidth', 5);
grid on;
title('Sine Wave Image', 'FontSize', fontSize);
xlabel('Column', 'FontSize', fontSize);
ylabel('Row', 'FontSize', fontSize);
% Scale to calibrated units.
% Scale y
y_Range = max(y) - min(y);
scaled_y = 1 + 2 * (min(y) - y) / y_Range;
% Scale x
scaled_x = linspace(0, 65, length(scaled_y));
subplot(2, 2, 4);
plot(scaled_x, scaled_y, 'b', 'LineWidth', 5);
grid on;
title('Sine Wave Image in Calibrated Units', 'FontSize', fontSize);
xlabel('Sample Number', 'FontSize', fontSize);
ylabel('Amplitude', 'FontSize', fontSize);
11 Comments
Image Analyst
on 27 May 2022
@Stephen john it was in a link in one of his other comments:
More Answers (1)
Image Analyst
on 1 Jun 2012
What do you mean by detect? How about this:
sinusiodPixels = grayImage < thresholdValue;
That will give you a binary image of anything dark on your paper. Or so you want the parameters like the wavelength and amplitude?
4 Comments
Image Analyst
on 1 Jun 2012
I see you need some more help. So I downloaded your image and discovered it was an indexed image rather than an RGB image. So I made slight modifications to the code to get the blue sine wave, and added the parts on getting the x,y coordinates and transforming them into calibrated coordinates and plotting. See code in my other Answer here on this page.
See Also
Categories
Find more on Image Processing Toolbox 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!