简体   繁体   English

读入后更改图像

[英]imread changes the image after reading it in

I have a folder full of .png files and I want to convert them to .jpg . 我有一个充满.png文件的文件夹,我想将它们转换为.jpg I wrote this code to process all of them and to convert them to jpg 我编写了这段代码来处理所有这些并将其转换为jpg

imgPath = 'C:\Users\SET1\writer_2\';
imgType = '*.png'; % change based on image type
images  = dir([imgPath imgType]);

newPath = 'C:\Users\Desktop\SET1\';

for idx = 1:length(images)
    Seq{idx} = imread([imgPath images(idx).name]);

    imwrite(Seq{idx}, strcat(newPath,images(idx).name, '.jpg'));
end

But I noticed that when it reads the image in it somehow changes it. 但是我注意到,当它读取其中的图像时,它会有所改变。 For example here is the original image 例如,这是原始图像

在此处输入图片说明

Here is the image read in, and this is what is saved as the output of imwrite 这是读入的图像,这就是保存为imwrite的输出

在此处输入图片说明

You can basically see the change. 您基本上可以看到更改。 First of it inverted the image, then it converted the strokes of the pen to a collection of dots, as in what happens when one performs im2bw 首先将图像反转,然后将笔的笔画转换为点的集合,如执行im2bw时发生的情况

I do not understand as to why this is happening and how to fix it. 我不明白为什么会这样以及如何解决它。

The problem is that the image is color mapped. 问题是图像是彩色映射的。 You need to read in the files like so: 您需要像这样读取文件:

[I,map]=imread(yourPngFile);
imshow(I,map);
imwrite(I,map,outfilename);

If you want you can convert it and save it in your cell array as an RGB image: 如果需要,可以对其进行转换并将其保存为RGB图像到单元格阵列中:

Seq{idx} = ind2rgb(I,map);

Source: 资源:

How do I load and show a PNG image in MATLAB? 如何在MATLAB中加载和显示PNG图片?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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