1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
// drawing-filter.cpp : 素描滤镜
// @mango
#include <iostream>
#include <opencv2/opencv.hpp>
#include<cmath>
int main()
{
cv::Mat img = cv::imread("fruit.jpg");
if (img.empty())
{
std::cout << "Failed to read the image!" << std::endl;
return -1;
}
//1、去色
cv::Mat gray(img.size(), CV_8UC3);
for (size_t i = 0; i < img.rows; i++)
{
for (size_t j = 0; j < img.cols; j++)
{
int max = std::max(
std::max(img.at<cv::Vec3b>(i, j)[0], img.at<cv::Vec3b>(i, j)[1]),
img.at<cv::Vec3b>(i, j)[2]
);
int min = std::min(
std::min(img.at<cv::Vec3b>(i, j)[0], img.at<cv::Vec3b>(i, j)[1]),
img.at<cv::Vec3b>(i, j)[2]
);
for (size_t k = 0; k < 3; k++)
{
gray.at<cv::Vec3b>(i, j)[k] = (max + min) / 2;
}
}
}
//2、复制去色图层,并且反色
cv::Mat gray_revesal(img.size(), CV_8UC3);
for (size_t i = 0; i < gray.rows; i++)
{
for (size_t j = 0; j < gray.cols; j++)
{
for (size_t k = 0; k < 3; k++)
{
gray_revesal.at<cv::Vec3b>(i, j)[k] = 255 - gray.at<cv::Vec3b>(i, j)[k];
}
}
}
//3、对反色图像进行高斯模糊;
cv::GaussianBlur(gray_revesal, gray_revesal, cv::Size(7, 7), 0);
//4、模糊后的图像叠加模式选择颜色减淡效果。
// 减淡公式:C =MIN( A +(A×B)/(255-B),255),其中C为混合结果,A为去色后的像素点,B为高斯模糊后的像素点。
cv::Mat result(gray.size(), CV_8UC3);
for (size_t i = 0; i < gray.rows; i++)
{
for (size_t j = 0; j < gray.cols; j++)
{
for (size_t k = 0; k < 3; k++)
{
int a = gray.at<cv::Vec3b>(i, j)[k];
int b = gray_revesal.at<cv::Vec3b>(i, j)[k];
int c = std::min(a + (a * b) / (255 - b), 255);
result.at<cv::Vec3b>(i, j)[k] = c;
}
}
}
cv::imshow("素描效果", result);
cv::waitKey(0);
return 0;
}
|