OpenCV——颜色运算

Posted lytwajue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV——颜色运算相关的知识,希望对你有一定的参考价值。

#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 &);

void Save_Image(Mat&, const string &);

#endif // PS_ALGORITHM_H_INCLUDED



/*
This program will do the computation
on three channels respectively, and generate
the "cast" effect.

*/

#include "PS_Algorithm.h"

using namespace std;
using namespace cv;

int main(void)
{

    string Img_name("4.jpg");

    Mat Image_in;
    Image_in=imread(Img_name);
    Show_Image(Image_in, Img_name);

    // split the three channels
    Mat Image_2(Image_in.size(), CV_32FC3);
    Image_in.convertTo( Image_2, CV_32FC3);

    Mat r(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat g(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat b(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat out[]={b, g, r};

    split(Image_2, out);

    b=out[0]/255;
    g=out[1]/255;
    r=out[2]/255;

    // generate the new image
    Mat Image_new(Image_in.size(), CV_32FC3);
    Image_in.convertTo( Image_new, CV_32FC3);

    // generate new channels
    Mat r_new(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat g_new(Image_in.rows, Image_in.cols, CV_32FC1);
    Mat b_new(Image_in.rows, Image_in.cols, CV_32FC1);

    float alpha;
    alpha=0.15;
    // cv::divide(r,(b+g+0.001),r_new, alpha);
    // cv::divide(g,(b+r+0.001),g_new, alpha);
    // cv::divide(b,(r+g+0.001),b_new, alpha);

    r_new=r*alpha/(b+g+0.001);
    g_new=g*alpha/(r+b+0.001);
    b_new=b*alpha/(r+g+0.001);

    Mat out_new[]={b_new, g_new, r_new};
    cv::merge(out_new, 3, Image_new);
    Show_Image(Image_new, "Img_out");
    waitKey();

    cout<<"All is well \n";

}

</pre><pre name="code" class="cpp">#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);
    // waitKey();
    // cvDestroyWindow(str.c_str());
}





原图: 

技术分享


效果图:

技术分享












以上是关于OpenCV——颜色运算的主要内容,如果未能解决你的问题,请参考以下文章

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

Python 图像处理 OpenCV :图像处理形态学开运算闭运算以及梯度运算

OpenCV-Python基础教程4-颜色空间转换

OpenCV常用操作函数大全

opencv1

opencv1