Octave imwrite失去了灰度 [英] Octave imwrite loses grayscale

查看:285
本文介绍了Octave imwrite失去了灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Octave 3.6.4来处理图像并在之后存储它。我读取的图像是灰度图像,计算后矩阵应该是相同的类型。但是,如果我打开存储的图像,则没有灰色像素。只有黑色和白色,灰色的丢失了。它们基本上都是白色的。

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.

这是处理代码:

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

那么我做错了什么?

推荐答案

原因是因为 M double 矩阵,因此在表示图像时,预计值将介于 [0,1] 之间。因为在读入时(类型 uint8 ),图像中的值在 [0,255] 之间,所以很多值是白色的,因为它们超出了1的值。您应该做的是将图像转换为双精度并在 [0,1] 之间标准化,然后继续像平常一样。这可以通过 im2double <来完成/ a>功能。

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.

换句话说,这样做:

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

这篇关于Octave imwrite失去了灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆