在OpenCV C ++中将RGB彩色图像转换为索引彩色图像类型 [英] Convert RGB Color Image to Indexed Color Image Type in OpenCV C++

查看:751
本文介绍了在OpenCV C ++中将RGB彩色图像转换为索引彩色图像类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将rgb图像转换为索引颜色类型.

I want to convert rgb image to indexed color type.

这是我的Code C ++.这将转换为灰度.

Here is my Code C++. This is convert to gray scale.

我该怎么办?

Mat black_background = imread("image_path", IMREAD_COLOR); 

Mat output;
cvtColor(black_background, output, cv::COLOR_RGB2GRAY);

imwrite("save_path", output);

推荐答案

我不相信 OpenCV imwrite()支持编写浅化的PNG.一种选择是写成您喜欢的任何其他格式,然后使用 ImageMagick 转换为浅绿色的PNG.

I do not believe OpenCV's imwrite() supports writing a palettised PNG. One option may be to write as any other format you like and convert to a palettised PNG with ImageMagick afterwards.

ImageMagick 包含在大多数Linux发行版中,并且可用于macOS和Windows.

ImageMagick is included in most Linux distros and is available for macOS and Windows.

因此,在Windows的终端或命令提示符下:

So, in Terminal, or Command Prompt on Windows:

magick input.png PNG8:result.png

ImageMagick 的v7之前的版本是

convert input.png PNG8:result.png

PNG8:前缀强制显示结果.

如果您不想在事后明确转换文件,可以使用 OpenCV imwrite()作为文件系统中的PNG文件编写它们,然后使用C ++的获取 ImageMagick 进行转换:

If you don't want to have to go around converting your files explicitly afterwards, you could write them using OpenCV's imwrite() as PNG files in the filesystem and then use C++'s system() to get ImageMagick to convert them:

#include <stdlib.h>

...

imwrite('result.png', image);

// Delegate conversion to palette image to ImageMagick
system("magick result.png PNG8:result.png');


对于更急/敏锐的读者:


For the more anxious/astute readers:

  • 是的,与其他Unix实用程序不同,可以使用相同的文件作为 ImageMagick 的输出来输入,并且不必担心损坏,因为可以完全读取文件在处理然后写出来之前,

  • yes, unlike with other Unix utilities, it is ok to use the same file for input as output with ImageMagick and there is no fear of corruption because the file is read in its entirety before being processed and then written out,

是的, ImageMagick 将隐式处理将图像中的颜色减少为调色板版本所需的量化,

yes, ImageMagick will implicitly take care of the quantisation necessary to reduce the colours in the image to a palette version,

是,您可以使用其他前缀强制使用其他类型的PNG文件,例如RGB24:result.png将导致RGB888输出,RGB32:result.png将导致RGBA8888输出,RGB48:result.png将导致16位红色,16位绿色和16位蓝色,等等.

yes, you can force other types of PNG file with other prefixes, e.g. RGB24:result.png will result in RGB888 output, RGB32:result.png will result in RGBA8888 output, RGB48:result.png will result in 16-bit red, 16-bit green and 16-bit blue and so on.

这篇关于在OpenCV C ++中将RGB彩色图像转换为索引彩色图像类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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