rokevin
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
  • 客户端

    • Android
    • IOS
  • 混合开发

    • Flutter
    • weex
    • react-native
    • uni-app
  • 系统OS

    • 鸿蒙
    • fuchsia

OpenCV

为了加快处理速度在图像处理算法中,往往需要把彩色图像转换为灰度图像。24为彩色图像每个像素用3个字节表示,每个字节对应着RGB分量的亮度。

当RGB分量值不同时,表现为彩色图像;当RGB分量相同时,变现为灰度图像:

一般来说,转换公式有3中。

(1)Gray(i,j)=[R(i,j)+G(i,j)+B(i,j)]/3;

(2)Gray(i,j)=0.299*R(i,j)+0.587*G(i,j)+0.144*B(i,j);

(3)Gray(i,j)=G(i,j);//从2可以看出G的分量比较大所以可以直接用它代替

代码

 1 #include<cv.h>
 2 #include<highgui.h>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     IplImage *img=cvLoadImage("D:\\Opencv\\Images\\lena.jpg");
10     IplImage* dest=cvCreateImage(cvGetSize(img),img->depth,1);
11     cvNamedWindow("SourceImage");
12     cvNamedWindow("destinationImage");
13 
14     uchar* data=(uchar*)img->imageData;
15 
16     int step = img->widthStep/sizeof(uchar);     //step即为上图的widthstep
17     int channels = img->nChannels;            // 这个图片为3通道的
18     uchar    b=0,g=0,r=0;
19     for(int i=0;i<img->height;i++)
20       for(int j=0;j<img->width;j++){
21            b=data[i*step+j*channels+0];   // 此时可以通过更改bgr的值达到访问效果。
22            g=data[i*step+j*channels+1];
23            r=data[i*step+j*channels+2];
24            // ((char*)(dest->imageData + j*dest->widthStep))[i] =(b+r+g)/3;//(1)
25            //((char*)(dest->imageData + j*dest->widthStep))[i] =r*0.299+g*0.587+0.144*b;//(2)
26            ((char*)(dest->imageData + j*dest->widthStep))[i] =g;//(3)
27       }
28 
29       cvShowImage("SourceImage",img);
30       cvShowImage("destinationImage",dest);
31       cvWaitKey();
32       cvDestroyAllWindows();
33       cvReleaseImage(&img);
34       cvReleaseImage(&dest);
35     //i*step     当i=0 即为上图的第一行   为1就是第二行  
36     //j*chanels+0  j*通道数当j=0为第一列的第0个通道->b   
37     //j*channels+1         当j=1为第二列的第1个通道->g
38 }

https://www.cnblogs.com/BasilLee/p/3818217.html

最近更新:: 2020/8/9 20:29
Contributors: luokaiwen