码迷,mamicode.com
首页 > 其他好文 > 详细

Opencv——彩色图像转成灰度图像

时间:2014-08-17 10:28:22      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:photoshop   算法   应用   opencv   

// 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();
}


Opencv——彩色图像转成灰度图像,布布扣,bubuko.com

Opencv——彩色图像转成灰度图像

标签:photoshop   算法   应用   opencv   

原文地址:http://blog.csdn.net/matrix_space/article/details/38122673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!