博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenCV——彩色图像转成灰度图像
阅读量:4590 次
发布时间:2019-06-09

本文共 1362 字,大约阅读时间需要 4 分钟。

// PS_Algorithm.h

#ifndef PS_ALGORITHM_H_INCLUDED

#define PS_ALGORITHM_H_INCLUDED

#include <iostream>
#include <string>
#include "cv.h"
#include "highgui.h"
#include "cxmat.hpp"

#include "cxcore.hpp"

using namespace std;
using namespace cv;

#endif // PS_ALGORITHM_H_INCLUDED

/*

The program will transfor the color
image to the gray image.
The image must be color image.
*/
#include "PS_Algorithm.h"
int main()
{
    string  Image_name("2.jpg");
    Mat Image=imread(Image_name.c_str());
    Mat Image_test(Image.size(),CV_32FC3);
    Image.convertTo(Image_test, CV_32FC3);
    Mat Gray_img(Image_test.size(), CV_32FC1);
    Mat r,g,b;
    Gray_img.copyTo(r);
    g=r;
    b=r;
    Mat bgr[]={b,g,r};
    split(Image_test, bgr);
    b=bgr[0];
    g=bgr[1];
    r=bgr[2];
    Mat I1,I2,I3;
    I1=r;
    I2=r;
    I3=r;
    // I=0.299*R+0.587*G+0.144*B   方案一
    Gray_img=(0.299*r+0.587*g+0.144*b);
    I1=Gray_img/255;
    imshow("I1", I1);
    // I=(R+G+B)/3  方案二
    Gray_img=(0.333*r+0.333*g+0.333*b);
    I2=Gray_img/255;
    for(int i=100; i<105; i++)
        for(int j=100; j<105; j++)
            cout<<I2.at<float>(i,j)<<endl;
    imshow ("I2", I2);
    // I=max(R,G,B)   方案三
    float *p1,*p2,*p3,*p;
    p1=r.ptr<float>(0);
    p2=g.ptr<float>(0);
    p3=b.ptr<float>(0);
    p=I3.ptr<float>(0);
    int nums;
    nums=Gray_img.rows*Gray_img.cols;
    for (int i=0; i<nums; i++)
        p[i]=max(p1[i],max(p2[i],p3[i]))/255;
    imshow("I3",I3);
    cout<<"All is well."<<endl;
    waitKey();
}

转载于:https://www.cnblogs.com/mtcnn/p/9412693.html

你可能感兴趣的文章
Css进阶
查看>>
SQL在工作中遇到的问题
查看>>
在电脑CMD中通过pip安装完部分文件后PyCharm仍无法使用的解决方法
查看>>
笨方法学Python3(21-44)
查看>>
笨方法学python3
查看>>
Linux for Matlab中文注释乱码(亲测有效)
查看>>
RK3399Pro Android Rock-X 人工智能开发系列(1)
查看>>
RK3399Pro Android Rock-X 人工智能开发系列(2)
查看>>
pxe批量装机
查看>>
linux典型应用对系统资源使用的特点
查看>>
linux性能分析工具Procs
查看>>
linux性能分析工具Vmstat
查看>>
linux性能分析工具Memory
查看>>
linux性能分析工具Uptime
查看>>
linux性能分析工具Cpu
查看>>
linux性能分析工具Ntop
查看>>
linux性能分析工具Sysstat
查看>>
linux改变内核参数
查看>>
Linux系统平台调优
查看>>
linux内存子系统调优
查看>>