1,原图:
2.1,怀旧代码:
//10怀旧色 & 连环画 & 熔铸 & 冰冻
void Nostalgic()
{
Mat src = imread("D:\\test\\66.jpg");
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);
}
//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
Nostalgic();
}
怀旧结果:
2.2,连环画代码:
//连环画
void ComicStrip()
{
Mat src = imread("D:\\test\\66.jpg");
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);
}
//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
ComicStrip();
}
连环画结果:
2.3,熔铸&冰冻
//熔铸
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://熔铸.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://冰冻.jpg", dst);
}
void Casting()
{
Mat src = imread("D:\\test\\66.jpg");
imshow("src", src);
casting(src);
freezing(src);
waitKey();
}
//-----开始------
void COpenCVLearningDlg::OnBnClickedStartButton()
{
Casting();
}
熔铸结果:
冰冻结果:
欢迎扫码关注我的微信公众号
原文地址:https://blog.csdn.net/sangni007/column/info/stylizefliter