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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// ps-diffusion-filter.cpp : ps扩散特效
// @mango
// https://mangoroom.cn
#include <iostream>
#include<random>
#include<opencv2/opencv.hpp>
int main()
{
cv::Mat img = cv::imread("fruit.jpg");
if (img.empty())
{
std::cout << "failed to read the image!" << std::endl;
return -1;
}
// random engine | 随机数引擎
std::default_random_engine generator;
std::uniform_int_distribution<int> dis(1, 8);
// result
cv::Mat result(img.size(), CV_8UC3);
for (size_t i = 1; i < img.rows - 1; i++)
{
for (size_t j = 1; j < img.cols - 1; j++)
{
// generator random | 产生随机数
int r = dis(generator);
// 1 2 3
// 4 p 5
// 6 7 8
switch (r)
{
case 1:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i - 1, j - 1)[k];
}
break;
case 2:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i - 1, j)[k];
}
break;
case 3:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i - 1, j + 1)[k];
}
break;
case 4:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i, j - 1)[k];
}
break;
case 5:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i, j + 1)[k];
}
break;
case 6:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i + 1, j - 1)[k];
}
break;
case 7:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i + 1, j)[k];
}
break;
case 8:
for (size_t k = 0; k < 3; k++)
{
result.at<cv::Vec3b>(i, j)[k] = img.at<cv::Vec3b>(i + 1, j + 1)[k];
}
break;
default:
assert(false);
break;
}
}
}
cv::imshow("PS扩散特效", result);
cv::waitKey(0);
return 0;
}
|