简体   繁体   English

OpenCV:imwrite 在保存时更改通道像素值

[英]OpenCV : imwrite changes the channels pixels values when saving

I'm reading an image and doing some processing on the blue channel without changing the Red nor the green channels.我正在读取图像并对蓝色通道进行一些处理,而不更改红色和绿色通道。

When i finished processing the blue channel, i merged back the three channels into one RGB image.当我处理完蓝色通道后,我将三个通道合并回一个 RGB 图像。 and when i use imshow to view the channels, every thing is alright and i can see that the changes i've made only affect the Blue channel and they do not affect the red nor the green ones.当我使用 imshow 查看频道时,一切正常,我可以看到我所做的更改仅影响蓝色频道,不会影响红色和绿色频道。

Up to this point every thing is alright !至此,一切正常!

But when i save the image using imwrite, the resulting image is slightly different, in that the changes made on the blue channel seem to get propagated to the red and green channels, it's like imwrite is doing some kind of mean between the 3 channels :但是当我使用 imwrite 保存图像时,生成的图像略有不同,因为在蓝色通道上所做的更改似乎会传播到红色和绿色通道,就像 imwrite 在 3 个通道之间做某种平均:

 image = imread('image.jpg', IMREAD_COLOR);
 split(image, channels);
// Create some changes on channels[0]

merge(channels, 3, image);
// Up to this point every thing is alright
imwrite("modified.jpg", image); // Image changes when written;

Is there any solution to avoid this behavior ?有什么解决方案可以避免这种行为吗?

JPG is a lossy format: https://en.wikipedia.org/wiki/JPEG JPG 是一种有损格式: https : //en.wikipedia.org/wiki/JPEG

JPEG (/ˈdʒeɪpɛɡ/ JAY-peg) 1 is a commonly used method of lossy compression for digital images, particularly for those images produced by digital photography. JPEG (/ˈdʒeɪpɛɡ/ JAY-peg) 1是一种常用的数字图像有损压缩方法,特别是对于数字摄影产生的图像。 The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality.压缩程度可以调整,允许在存储大小和图像质量之间进行可选择的权衡。 JPEG typically achieves 10:1 compression with little perceptible loss in image quality. JPEG 通常可实现 10:1 压缩,而图像质量几乎没有明显损失。

Solution: Use a lossles Format like PNG to save your image.解决方案:使用像 PNG 这样的无损格式来保存您的图像。

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

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