-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSource (1).cpp
311 lines (256 loc) · 6.79 KB
/
Source (1).cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <cmath>
#include "opencv2/video.hpp"
#include<windows.h>;
using namespace std;
using namespace cv;
int main() {
//------------------------------------------ ej 1a
Mat oriDa = imread("dados.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat chida = imread("cielo.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat chida4 = imread("robot.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat image2;
VideoCapture cap(0);// abre la cámara de default
if (!cap.isOpened())//revisa si se abrió correctamente
return -1;
float b = 0;
float a = 1 - b;
float inc = 1.0f / ((oriDa.cols)*0.5);
//-------------------------------------------------- ej 1a
for (;;) {
Mat frame;
cap >> frame;//obtiene un nuevo cuadro de la cámara
cvtColor(frame, image2, COLOR_BGR2GRAY);
Mat img3(480, 640, CV_8U, Scalar(0));
for (int i = 0; i < frame.rows; i++) {
for (int j = 0; j < frame.cols; j++) {
if (j < image2.cols / 2) {
img3.at<uchar>(i,j) = (1 - b)*image2.at<uchar>(i, j) + (b)*chida.at<uchar>(i, j);
}
else {
img3.at<uchar>(i, j) = chida.at<uchar>(i, j);
}
b = inc * j;
}
}
imshow("frame", img3);
if (waitKey(1) >= 0)
break;
}
// --------------------------------------------------- ej 1b
int arr[256] = { 0 };
float arr2[256] = { 0 };
float arr3[256] = { 0 };
Mat oriDa2 = imread("dados.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat chida2 = imread("cielo.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat image2_2;
VideoCapture cap2(0);// abre la cámara de default
if (!cap2.isOpened())//revisa si se abrió correctamente
return -1;
float b2 = 0;
float a2 = 1 - b;
float inc2 = 1.0f / ((oriDa2.cols)*0.5);
bool showec = false;
for (;;) {
Mat frame;
cap2 >> frame;//obtiene un nuevo cuadro de la cámara
cvtColor(frame, image2_2, COLOR_BGR2GRAY);
Mat img3_2(480, 640, CV_8U, Scalar(0));
for (int i = 0; i < frame.rows; i++) {
for (int j = 0; j < frame.cols; j++) {
if (j < image2.cols / 2) {
img3_2.at<uchar>(i, j) = (1 - b)*image2_2.at<uchar>(i, j) + (b)*chida.at<uchar>(i, j);
}
else {
img3_2.at<uchar>(i, j) = chida.at<uchar>(i, j);
}
b = inc * j;
}
}
// histograma arreglo
for (int i = 0; i < img3_2.rows; i++) {
for (int j = 0; j < img3_2.cols; j++) {
int index = (int)(img3_2.at<uchar>(i, j));
arr[index]++;
}
}
//histograma max
int max = 0;
for (int i = 0; i < 255; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
//histograma img
int alto = 750;
int ancho = 750;
Mat h2(alto, ancho, CV_8U, Scalar(0));
Mat ec(img3_2.rows, img3_2.cols, CV_8U, Scalar(0));
int inc_h2 = ancho / 256;
for (int i = 0; i < 255; i++) {
rectangle(h2, Point(inc_h2 * i, h2.rows), Point((inc_h2*(i + 1) - 1), h2.rows - ((arr[i] * h2.rows) / (max))), Scalar(255, 255, 255, 0), CV_FILLED);
}
//ecualizacion
if (GetKeyState('H') & 0x8000) {
if (!showec) {
showec = true;
float total = img3_2.cols*img3_2.rows;
for (int i = 0; i < 255; i++)
{
arr2[i] = float(arr[i]) / total;
}
arr3[0] = arr2[0];
//-- CDF
for (int i = 1; i < 255; i++)
{
arr3[i] = arr2[i] + arr3[i - 1];
}
for (int i = 0; i < ec.rows; i++) {
for (int j = 0; j < ec.cols; j++) {
ec.at<uchar>(i, j) = floor((256 - 1)*arr3[img3_2.at<uchar>(i, j)]);
}
}
imshow("ecual", ec);
}
else {
}
}
imshow("frame2", img3_2);
imshow("hist", h2);
if (GetKeyState(VK_RETURN) & 0x8000) {
break;
}
if (waitKey(10) >= 0)
continue;
}
//----------------------------ej 2a
Mat check1(480, 640, CV_8U, Scalar(0));
bool hola = true;
Mat image2_3;
VideoCapture cap3(0);// abre la cámara de default
if (!cap3.isOpened())//revisa si se abrió correctamente
return -1;
int f = 0;
bool change = false;
for (;;) {
Mat frame;
f += 1;
if (f % 25 == 0) {
change = !change;
}
cap3 >> frame;//obtiene un nuevo cuadro de la cámara
cvtColor(frame, image2_3, COLOR_BGR2GRAY);
Mat img3_3(480, 640, CV_8U, Scalar(0));
Mat res(480, 640, CV_8U, Scalar(0));
for (int a = 0; a < check1.rows; a++) {
if (a % 80 == 0) {
hola = !hola;
}
for (int j = 0; j < check1.cols; j++) {
if (j % 80 == 0) {
hola = !hola;
}
if (hola) {
check1.at<uchar>(a, j) = (255);
}
else {
check1.at<uchar>(a, j) = (0);
}
}
}
for (int i = 0; i < img3_3.rows; i++) {
for (int j = 0; j < img3_3.cols; j++) {
if (check1.at<uchar>(i, j) == 0 && change == true) {
image2_3.at<uchar>(i, j) = chida.at<uchar>(i, j);
}
else if (check1.at<uchar>(i, j) == 255 && change == false) {
image2_3.at<uchar>(i, j) = chida.at<uchar>(i, j);
}
}
}
imshow("checkered", image2_3);
if (waitKey(1) >= 0)
break;
}
// -------------------------------- ej 2b
Mat check4(480, 640, CV_8U, Scalar(0));
bool hola4 = true;
Mat image2_4;
VideoCapture cap4(0);// abre la cámara de default
if (!cap4.isOpened())//revisa si se abrió correctamente
return -1;
int f4 = 0;
bool change4 = false;
float inc4 = 1.0f / 25.0f;
int b4 = 0;
for (;;) {
Mat frame;
f4 += 1;
if (f4 % 25 == 0) {
change4 = !change4;
}
cap4 >> frame;//obtiene un nuevo cuadro de la cámara
cvtColor(frame, image2_4, COLOR_BGR2GRAY);
Mat img3_4(480, 640, CV_8U, Scalar(0));
Mat res4(480, 640, CV_8U, Scalar(0));
for (int a = 0; a < check4.rows; a++) {
if (a % 80 == 0) {
hola4 = !hola4;
}
for (int j = 0; j < check4.cols; j++) {
if (j % 80 == 0) {
hola4 = !hola4;
}
if (hola4) {
check4.at<uchar>(a, j) = (255);
}
else {
check4.at<uchar>(a, j) = (0);
}
}
}
int p = f % 25;
for (int i = 0; i < img3_4.rows; i++) {
for (int j = 0; j < img3_4.cols; j++) {
if (check4.at<uchar>(i, j) == 0 && change4 == true) {
image2_4.at<uchar>(i, j) = (1-p)*inc4*image2_4.at<uchar>(i, j) + p*inc4*chida4.at<uchar>(i, j);
}
else if (check4.at<uchar>(i, j) == 255 && change4 == false) {
image2_4.at<uchar>(i, j) = (1 - p)*inc4*image2_4.at<uchar>(i,j) + p*inc4*chida4.at<uchar>(i, j);
}
}
}
imshow("checkered", image2_4);
if (waitKey(1) >= 0)
break;
}
}
/*
Mat oriDa = imread("dados.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat masc = imread("dados.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat chida = imread("cielo.jpg", CV_LOAD_IMAGE_GRAYSCALE);
for (int i = 0; i < masc.rows; i++) {
for (int j = 0; j < masc.cols; j++) {
if (masc.at<uchar>(i, j)>200) {
masc.at<uchar>(i, j) = (uchar)(255);
}
else {
masc.at<uchar>(i, j) = (uchar)(0);
}
}
}
for (int i = 0; i < chida.rows; i++) {
for (int j = 0; j < chida.cols; j++) {
if (masc.at<uchar>(i, j) == 255) {
oriDa.at<uchar>(i, j) = chida.at<uchar>(i, j);
}
}
}
//-----
namedWindow("binary", WINDOW_AUTOSIZE);
imshow("binary", oriDa);
waitKey(0);
*/