OpenCV——PS 图层混合算法 (三)
2014-07-17 20:17
351 查看
具体的算法原理可以参考
// 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
// main function
#include "PS_Algorithm.h"
void Screen(Mat& src1, Mat& src2, Mat& dst);
void Add_Color(Mat& src1, Mat& src2, Mat& dst);
void Soft_Lighten(Mat& src1, Mat& src2, Mat& dst);
void Strong_Lighten(Mat& src1, Mat& src2, Mat& dst);
int main(void)
{
Mat Origin_Image1;
Mat Origin_Image2;
Origin_Image1=imread("2.jpg");
Origin_Image2=imread("3.jpg");
Mat Image_up(Origin_Image1.size(),CV_32FC3);
Mat Image_down(Origin_Image2.size(), CV_32FC3);
Origin_Image1.convertTo(Image_up,CV_32FC3);
Origin_Image2.convertTo(Image_down,CV_32FC3);
Image_up=Image_up/255;
Image_down=Image_down/255;
Mat Image_mix(Image_up);
//Screen(Image_up, Image_down, Image_mix);
//Add_Color(Image_up, Image_down, Image_mix);
//Soft_Lighten(Image_up, Image_down, Image_mix);
//Strong_Lighten(Image_up, Image_down, Image_mix);
namedWindow("Img", CV_WINDOW_AUTOSIZE);
imshow("Img",Image_mix);
waitKey();
cvDestroyWindow("Img");
cout<<"All is well."<<endl;
return 0;
}
// Screen
void Screen(Mat& src1, Mat& src2, Mat& dst)
{
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
dst.at<Vec3f>(index_row, index_col)[index_c]=1-
(1-src1.at<Vec3f>(index_row, index_col)[index_c])*
(1-src2.at<Vec3f>(index_row, index_col)[index_c]);
}
}
}
// Add color
void Add_Color(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(b>0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=2*a*b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=1-2*(1-a)*(1-b);
}
}
}
}
}
// Soft Lighten
void Soft_Lighten(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(a<=0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=(2*a-1)*(b-b*b)+b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=(2*a-1)*(sqrt(b)-b)+b;
}
}
}
}
}
// Strong Lighten
void Strong_Lighten(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(a<=0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=2*a*b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=1-2*(1-a)*(1-b);
}
}
}
}
}
PS图层混合算法之三(滤色, 叠加, 柔光, 强光)
#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
// main function
#include "PS_Algorithm.h"
void Screen(Mat& src1, Mat& src2, Mat& dst);
void Add_Color(Mat& src1, Mat& src2, Mat& dst);
void Soft_Lighten(Mat& src1, Mat& src2, Mat& dst);
void Strong_Lighten(Mat& src1, Mat& src2, Mat& dst);
int main(void)
{
Mat Origin_Image1;
Mat Origin_Image2;
Origin_Image1=imread("2.jpg");
Origin_Image2=imread("3.jpg");
Mat Image_up(Origin_Image1.size(),CV_32FC3);
Mat Image_down(Origin_Image2.size(), CV_32FC3);
Origin_Image1.convertTo(Image_up,CV_32FC3);
Origin_Image2.convertTo(Image_down,CV_32FC3);
Image_up=Image_up/255;
Image_down=Image_down/255;
Mat Image_mix(Image_up);
//Screen(Image_up, Image_down, Image_mix);
//Add_Color(Image_up, Image_down, Image_mix);
//Soft_Lighten(Image_up, Image_down, Image_mix);
//Strong_Lighten(Image_up, Image_down, Image_mix);
namedWindow("Img", CV_WINDOW_AUTOSIZE);
imshow("Img",Image_mix);
waitKey();
cvDestroyWindow("Img");
cout<<"All is well."<<endl;
return 0;
}
// Screen
void Screen(Mat& src1, Mat& src2, Mat& dst)
{
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
dst.at<Vec3f>(index_row, index_col)[index_c]=1-
(1-src1.at<Vec3f>(index_row, index_col)[index_c])*
(1-src2.at<Vec3f>(index_row, index_col)[index_c]);
}
}
}
// Add color
void Add_Color(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(b>0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=2*a*b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=1-2*(1-a)*(1-b);
}
}
}
}
}
// Soft Lighten
void Soft_Lighten(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(a<=0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=(2*a-1)*(b-b*b)+b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=(2*a-1)*(sqrt(b)-b)+b;
}
}
}
}
}
// Strong Lighten
void Strong_Lighten(Mat& src1, Mat& src2, Mat& dst)
{
float a=0;
float b=0;
for(int index_row=0; index_row<src1.rows; index_row++)
{
for(int index_col=0; index_col<src1.cols; index_col++)
{
for(int index_c=0; index_c<3; index_c++)
{
a=src1.at<Vec3f>(index_row, index_col)[index_c];
b=src2.at<Vec3f>(index_row, index_col)[index_c];
if(a<=0.5)
{
dst.at<Vec3f>(index_row, index_col)[index_c]=2*a*b;
}
else
{
dst.at<Vec3f>(index_row, index_col)[index_c]=1-2*(1-a)*(1-b);
}
}
}
}
}
相关文章推荐
- OpenCV——PS 图层混合算法(一)
- OpenCV——PS 图层混合算法 (三)
- OpenCV——PS 图层混合算法 (二)
- OpenCV——PS 图层混合算法(一)
- OpenCV——PS 图层混合算法 (二)
- OpenCV——PS 图层混合算法 (二)
- OpenCV——PS图层混合算法(六)
- OpenCV——PS图层混合算法(六)
- OpenCV——PS 图层混合算法 (四)
- OpenCV——PS 图层混合算法(一)
- OpenCV——PS 图层混合算法 (四)
- ps中图层混合模式算法公式
- 如何使用OPENCV实现两张图片的混合(PS中的图层不透明度)
- PS图层混合算法之二(线性加深,线性减淡,变亮,变暗)
- PS图层混合算法之六(差值,溶解, 排除)
- PS图层混合算法之三(滤色, 叠加, 柔光, 强光)
- PS图层混合算法之六(差值,溶解, 排除)
- PS图层混合算法之三(滤色, 叠加, 柔光, 强光)
- PS图层混合算法之四(亮光, 点光, 线性光, 实色混合)
- PS图层混合算法之五(饱和度,色相,颜色,亮度)