opencv求取RGB分量

需要注意的是下面r,b,g的类型和顺序

须用IPL_DEPTH_8U类型创建图像且[0][1][2]分量分别是b,g,r.

另外多谢郑乾师兄帮我发现了IPL_DEPTH_8U问题

[cpp] view plain copy
 
  1. uchar r,b,g;//notice 'uchar' type must be used instead of double   
  2. int h=workImg->height;  
  3. int w=workImg->width;  
  4. IplImage *D=cvCreateImage(cvGetSize(workImg),IPL_DEPTH_8U,workImg->nChannels);   
  5.   
  6. CvScalar s,s1;  
  7.   
  8. for(int x=0;x<workImg->height;x++)  
  9. {  
  10.     for(int y=0;y<workImg->width;y++)  
  11.     {  
  12.   
  13.         b=((uchar*)(workImg->imageData+x*workImg->widthStep))[y*3+0];  
  14.         g=((uchar*)(workImg->imageData+x*workImg->widthStep))[y*3+1];  
  15.         r=((uchar*)(workImg->imageData+x*workImg->widthStep))[y*3+2];  
  16.     }  
  17. }  

若用IPL_DEPTH_64U类型赋值会导致图片出现如下情况:

这里大家一定要小心哦~

 
 
from: http://blog.csdn.net/abcjennifer/article/details/7385672
原文地址:https://www.cnblogs.com/GarfieldEr007/p/5374026.html