学习OpenCV:滤镜系列(10)——怀旧色 & 连环画 & 熔铸 & 冰冻

来源:互联网 发布:数据库中的mvcc 编辑:程序博客网 时间:2024/06/02 12:16

==============================================

版权所有:小熊不去实验室CSDN博客

==============================================


怀旧色调色原理



#include <opencv/cv.h>#include <opencv/highgui.h>using namespace cv;using namespace std;int main(){Mat src = imread("D:/scene04.jpg",1);int width=src.cols;int heigh=src.rows;RNG rng;Mat img(src.size(),CV_8UC3);for (int y=0; y<heigh; y++){uchar* P0  = src.ptr<uchar>(y);uchar* P1  = img.ptr<uchar>(y);for (int x=0; x<width; x++){float B=P0[3*x];float G=P0[3*x+1];float R=P0[3*x+2];float newB=0.272*R+0.534*G+0.131*B;float newG=0.349*R+0.686*G+0.168*B;float newR=0.393*R+0.769*G+0.189*B;if(newB<0)newB=0;if(newB>255)newB=255;if(newG<0)newG=0;if(newG>255)newG=255;if(newR<0)newR=0;if(newR>255)newR=255;P1[3*x] = (uchar)newB;P1[3*x+1] = (uchar)newG;P1[3*x+2] = (uchar)newR;}}imshow("怀旧色",img);waitKey();imwrite("D:/怀旧色.jpg",img);}

原图:



怀旧色:





连环画原理:

连环画的效果与图像灰度化后的效果相似,它们都是灰度图,但连环画增大了图像的对比度,使整体明暗效果更强.
算法:

R = |g – b + g + r| * r / 256

G = |b – g + b + r| * r / 256;

B = |b – g + b + r| * g / 256;

#include <math.h>#include <opencv/cv.h>#include <opencv/highgui.h>using namespace cv;using namespace std;int main(){Mat src = imread("D:/scene04.jpg",1);int width=src.cols;int heigh=src.rows;RNG rng;Mat img(src.size(),CV_8UC3);for (int y=0; y<heigh; y++){uchar* P0  = src.ptr<uchar>(y);uchar* P1  = img.ptr<uchar>(y);for (int x=0; x<width; x++){float B=P0[3*x];float G=P0[3*x+1];float R=P0[3*x+2];float newB=abs(B-G+B+R)*G/256;float newG=abs(B-G+B+R)*R/256;float newR=abs(G-B+G+R)*R/256;if(newB<0)newB=0;if(newB>255)newB=255;if(newG<0)newG=0;if(newG>255)newG=255;if(newR<0)newR=0;if(newR>255)newR=255;P1[3*x] = (uchar)newB;P1[3*x+1] = (uchar)newG;P1[3*x+2] = (uchar)newR;}}Mat gray;cvtColor(img,gray,CV_BGR2GRAY);normalize(gray,gray,255,0,CV_MINMAX);imshow("连环画",gray);waitKey();imwrite("D:/连环画.jpg",gray);}

连环画效果:




熔铸算法

r = r*128/(g+b +1);
g = g*128/(r+b +1);
b = b*128/(g+r +1);

冰冻算法

r = (r-g-b)*3/2;
g = (g-r-b)*3/2;
b = (b-g-r)*3/2;


#include <math.h>#include <opencv/cv.h>#include <opencv/highgui.h>#define MAXSIZE (32768)using namespace cv;using namespace std;void casting(const Mat& src){Mat img;src.copyTo(img);int width=src.cols;int heigh=src.rows;Mat dst(img.size(),CV_8UC3);for (int y=0;y<heigh;y++){uchar* imgP=img.ptr<uchar>(y);uchar* dstP=dst.ptr<uchar>(y);for (int x=0;x<width;x++){float b0=imgP[3*x];float g0=imgP[3*x+1];float r0=imgP[3*x+2];float b = b0*255/(g0+r0+1);float g = g0*255/(b0+r0+1);float r = r0*255/(g0+b0+1);r = (r>255 ? 255 : (r<0? 0 : r));g = (g>255 ? 255 : (g<0? 0 : g));b = (b>255 ? 255 : (b<0? 0 : b));dstP[3*x] = (uchar)b;dstP[3*x+1] = (uchar)g;dstP[3*x+2] = (uchar)r;}}imshow("熔铸",dst);imwrite("D:/img/熔铸.jpg",dst);}void freezing(const Mat& src){Mat img;src.copyTo(img);int width=src.cols;int heigh=src.rows;Mat dst(img.size(),CV_8UC3);for (int y=0;y<heigh;y++){uchar* imgP=img.ptr<uchar>(y);uchar* dstP=dst.ptr<uchar>(y);for (int x=0;x<width;x++){float b0=imgP[3*x];float g0=imgP[3*x+1];float r0=imgP[3*x+2];float b = (b0-g0-r0)*3/2;float g = (g0-b0-r0)*3/2;float r = (r0-g0-b0)*3/2;r = (r>255 ? 255 : (r<0? -r : r));g = (g>255 ? 255 : (g<0? -g : g));b = (b>255 ? 255 : (b<0? -b : b));// r = (r>255 ? 255 : (r<0? 0 : r));// g = (g>255 ? 255 : (g<0? 0 : g));// b = (b>255 ? 255 : (b<0? 0 : b));dstP[3*x] = (uchar)b;dstP[3*x+1] = (uchar)g;dstP[3*x+2] = (uchar)r;}}imwrite("D:/img/冰冻.jpg",dst);}int main(){Mat src = imread("D:/img/scene04.jpg",1);imshow("src",src);casting(src);freezing(src);waitKey();}


熔铸:



冰冻: