Displaying pixel values of an image in Octave?
Asked Answered
P

1

8

If I load and display an image, for example

c = imread('cameraman.tif');
imshow(c)

then in the plot window, under the image itself, there are two numbers indicating the current cursor position, in the form

[39.25, 120.6]

I have two questions about this:

  1. Can the display be tweaked so that the positions are integers? So one image pixel per screen pixel?
  2. Can this information include the grayscale/rgb value of the pixel, such as

    [23, 46] = 127

    or

    [23, 46] = (46,128,210)?

I've tried fiddling with the "axis" command, but I haven't found anything which helps.

I guess what I want is something like Matlab's "Pixel Information Tool" impixelinfo: http://www.mathworks.com.au/help/images/ref/impixelinfo.html though I know from the octave image wiki at http://wiki.octave.org/Image_package that impixelinfo is not currently implemented in Octave. But maybe there's another way to achieve the same result?

I'm using Octave-3.8.0, the image package 2.2.0, under linux (Ubuntu 12.04).

Poss answered 17/5, 2014 at 2:11 Comment(0)
S
6

GNU Octave 3.8 uses FLTK as standard graphics_toolkit. Unfortunately WindowButtonMotionFcn is only triggered if a button is pressed while the mouse moves (dragging) with this toolkit (Today I would consider this as a bug). But you can use WindowButtonDownFcn.

This examples updates the title with the position and the image value at that position if you click in the image:

img = imread ("cameraman.png");
imshow (img)

function btn_down (obj, evt)
  cp = get (gca, 'CurrentPoint');
  x = round (cp(1, 1));
  y = round (cp(1, 2));
  img = get (findobj (gca, "type", "image"), "cdata");
  img_v = NA;
  if (x > 0 && y > 0 && x <= columns (img) && y <= rows (img))
    img_v = squeeze (img(y, x, :));
  endif

  if (numel (img_v) == 3) # rgb image
    title(gca, sprintf ("(%i, %i) = %i, %i, %i", x, y, img_v(1), img_v(2), img_v(3)));
  elseif (numel (img_v) == 1) # gray image
    title(gca, sprintf ("(%i, %i) = %i", x, y, img_v));
  endif
endfunction

set (gcf, 'WindowButtonDownFcn', @btn_down);

You can also place a text next to the cursor if you want.

Scandura answered 6/10, 2015 at 14:58 Comment(2)
If you want to view pixel values while mouse drag instead of button press use set (gcf, 'WindowButtonMotionFcn', @btn_down)Lamontlamontagne
@sriram: You should read my answer which clearly says that WindowButtonMotionFcn doesn't work with Octave 3.8Scandura

© 2022 - 2024 — McMap. All rights reserved.