简体   繁体   中英

OCTAVE - ind2rgb: X must be an indexed image

I am trying to plot and save some meteorological data into png image files using the following lines.

imag = data2image(flipud(vari'));
imag = gray2ind(imag);
imwrite(imag,colormap('jet'),'PSFC_RIO_im.png','png');

where data2image is a custom function that re-scales the data to be usable by image functions.

function image = data2image(data)
    HIGH = max(data(:));
    LOW = min(data(:));
    image = (data - LOW)/(HIGH-LOW);
end

Unfortunately the imwrite complains with the following error:

error: ind2rgb: X must be an indexed image
error: called from:
error:   /usr/local/share/octave/3.6.4/m/image/ind2rgb.m at line 44, column 5
error:   /usr/local/share/octave/3.6.4/m/image/imwrite.m at line 176, column 16
error:   /home/tufts/Documents/Octave/geomat.m at line 53, column 1

Where line 53 is the imwrite in the code above. This error makes absolutely no sense to me, as imag was converted to an indexed image with the gray2ind function. I also tested this by plotting with imshow , which works.

EDIT - class of imag:

octave:9> class(imag)
ans = double

I am going to answer my own question after accidentally, in an unrelated problem, figuring out how.

Indexed images in MATLAB have a minimum value of 1. My function was getting the minimum value of the data, and making that equal to 0.

Take care of that, and you solve the problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM