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?

 Réponse acceptée

Image Analyst
Image Analyst le 1 Juin 2012
Modifié(e) : Image Analyst le 27 Mai 2022

0 votes

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 commentaires

HASNAIN
HASNAIN le 1 Juin 2012
You Sir are an absolute legend! I really appreciate you spending your valuable time and bearing with me.
A few minor comments if i may! Firstly the image that i had posted was just as an example (as the one that i am working on is a sinusoid represented by a fourier series). Since i am an absolute beginner, i thought i will start off with the one i posted. I am not sure whether if the image iam working on is RGB or indexed?!
However the code that you have posted is very useful. I understand most of it and just had a question about this part :
% 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);
1)How do you know to choose the number 93?
2) Why do you say : redChannel = indexedImage == 93;
3) Would this code work for any time varying waveform or is it a special case for a sin wave?
Many thanks once again!
Image Analyst
Image Analyst le 1 Juin 2012
1) I looked at indexedImage in the variable editor and looked at the pattern of numbers to identify the number that was associated with the blue waveform.
2) This gives a binary image where it's true where indexedImage = 93 and false otherwise. Having that image will allow me to scan down each column to find the first "true/1/white" pixel.
3) Yes it would, but the scaling would have to be customized for each x and y scale of course. And it will only work for waveforms where you have 1 y value for each x value, unless you modified it to find multiple y values.
HASNAIN
HASNAIN le 1 Juin 2012
Awesome awesome!
For what you said for 1). is it possible you could show exactly where you saw that please (as a last question!) As in a screen shot or an exact place to look please!! What pattern is it that you saw to work out that the number 93 corresponds to blue in the image?
Thanks!
HASNAIN
HASNAIN le 1 Juin 2012
As in here is a screen shot of the variable editor for the image :
http://s18.postimage.org/5zrh8e120/Untitled.jpg
I can mainly see 98's in there. So how did you know that blue corresponds to 93?!
Image Analyst
Image Analyst le 2 Juin 2012
It looked like if I went about a quarter of the way over, and then went down, I should find the blue line. So do that and scroll to around column 111 and row 48 and you'll see this swatch of non-98 numbers at about the location of where I expected to see the blue line. This swatch of numbers was 93 so I figured that a gray level of 93 was being mapped to blue by the colormap.
HASNAIN
HASNAIN le 2 Juin 2012
I see. Was that just out of intuition or did you work out in some way to go about a quarter of the way over and down?
Thanks!
Image Analyst
Image Analyst le 2 Juin 2012
I looked at the image that was displayed. The top of the left hump of the sine was about a quarter of the way over from the left edge of the image, and it was near the top of the image. So that's how I found it.
HASNAIN
HASNAIN le 2 Juin 2012
Nice nice! Thank you very very much mate. You've been very helpful. Just as a parting gift, could you point me in the right direction for learning image segmentation please (Like what you just taught me!). By this i mean tutorials and stuff which has examples i can follow.
Many thanks once again mate.
Image Analyst
Image Analyst le 3 Juin 2012
Modifié(e) : Image Analyst le 27 Mai 2022
Check the help for others. Search the web for "image processing tutorials" or courses or demos or similar things. Good luck.
Stephen john
Stephen john le 27 Mai 2022
@Image Analyst Where is the image file?

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 1 Juin 2012

0 votes

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 commentaires

HASNAIN
HASNAIN le 1 Juin 2012
Hi,
By detect i mean formulating a way of getting the matlab code to be able to differentiate the graph of the sinusoid with the rest of the image. Just as an example here is a sinusoid :
http://images.dsplog.com/db-install/wp-content/uploads/2008/02/single_sine_wave.png
I want to be able to write a program that firstly differentiates the sinusoid from the background. Once this is established, i want to be able to calculate things like the amplitude at different times.
Thanks!
Image Analyst
Image Analyst le 1 Juin 2012
Get the red channel - it will be dark where your curve is
redChannel = rgbImage(:,:, 1);
% Threshold it
binaryImage = redChannel < 128;
% Crop to get rid of axes, tick marks, etc.
% You supply this code. Hint: imcrop() or use indexing to extract a submatrix.
% Then find row # for each column
for col = 1 : size(redChannel, 2)
xy(col, 1) = col; % x value
xy(col, 2) = find(binaryImage(:, col), '1', 'first'); % y value
end
Now you have the x and y values for the curve. Assuming this is a homework problem, I'll let you figure out how to get the equation from the x,y values. Good luck.
HASNAIN
HASNAIN le 1 Juin 2012
Thank u very much for a speedy reply its just i didnt get an email to find out that you had replied!
A few minor things :
1)
binaryImage = redChannel < 128;
How did you know to choose the number 128?
2)
% Then find row # for each column
for col = 1 : size(redChannel, 2)
xy(col, 1) = col; % x value
xy(col, 2) = find(binaryImage(:, col), '1', 'first'); % y value
end
I dont quite understand what this for loop is doing here. When i run it i get : Subscripted assignment dimension mismatch error. Am i doing something wrong here?
Thanks!!
Image Analyst
Image Analyst le 1 Juin 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.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by