根据Opencv中cvtcolor函数的源码描述,,参数CV_BGR2YCrCb用的系数为[1868, 9617, 4899, 11682, 9241], 而参数CV_BGR2YUV对应的系数只是将CV_BGR2YCrCb对应的系数中1868和4899互换而已,

,系数介绍,可参考http://baike.baidu.com/view/640197.htm?fr=aladdin

经过对源码的阅读,对源码做如下改写:

</pre><pre name="code" class="cpp">enum
{R2Y = 4899,G2Y = 9617,B2Y = 1868,
};
inline void RGB2YCrCb_i(int blueIdx, int coeffs[], const int* _coeffs)
{static const int coeffs0[] = {R2Y, G2Y, B2Y, 11682, 9241};//CV_BGR2YUV对应的系数memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);//对应的code 为CV_BRG2YCrCv时, 将CV_BGR2YUV对应的系数的R2y(4899)与B2y(1868)对应的值互换
}

inline void Operater_YCrCb_i(unsigned char* src,int srccn, int blueId,  int coeffs[], unsigned char* dst, int n)
{int scn = srccn, bidx = blueId;int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];ColorChannel cc;int delta = cc.half()*(1<<yuv_shift);n *= 3;for (int i=0; i<n; i +=  3,  src += scn){int Y = CV_DESCALE(src[0]*C0 + src[1]*C1 + src[2]*C2, yuv_shift);int Cr = CV_DESCALE((src[bidx^2] - Y)*C3 + delta, yuv_shift);int Cb = CV_DESCALE((src[bidx] - Y)*C4 + delta, yuv_shift);dst[i] = (unsigned char)(Y);dst[i+1] = (unsigned char)(Cr);dst[i+2] = (unsigned char)(Cb);}}void CvtColor_YCrCb_i(unsigned char* src, int sstep, unsigned char* dst, int dstep, int nWidth, int nHeight, int nChannel,  int code, int dcn)
{assert(nWidth >=0 && nHeight >= 0);int scn = nChannel, bidx;unsigned char* src_ptr = NULL;unsigned char* dst_ptr = NULL;switch(code){case CV_BGR2YCrCb: case CV_RGB2YCrCb:case CV_BGR2YUV: case CV_RGB2YUV:assert(scn == 3 || scn == 4);bidx = code == CV_BGR2YCrCb || code == CV_RGB2YUV ? 0 : 2;static const int yuv_i[]= {B2Y, G2Y, R2Y, 8061, 14369};const int* coeffs_i = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_i;for (int row = 0; row < nHeight; row++){src_ptr = (unsigned char*)(src + row*sstep);dst_ptr = (unsigned char*)(dst + row*dstep);for (int col = 0; col < nWidth; col++){int coeffs[5];RGB2YCrCb_i(bidx, coeffs,coeffs_i );Operater_YCrCb_i(src_ptr, scn, bidx, coeffs, dst_ptr, nWidth*nHeight/(double(1<<16)));src_ptr += 3;dst_ptr += 3;}}}
}

该后的代码去掉了源码中的并行运算部分,该代码没有考虑运算效率。

维基百科中关于rgb->yuv 和rgb->ycbcr的具体介绍为:

详细内容可参考 http://zh.wikipedia.org/wiki/YUV

及http://blog.sina.com.cn/s/blog_4665b35b0100vf6r.html

附整个改写的代码部分:

#ifndef _RGB2YUV_H_
#define _RGB2YUV_H_#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"#include <math.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <float.h>using namespace std;
using namespace cv;#undef R2Y
#undef G2Y
#undef B2Y#define  CV_DESCALE(x,n)     (((x) + (1 << ((n)-1))) >> (n))enum
{yuv_shift = 14,xyz_shift = 12,R2Y = 4899,G2Y = 9617,B2Y = 1868,BLOCK_SIZE = 256
};struct ColorChannel
{typedef float worktype_f;static uchar max() { return std::numeric_limits<uchar>::max(); }static uchar half() { return (uchar)(max()/2 + 1); }
};inline void RGB2YCrCb_i( int blueId, int coeffs[], const int* _coeffs);
inline void Operater_YCrCb_i(unsigned char* src,int srccn, int blueId,  int coeffs[], unsigned char* dst, int n);void CvtColor_YCrCb_i(unsigned char* src, int sstep, unsigned char* dst, int dstep, int nWidth, int nHeight, int nChannel,  int code, int dcn = 0);#endif
#include "rgb2yuv.h"inline void RGB2YCrCb_i(int blueIdx, int coeffs[], const int* _coeffs)
{static const int coeffs0[] = {R2Y, G2Y, B2Y, 11682, 9241};//CV_BGR2YUV对应的系数memcpy(coeffs, _coeffs ? _coeffs : coeffs0, 5*sizeof(coeffs[0]));if(blueIdx==0) std::swap(coeffs[0], coeffs[2]);//对应的code 为CV_BRG2YCrCv时, 将CV_BGR2YUV对应的系数的R2y(4899)与B2y(1868)对应的值互换
}inline void Operater_YCrCb_i(unsigned char* src,int srccn, int blueId,  int coeffs[], unsigned char* dst, int n)
{int scn = srccn, bidx = blueId;int C0 = coeffs[0], C1 = coeffs[1], C2 = coeffs[2], C3 = coeffs[3], C4 = coeffs[4];ColorChannel cc;int delta = cc.half()*(1<<yuv_shift);n *= 3;for (int i=0; i<n; i +=  3,  src += scn){int Y = CV_DESCALE(src[0]*C0 + src[1]*C1 + src[2]*C2, yuv_shift);int Cr = CV_DESCALE((src[bidx^2] - Y)*C3 + delta, yuv_shift);int Cb = CV_DESCALE((src[bidx] - Y)*C4 + delta, yuv_shift);dst[i] = (unsigned char)(Y);dst[i+1] = (unsigned char)(Cr);dst[i+2] = (unsigned char)(Cb);}}void CvtColor_YCrCb_i(unsigned char* src, int sstep, unsigned char* dst, int dstep, int nWidth, int nHeight, int nChannel,  int code, int dcn)
{assert(nWidth >=0 && nHeight >= 0);int scn = nChannel, bidx;unsigned char* src_ptr = NULL;unsigned char* dst_ptr = NULL;switch(code){case CV_BGR2YCrCb: case CV_RGB2YCrCb:case CV_BGR2YUV: case CV_RGB2YUV:assert(scn == 3 || scn == 4);bidx = code == CV_BGR2YCrCb || code == CV_RGB2YUV ? 0 : 2;static const int yuv_i[]= {B2Y, G2Y, R2Y, 8061, 14369};const int* coeffs_i = code == CV_BGR2YCrCb || code == CV_RGB2YCrCb ? 0 : yuv_i;for (int row = 0; row < nHeight; row++){src_ptr = (unsigned char*)(src + row*sstep);dst_ptr = (unsigned char*)(dst + row*dstep);for (int col = 0; col < nWidth; col++){int coeffs[5];RGB2YCrCb_i(bidx, coeffs,coeffs_i );Operater_YCrCb_i(src_ptr, scn, bidx, coeffs, dst_ptr, nWidth*nHeight/(double(1<<16)));src_ptr += 3;dst_ptr += 3;}}}
}

关于opencv中cvtcolor函数的code参数用CV_BGR2YUV与CV_BGR2YCrCb的了解相关推荐

  1. opencv中cvtcolor()函数用法总结(07)

    还是老习惯,分三步走.第一步,功能说明.第二步,结果图显示,第三步,API详解.第四步,代码展示(注释很详细,保证所有有C++基础的人都可以看懂.) 第一步,功能说明:cvtcolor()函数是一个颜 ...

  2. python+opencv中imread函数第二个参数的含义

    = 0: 灰度图 0:三通道彩色图 <0:原图,带alpha通道

  3. opencv的cvtColor函数Lab转RGB源码解析及结果截断处理解决

    cvtColor( InputArray src, OutputArray dst, int code, int dstCn=0 ); 这是opencv中色彩转换函数.当由Lab转RGB时,code ...

  4. 关于Opencv中Filter2D函数的补全方式

    目录 关于Opencv中Filter2D函数的补全方式 环境 验证 C++举例 Python举例 关于Opencv中Filter2D函数的补全方式 环境 OpenCV3.4.16(C++) openc ...

  5. Opencv中直方图函数calcHist

    Opencv中直方图函数calcHist calcHist函数在Opencv中是极难理解的一个函数,一方面是参数说明晦涩难懂,另一方面,说明书给出的实例也不足以令人完全搞清楚该函数的使用方式.最难理解 ...

  6. OpenCV中flip函数实现

    一.flip()函数原型介绍 void cv::flip(InputArray src,OutputArray dst, int flipCode) 各参数含义 src:输入图像. dst:输出图像. ...

  7. OpenCV中initUndistortRectifyMap函数存在bug原因探究

    原文首发于公众号「3D视觉工坊」:OpenCV中initUndistortRectifyMap函数存在bug原因探究. 最近在运行如下一段代码时,生成的mapx和mapy有点异常. 代码片段如下: # ...

  8. 转载:opencv中imshow函数运行中断的解决办法

    OpenCV中 imshow函数运行中断的解决方法 羊和咩咩 2017-03-10 16:00:49 5919 收藏 最后发布:2017-03-10 16:00:49首发:2017-03-10 16: ...

  9. imfilter c语言,opencv中cvFilter2D( ) 函数filter2D()函数与MATLAB中imfilter()函数的差异...

    出处: 1:cvFilter2D() 函数为opencv中c语言函数 2:filter2D()函数为opencv中c++函数 3:imfilter()函数为matlab版本函数 计算结果的异同: 2( ...

最新文章

  1. 源码阅读:SDWebImage(十九)——UIImage+ForceDecode/UIImage+GIF/UIImage+MultiFormat
  2. 全球30篇最热门的计算机视觉和深度学习论文
  3. Spring Ioc 源码分析(一)--Spring Ioc容器的加载
  4. 数学建模十大算法(收藏)
  5. 在Python中操纵json数据的最佳方式
  6. 第三次学JAVA再学不好就吃翔(part93)--LinkedHashMap
  7. .net的页面在大并发下偶尔出现503错误
  8. 电脑怎么打字切换中文_五个练习打字的网站,让你的速度飞起
  9. SQL Server数据库导入导出数据方式比较
  10. 好风凭借力,送我上青云!
  11. android开源系统brvah,Brvah——一个强大的Adapter框架
  12. 樊登讲亲密关系_《亲密关系》
  13. deepin关机卡桌面_有了MyDock,我也有了一个MacBook桌面
  14. php里pluck,Pluck CMS后台另两处任意代码执行
  15. centos使用xfreerdp登录报错
  16. 计算机考试工作表怎么做表格步骤,计算机考试Excel表格中换行的方法
  17. intel linux核显性能,Linux下调节屏幕亮度(Intel核显)
  18. Esrally性能测试步骤与调优
  19. python 论文写作_论文编辑神器Sublime Text,让论文写作过程事半功倍
  20. 影响IT人员未来发展的五个IT新技术方向

热门文章

  1. 代码文档生成工具Doxygen教程及实例
  2. [附源码]java毕业设计社区健康服务平台管理系统lunwen
  3. 使用DelayQueue实现订单限时支付处理
  4. 英语基础-主语、宾语、表语、同位语
  5. Study - Material Design 浅析
  6. 微信小程序接入微信客服【超详细,仅需3步】
  7. 在Linux系统中运行C语言程序
  8. iOS 绘制纯色图片与渐变色图片
  9. C专家编程 第6章 运动的诗章:运行时数据结构 6.1 a.out及其传说
  10. 【人工智能项目】- 深度学习实现猫狗大战