使用imread将OpenCV图像从RGB转换为灰度的结果较差 [英] OpenCV image conversion from RGB to Grayscale using imread giving poor results

查看:136
本文介绍了使用imread将OpenCV图像从RGB转换为灰度的结果较差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将PNG文件中的24位RGB图像加载到我的OpenCV应用程序中.

I'm loading a 24 Bit RGB image from a PNG file into my OpenCV application.

但是直接使用imread将图像加载为灰度图像会产生非常差的结果.

However loading the image as grayscale directly using imread gives a very poor result.

Mat src1 = imread(inputImageFilename1.c_str(), 0);

将RGB图像加载为RGB并将其转换为灰度可提供更好的外观效果.

Loading the RGB image as RGB and converting it to Grayscale gives a much better looking result.

Mat src1 = imread(inputImageFilename1.c_str(), 1);
cvtColor(src1, src1Gray, CV_RGB2GRAY);

我想知道我是否对图像类型正确使用了imread.有没有人经历过类似的行为?

I'm wondering if I'm using imread for my image type correctly. Has anyone experienced similar behavior?

使用imread转换为灰度的图像如下所示:

The image converted to grayscale using imread is shown here:

使用cvtColor转换为灰度的图像如下所示:

The image converted to grayscale using cvtColor is shown here:

推荐答案

我今天遇到了同样的问题.最终,我比较了三种方法:

I was having the same issue today. Ultimately, I compared three methods:

//method 1
cv::Mat gs = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);

//method 2
cv::Mat color = cv::imread(filename, 1); //loads color if it is available
cv::Mat gs_rgb(color.size(), CV_8UC1);
cv::cvtColor(color, gs_rgb, CV_RGB2GRAY);

//method 3
cv::Mat gs_bgr(color.size(), CV_8UC1);
cv::cvtColor(color, gs_bgr, CV_BGR2GRAY);

方法1(加载灰度)和方法3(CV_BGR2GRAY)产生相同的结果,而方法2产生不同的结果.为了我自己的目的,我开始使用CV_BGR2GRAY.

Methods 1 (loading grayscale) and 3 (CV_BGR2GRAY) produce identical results, while method 2 produces a different result. For my own ends, I've started using CV_BGR2GRAY.

我的输入文件是jpg,因此可能存在与您的特定图像格式有关的问题.

My input files are jpgs, so there might be issues related to your particular image format.

这篇关于使用imread将OpenCV图像从RGB转换为灰度的结果较差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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