• OpenCV——黑白调整


    参考算法:

    闲人阿发伯的博客


    // define head function
    #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;
    
    void Show_Image(Mat&, const string &);
    
    #endif // PS_ALGORITHM_H_INCLUDED
    
    /*
    This program will transform
    the color image to Black-whithe
    image.
    
    */
    
    #include "PS_Algorithm.h"
    #include <time.h>
    
    using namespace std;
    using namespace cv;
    
    int main(void)
    {
        string Img_name("9.jpg");
        Mat Img_in;
        Img_in=imread(Img_name);
        Show_Image(Img_in, Img_name);
    
        Mat Img_out(Img_in.size(), CV_32FC3);
        Img_in.convertTo(Img_out, CV_32FC3);
    
        Mat R(Img_in.size(),CV_32FC1);
        Mat G(Img_in.size(),CV_32FC1);
        Mat B(Img_in.size(),CV_32FC1);
    
        Mat I(Img_in.size(),CV_32FC1);
    
        Mat BW_out(Img_in.size(), CV_32FC1);
    
        Mat rgb[]={B, G, R};
        cv::split(Img_out, rgb);
    
        I=B+G+R;
    
        float maxVal, minVal, midVal;
    
        float color_ratio[6]={0.4,0.6,0.4,0.6,0.2,0.8};
        float r_max_mid, r_max;
        int Ind;
    
        for(int i=0; i<I.rows; i++)
        {
            for(int j=0; j<I.cols; j++)
            {
                maxVal=std::max(R.at<float>(i,j), std::max(G.at<float>(i,j),
                                                           B.at<float>(i,j)));
                minVal=std::min(R.at<float>(i,j), std::min(G.at<float>(i,j),
                                                           B.at<float>(i,j)));
                midVal=I.at<float>(i,j)-maxVal-minVal;
    
                if(minVal==R.at<float>(i,j))
                {
                    Ind=0;
                }
                else if(minVal==G.at<float>(i,j))
                {
                    Ind=2;
                }
                else
                {
                    Ind=4;
                }
                r_max_mid=color_ratio[(Ind+3)%6+1];
    
                if(maxVal==R.at<float>(i,j))
                {
                    Ind=1;
                }
                else if(maxVal==G.at<float>(i,j))
                {
                    Ind=3;
                }
                else
                {
                    Ind=5;
                }
    
                r_max=color_ratio[Ind];
    
                BW_out.at<float>(i,j)=(maxVal-midVal)*r_max+(midVal-minVal)
                          *r_max_mid+minVal;
    
            }
        }
    
        BW_out=BW_out/255;
        Show_Image(BW_out, "out");
    
        imwrite("out.jpg", BW_out*255);
    
        waitKey();
        cout<<"All is well."<<endl;
    
    }
    
    
    // define the show image
    #include "PS_Algorithm.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    using namespace cv;
    
    void Show_Image(Mat& Image, const string& str)
    {
        namedWindow(str.c_str(),CV_WINDOW_AUTOSIZE);
        imshow(str.c_str(), Image);
    
    }
    


    原图 


    效果图


  • 相关阅读:
    Apache与Nginx的优缺点比较
    [PHP基础]有关isset empty 函数的面试题
    PHP求解一个值是否为质数
    15个魔术方法的总结
    对象在类中的存储方式有哪些?
    cookie大小
    Tp3.2 和 Tp5.0之间的区别
    经典的面试题,(这是著名的约瑟夫环问题)
    怎么计算数据库有多大的数据量
    [置顶] 实用电子电路设计丛书
  • 原文地址:https://www.cnblogs.com/mtcnn/p/9412600.html
Copyright © 2020-2023  润新知