0

I have image files of curved black lines on a white background. All the lines are different, but here is one example. The actual image is copyrighted; I cannot upload it. I have traced the line in MS Paint.

red line

My end objective is to calculate the area under these curves using trapz, but first I must convert the y-coordinates to their 10th power, since they are in log scale. I see the first step as getting an array of coordinates that represent the lines.

This is my first time using Octave. I used imread to upload my image. It shows a matrix of 255s and 0s corresponding to my image. I tried

[yy xx] = find(img);

which I saw on another question (Calculate area under FFT graph in MATLAB), but when I entered

figure;plot(xx,yy,'.');

I got an image of two histogram-looking bars.

For my second try, I tried the top answer at

How do I calculate the area under a curve in an image with MATLAB?

However, the first line

[img,map] = imread('file.png')

returned img as uint8 of 178x373x3 and map as a double of 0x0.

Alternately, if there is a smarter way to solve my problem in R or Python, please point me in that direction.

Thanks for any help!

Community
  • 1
  • 1

1 Answers1

0

In your question you said you have a black line and the you've uploaded a red line but here we go:

Your input red_line.png:

input image

## load image
img = im2double (imread ('red_line.png'));

## use only green inverted channel
tmp = 1 - img(:,:,2);

## get the weighted centroid in y direction
## in GNU Octave
#y = sum (tmp .* (1:rows (tmp))') ./ sum (tmp);

## in GNU Octave and Matlab
y = sum (bsxfun (@times, tmp , (1:rows (tmp))')) ./ sum (tmp);


plot (y, 'linewidth', 2)
set (gca, 'ydir', 'reverse')
grid on

print out.png

plot after extraction

NOTE: This example uses automatic broadcasting. In Matlab you have to replace it with bsxfun or wait for R2017 where they will adapt it.

Andy
  • 7,931
  • 4
  • 25
  • 45
  • I don't think taking the centroid is giving an exact copy of the curve. I also don't see how this achieves either of my objectives. Can you explain how this populates an array with x and y values that make up the curve, or finds the area under the curve with a log scale on the y-axis? Thanks – user6917220 Oct 06 '16 at 17:51
  • I See you don't have any clue on image processing. Stackoverflow is for asking help for programming problems not to teach basic concepts. The centroid gives you the best approximation for the y coordinate, it's more accurate than solutions with `find`. The y array is filled in my example and integrating using y is easy. Have you even tried to unterstand the code? – Andy Oct 07 '16 at 05:32