对于同一张图片,OpenCV和MATLAB灰度值有所不同 [英] OpenCV and MATLAB gray scale values differ for the same image

查看:173
本文介绍了对于同一张图片,OpenCV和MATLAB灰度值有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,我读取了彩色视频,提取了特定的帧并使用rgb2gray函数将其转换为灰度图像.但是当我使用OpenCV2.3.1加载相同的视频时,请提取相同的帧然后进行转换它是灰度的,它不能提供与MATLAB相同的灰度值.为什么会这样?

In MATLAB, i read a color video , extract a certain frame and convert it to a gray scale image using the rgb2gray function.But when I load the same video with OpenCV2.3.1, extract the same frame and then convert it to gray scale it doesn't give the same gray scale values as MATLAB. Why is that?

这是使用OpenCV的C ++代码:

This is the C++ code using OpenCV:

VideoCapture cap(0);    
cap.open("Human sperm evaluation_0.avi");    

Mat image;    
Mat gray(480,640,CV_8U);    

for(int i=0;i<513;i++)
{
    cap>>image;
    cvtColor(image,gray,CV_RGB2GRAY);
}

for(int i=0;i<20;i++)
{
    for(int j=0;j<20;j++)
    {
        cout<<(int)gray.at<uchar>(i,j)<<' ';
    }
}

推荐答案

首先,OpenCV中的所有彩色图像都是BGR而不是RGB,因此问题之一可能是OpenCV使转换错误.您应该使用BGR2GRAY. 其次,如果我对Matlab的印象很好,则应指定图像中值的范围.您必须在0到255之间输入灰度图像.

First, all color images in OpenCV are BGR and not RGB so maybe one of the problems could be that OpenCV is making the transformation wrong. You should use BGR2GRAY. And second, If I remember well in matlab yo should specify which are the ranges of values in your image. You have to put between 0 and 255 for a gray image.

我希望这可以为您提供帮助.

I hope this can help you.

这篇关于对于同一张图片,OpenCV和MATLAB灰度值有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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