Octave imwrite loses grayscale

2019-07-31 10:39发布

I am using Octave 3.6.4 to process an image and store it afterwards. The image I read is grayscale, and after calculations the matrix should be of the same type. However, if I open the stored image, there are no gray pixels. There are only black and white ones and the gray ones got lost. They are essentially all white.

Here is the processing code:

function aufgabe13()
    [img, map, alpha] = imread("Buche.png");
    [imax, jmax] = size(img);
    a = 0.7;
    M = 255 * ones(imax,jmax + round(imax * a));
    for i = 1:imax
        begin = round((imax-i)*a);
        M(i,begin +1 : begin + jmax) = img(i,:);
    end
    imwrite(M, 'BucheScherung.png', 'png');
end

So what am I doing wrong?

1条回答
走好不送
2楼-- · 2019-07-31 11:00

The reason why is because M is a double matrix so the values are expected to be between [0,1] when representing an image. Because your values in your image are between [0,255] when read in (type uint8), a lot of the values are white because they're beyond the value of 1. What you should do is convert the image so that it is double precision and normalized between [0,1], then proceed as normal. This can be done with the im2double function.

In other words, do this:

function aufgabe13()
    [img, map, alpha] = imread("Buche.png");
    img = im2double(img); % Edit
    [imax, jmax] = size(img);
    a = 0.7;
    M = ones(imax,jmax + round(imax * a)); % Edit
    for i = 1:imax
        begin = round((imax-i)*a);
        M(i,begin +1 : begin + jmax) = img(i,:);
    end
    imwrite(M, 'BucheScherung.png', 'png');
end
查看更多
登录 后发表回答