'improfile' is a missing function in octave, what other option could I use to make my loop work?

imshow(matrix(:,:,1))
%identify axes
[x, y] = ginput(2);
% preallocate matrices
cog = zeros(size(matrix,3),1);
% loop start
for i = 1:size(matrix,3)
I = matrix(:,:,i);
%what to do to make this code works in octave
test = improfile(I,[x(1) x(2)],[y(1) y(2)]);

cog(i) = sum((1:length(test)).*test')/sum(test); 
% loop end

end
scog = (cog - min(cog)) / (max(cog) - min(cog));

so, octave does not have the graphical picking option for getting pixel values that matlab has implemented. do you need this ability to use the mouse to select a polyline of image values? the closest I can see in the image package is the impixel function that returns pixel values given the desired set of x-y coordinates. (peeking at the code, it does seem to suggest there is an ‘interactive mode’ that may be closer to the graphical picking you want, but I’m not familiar enough with the package to be certain)

Thank you for this.
I used interp2 (have a look at the code below) instead improfile but I still have an error: interp2: X and Y must be matrices of equal size
Any suggestions for this

n = ceil(norm([diff(x), diff(y)])); % A rough estimation of number of points
test = interp2(I, 2, linspace(x(1), x(2), n), linspace(y(1), y(2), n));

Your use of improfile:

 test = improfile(I,[x(1) x(2)],[y(1) y(2)]);

is not interactive so should be relatively simple to implement. My guess is that you only need a call to interp2 but need to do convert the arguments first. impixel from the image package does something similar so take a look how that’s done there (run pkg load image; type impixel to see the source for impixel).

what do you mean by ‘convert’?

impixel expects image coordinates for each pixel. if you just generated a list of x and y values between hte two desired endpoints, you could pass those to impixel and get the values.

e.g., here’s a test image and some values along the diagonal:

a = 0:1/255:1; 
Img = a .* a';
Img = uint8(Img * 255);
imshow (Img);

Img =
image

x = 50:75;
y = x;
rgb = impixel (Img, x,y)
rgb =

    9    9    9
   10   10   10
   10   10   10
   11   11   11
   11   11   11
   11   11   11
   12   12   12
   12   12   12
   13   13   13
   13   13   13
   14   14   14
   14   14   14
   15   15   15
   15   15   15
   16   16   16
   16   16   16
   17   17   17
   17   17   17
   18   18   18
   18   18   18
   19   19   19
   19   19   19
   20   20   20
   20   20   20
   21   21   21
   21   21   21

note that impixel always returns a nx3 matrix, even for greyscale images. improfile would return nx1 for greyscale and nx3 for color images. from your code it may be that it was expecting only nx1, so you may have to trim out the other redundant columns.

interp2 could maybe work too, and it would give only one column for a greyscale image, but your inputs aren’t in the order interp2 wants. you should look at the help for interp2 (help interp2) and decide what calling form you what to use, and provide your inputs accordingly.

2 Likes

I meant that if you look inside impixel, it converts the 2D array that is your image, into the three arguments (X, Y, and Z) that interp2 expects.