zoukankan      html  css  js  c++  java
  • 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();
    }


  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9412693.html
Copyright © 2011-2022 走看看