1

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?

rayryeng
  • 102,964
  • 22
  • 184
  • 193
Nad
  • 13
  • 3

1 Answers1

3

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
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    instead of the division by 255 or similar hacks which will break if the next image has different precision, you should use `im2double()`, `im2uint8()`, and friends. – carandraug May 01 '14 at 16:48
  • 2
    Holy crap. I even forgot I answered this question lol. Took you long enough to accept. – rayryeng Jul 30 '15 at 17:25