-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnow.cpp
More file actions
93 lines (77 loc) · 2.02 KB
/
Snow.cpp
File metadata and controls
93 lines (77 loc) · 2.02 KB
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
#include "OpenCV_example.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//为了多重导入cpp文件,只是将main函数换了个名字
int run137()
{
Mat image;
// 载入图形文件
image = imread("1.jpg", IMREAD_COLOR);
// 检查读取文件是否成功
if (!image.data)
{
cout << "无法打开或找不到图形文件1.jpg" << std::endl;
return -1;
}
// 建立显示图档窗口
namedWindow("原图", WINDOW_NORMAL);
namedWindow("下雪图", WINDOW_NORMAL);
imshow("原图", image);
// 雪点数
int i = 600;
for (int k = 0; k < i; k++) {
// rand() is the MFC random number generator
// try qrand() with Qt
// 随机产生点
int i = rand() % image.cols;
int j = rand() % image.rows;
// 如果是灰度图
if (image.channels() == 1) { // gray-level image
//当前点为白色
image.at<uchar>(j, i) = 255;
// 右点为白色
if (j < (int)image.cols-1)
image.at<uchar>(j + 1, i) = 255;
// 下点为白色
if (i < (int)image.rows-1)
image.at<uchar>(j, i + 1) = 255;
// 右下点为白色
if (i < (int)image.cols-1 && j < (int)image.rows-1)
image.at<uchar>(j + 1, i + 1) = 255;
}
//如果是彩色图
else if (image.channels() == 3) { // color image
// 三通道的颜色改为白色
image.at<cv::Vec3b>(j, i)[0] = 255;
image.at<cv::Vec3b>(j, i)[1] = 255;
image.at<cv::Vec3b>(j, i)[2] = 255;
if (i < (int)image.cols - 1)
{
image.at<cv::Vec3b>(j, i + 1)[0] = 255;
image.at<cv::Vec3b>(j, i + 1)[1] = 255;
image.at<cv::Vec3b>(j, i + 1)[2] = 255;
}
if (j < (int)image.rows - 1)
{
image.at<cv::Vec3b>(j + 1, i)[0] = 255;
image.at<cv::Vec3b>(j + 1, i)[1] = 255;
image.at<cv::Vec3b>(j + 1, i)[2] = 255;
}
if (j < (int)image.rows - 1 && i < (int)image.cols - 1)
{
image.at<cv::Vec3b>(j + 1, i + 1)[0] = 255;
image.at<cv::Vec3b>(j + 1, i + 1)[1] = 255;
image.at<cv::Vec3b>(j + 1, i + 1)[2] = 255;
}
}
}
// 在窗口内显示图形文件
imshow("下雪图", image);
// 窗口等待按键
waitKey(0);
return 0;
}
int (*run_snow)() =&run137;