Convert RGB to L*a*b* (Lab Color space) in OpenCV

Problem 1: 
Using cvCvtColor convert a sRGB image into Lab image; In other words change the color space from RGB to LAB
Problem 2:  
Do the same in MATLAB and compare results

*********************************************************************************
Lets begin with problem 2


I_srgb=imread('C:\view0.png');
I_lab = applycform(I_srgb,makecform('srgb2lab', 'AdaptedWhitePoint', whitepoint('D65')));

Lets print some values from three channels (L , a , b)

I_lab(1,1:10,1)

ans =

  184  186  195  202  203  208  213  214  206  235

Trial>> I_lab(1,1:10,2)

ans =

  128  127  125  125  125  124  123  123  125  129

Trial>> I_lab(1,1:10,3)

ans =

  148  152  154  153  153  152  149  147  147  148

Now in OpenCV here is the program

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
          
          
/********************************
* MYcvtColor.cpp
* S. F. Hasan
* *****************************/
#include <opencv2/opencv.hpp>
 
#include <iostream>
#include <fstream>
 
using namespace cv;
using namespace std;
double alpha; /**< Simple contrast control */
int beta; /**< Simple brightness control */
 
int main( int argc, char** argv )
{
IplImage* imgA = cvLoadImage("C:/view0.png");
CvSize img_sz = cvGetSize( imgA );
 
if (!imgA) {
return 0;
}
 
IplImage* lab_image = cvCreateImage( img_sz, imgA->depth, 3 );
cvCvtColor(imgA, lab_image, CV_BGR2Lab);
for (int row=0;row<lab_image->height;row++)
{
const uchar* ptr= (uchar*)(lab_image->imageData + row*lab_image->widthStep );
for(int col=0;col<lab_image->width;col++)
{
//cout << "uchar = "<< endl << " " << (uchar)*ptr << endl << endl;
cout << "int = "<< endl << " " << (int)*ptr << endl << endl;
*ptr++;
}
}
 
cvNamedWindow("rgb",1);
cvNamedWindow("lab",1);
cvShowImage("rgb",imgA);
cvShowImage("lab",lab_image);
 
cvReleaseImage(&imgA);
cvReleaseImage(&lab_image);
 
cvWaitKey();
return 0;
}
view raw MYcvtColor.cpp This Gist brought to you by GitHub.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值