在opencv中输出rgb2lab函数的图像

时间:2015-08-23 15:38:03

标签: matlab opencv image-processing

我正在尝试在rgb2lab()中实现opencv功能。这是我的输入和输出图像(以下链接):

input

output

这是否与Matlab函数rgb2lab()的输出相当。如果没有,请在Matlab中输入rgb2lab函数获得的输出作为输入图像。

1 个答案:

答案 0 :(得分:2)

您可以将Matlab中的RGB图像转换为实验室:

img = imread('path_to_image');
% Matlab 2015
%lab = rgb2lab(img);

% Matlab 2013
cform = makecform('srgb2lab');
lab = applycform(img,cform); 

并在OpenCV中:

#include <opencv2\opencv.hpp>
using namespace cv;

int main()
{
    Mat3b img = imread("path_to_image");

    Mat3b lab;
    cvtColor(img, lab, COLOR_BGR2Lab);

    // NOTE: pixel values are the same as Matlab now.
    // However, since OpenCV stores RGB values as BGR, 
    // to VISUALIZE the same image as Matlab you
    // need to swap R and B channels.
    // For visualization, whatever colorspace the image is in,
    // the image is always considered as an BGR image.

    Mat3b visuallySameAsMatlab;
    cvtColor(lab, visuallySameAsMatlab, COLOR_BGR2RGB);

    return 0;
}

注意:调用cvtColor(img, lab, COLOR_BGR2Lab);后,像素值与Matlab相同。但是,出于可视化目的,您需要交换B和R通道,因为OpenCV在内部将BGR图像存储为RGB。 当您可视化图像时,无论在何种颜色空间中,您都将像素值视为RGB(或OpenCV中的BGR)。