-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoughChange.cpp.bak
498 lines (464 loc) · 12.2 KB
/
HoughChange.cpp.bak
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// HoughChange.cpp : 定义控制台应用程序的入口点。
//
/*
#include "stdafx.h"
#include<opencv2\opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(void)
{
IplImage *src = cvLoadImage("F:\\vs数字图像处理典型算法\\Hough变换\\翻牌状态.jpg",0);
if (src)
{
IplImage *dst = cvCreateImage(cvGetSize(src),8,1);
IplImage *color_dst = cvCreateImage(cvGetSize(src),8,3);
CvMemStorage *storage = cvCreateMemStorage();
CvSeq *lines = 0;
cvCanny(src,dst,50,100,3);
cvCvtColor(dst,color_dst,CV_GRAY2BGR);
#if 0
lines = cvHoughLines2(dst,storage,CV_HOUGH_STANDARD,1,CV_PI/180,150,0,0);
for (int i=0;i<lines->total;i++)
{
float *line = (float *)cvGetSeqElem(lines,i);
float rho = line[0]; //像素
float theta = line[1]; //弧度
CvPoint pt1,pt2;
double a = cos(theta);
double b = sin(theta);
if (fabs(a)<0.001)
{
pt1.x = pt2.x = cvRound(rho);
pt1.y = 0;
pt2.y = color_dst->height;
}
else if (fabs(b)<0.001)
{
pt1.y = pt2.y = cvRound(rho);
pt1.x = 0;
pt2.x = color_dst->width;
}
else
{
pt1.x = 0;
pt1.y = cvRound(rho/b);
pt2.x = cvRound(rho/a);
pt2.y = 0;
}
cvLine(color_dst,pt1,pt2,CV_RGB(255,0,0),1,8);
}
#else
lines = cvHoughLines2(dst,storage,CV_HOUGH_PROBABILISTIC,1,CV_PI/180,60,30,5);
for (int i=0;i<lines->total;i++)
{
CvPoint *line = (CvPoint *)cvGetSeqElem(lines,i);
cvLine(color_dst,line[0],line[1],CV_RGB(255,0,0),1,CV_AA);
}
#endif
cvNamedWindow("Source");
cvShowImage("Source",src);
cvNamedWindow("Hough");
cvShowImage("Hough",color_dst);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseImage(&color_dst);
cvReleaseMemStorage(&storage);
cvDestroyAllWindows();
return 1;
}
} */
//#include <opencv2/opencv.hpp>
#include <stack>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
//#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
//angle函数用来返回(两个向量之间找到角度的余弦值)
double angle(CvPoint *pt1, CvPoint *pt2, CvPoint *pt0)
{
double dx1 = pt1->x - pt0->x;
double dy1 = pt1->y - pt0->y;
double dx2 = pt2->x - pt0->x;
double dy2 = pt2->y - pt0->y;
return (dx1 * dx2 + dy1 * dy2) / sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10);
}
//drawSquares函数用来画出在图像中找到的所有正方形轮廓
void drawSquares(IplImage *img, CvSeq *squares)
{
CvSeqReader reader;
IplImage *cpy = cvCloneImage(img);
int i;
cvStartReadSeq(squares, &reader, 0);
// read 4 sequence elements at a time (all vertices of a square)
for (i = 0; i < squares->total; i += 4)
{
CvPoint pt[4], *rect = pt;
int count = 4;
// read 4 vertices
CV_READ_SEQ_ELEM(pt[0], reader);
CV_READ_SEQ_ELEM(pt[1], reader);
CV_READ_SEQ_ELEM(pt[2], reader);
CV_READ_SEQ_ELEM(pt[3], reader);
// draw the square as a closed polyline
cvPolyLine(cpy, &rect, &count, 1, 1, CV_RGB(255, 0, 0), 2, CV_AA, 0);
}
cvShowImage("xx", cpy);
/*const char* filename = "111111111111111111111.jpg";
const CvArr* image = cpy;
int cvSaveImage("1111111111111111111111.jpg",image);*/
char *filename2 = "G:\\yy2.bmp"; //图像名
cvSaveImage(filename2, cpy); //把图像写入文件
//Mat mtx(cpy);
cvReleaseImage(&cpy);
}
int main(int argc, char **args)
{
IplImage *src = cvLoadImage(args[1], 1); //2222222
if (src)
{
IplImage *dst = cvCreateImage(cvGetSize(src), 8, 1);
IplImage *dst_color = cvCreateImage(cvGetSize(src), 8, 3);
CvMemStorage *storage = cvCreateMemStorage();
CvSeq *lines = 0;
IplImage *gray = cvCreateImage(cvGetSize(src), 8, 1);
IplImage *gray2 = cvCreateImage(cvGetSize(src), 8, 1);
IplImage *hsv = cvCreateImage(cvGetSize(src), 8, 3);
cvCvtColor(src, gray, CV_RGB2GRAY);
cvCvtColor(src, hsv, CV_BGR2HSV);
cvNamedWindow("灰度变换图");
cvNamedWindow("灰度变换图2");
cvShowImage("灰度变换图", gray);
cvNamedWindow("hsv");
cvShowImage("hsv", hsv);
CvSeq *contours = NULL;
CvSeq *result = NULL;
double s, t;
// 创建一个空序列用于存储轮廓角点
CvSeq *squares = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage);
for (int qq = 0; qq < 256; qq++)
{
if (104 == qq)
{
int a = 0;
}
else
{
// continue;
}
CvScalar cs, hs; //声明像素变量
for (int i = 0; i < gray->height; i++)
{
for (int j = 0; j < gray->width; j++)
{
hs = cvGet2D(hsv, i, j);
cs = cvGet2D(gray, i, j); //获取像素
if (cs.val[0] != qq)
{
cs.val[0] = 0;
}
else
{
cs.val[0] = 255;
}
cvSet2D(gray2, i, j, cs); //将改变的像素保存到图片中
}
}
cvCanny(gray2, dst, 50, 100, 3);
cvShowImage("灰度变换图2", dst);
//cvCvtColor(dst, dst_color, CV_GRAY2RGB);
/*lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI / 90, 50, 30, 3);
for (int i = 0;i<lines->total;i++)
{
CvPoint *line = (CvPoint *)cvGetSeqElem(lines, i);
cvLine(dst_color, line[0], line[1], CV_RGB(255, 0, 0), 1, CV_AA);
}*/
// 找到所有轮廓并且存储在序列中
cvFindContours(dst, storage, &contours, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
// 遍历找到的每个轮廓contours
while (contours)
{
//用指定精度逼近多边形曲线
result = cvApproxPoly(contours, sizeof(CvContour), storage,
CV_POLY_APPROX_DP, cvContourPerimeter(contours) * 0.02, 0);
if (result->total == 4 &&
fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 500 &&
fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 100000 &&
cvCheckContourConvexity(result))
{
s = 0;
int i = 0;
for (i = 0; i < 5; i++)
{
// find minimum angle between joint edges (maximum of cosine)
if (i >= 2)
{
t = fabs(angle(
(CvPoint *)cvGetSeqElem(result, i),
(CvPoint *)cvGetSeqElem(result, i - 2),
(CvPoint *)cvGetSeqElem(result, i - 1)));
s = s > t ? s : t;
}
}
// if 余弦值 足够小,可以认定角度为90度直角
//cos0.1=83度,能较好的趋近直角
if (s < 0.1)
for (i = 0; i < 4; i++)
cvSeqPush(squares,
(CvPoint *)cvGetSeqElem(result, i));
}
// 继续查找下一个轮廓
contours = contours->h_next;
}
}
drawSquares(src, squares);
cvNamedWindow("原图");
cvNamedWindow("Hough变换图");
cvShowImage("原图", src);
cvShowImage("Hough变换图", dst_color);
cvWaitKey(0);
cvReleaseImage(&src);
cvReleaseImage(&dst);
cvReleaseImage(&dst_color);
}
cvDestroyAllWindows();
return 0;
}
//
//
//#include "cv.h"
//#include "highgui.h"
//
//#include<opencv2/core/core.hpp>
//#include<opencv2/highgui/highgui.hpp>
//#include<opencv2/imgproc/imgproc.hpp>
//#include<opencv2/opencv.hpp>
//#include <stdio.h>
//#include <math.h>
//#include <string.h>
//#include <iostream>
//#include <fstream>
//#include <sstream>
//#include <io.h>
//#include <atlbase.h>
//
//
//
//
//using namespace std;
//using namespace cv;
//
//
//
//int thresh = 50;
//IplImage* img = NULL;
//IplImage* img0 = NULL;
//CvMemStorage* storage = NULL;
//const char * wndname = "正方形检测 demo";
//
////angle函数用来返回(两个向量之间找到角度的余弦值)
//double angle(CvPoint* pt1, CvPoint* pt2, CvPoint* pt0)
//{
// double dx1 = pt1->x - pt0->x;
// double dy1 = pt1->y - pt0->y;
// double dx2 = pt2->x - pt0->x;
// double dy2 = pt2->y - pt0->y;
// return (dx1*dx2 + dy1*dy2) / sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
//}
//
//// 返回图像中找到的所有轮廓序列,并且序列存储在内存存储器中
//
//CvSeq* findSquares4(IplImage* img, CvMemStorage* storage)
//{
// CvSeq* contours;
// int i, c, l, N = 255;
// CvSize sz = cvSize(img->width & -2, img->height & -2);
//
// IplImage* timg = cvCloneImage(img);
// IplImage* gray = cvCreateImage(sz, 8, 1);
// IplImage* grayTMP = cvCreateImage(sz, 8, 1);
// IplImage* pyr = cvCreateImage(cvSize(sz.width / 2, sz.height / 2), 8, 3);
// IplImage* tgray;// = cvCreateImage(sz, 8, 1);;
// CvSeq* result;
// double s, t;
// // 创建一个空序列用于存储轮廓角点
// CvSeq* squares = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage);
//
// //cvSetImageROI(timg, cvRect(0, 0, sz.width, sz.height));
// // 过滤噪音
// //cvPyrDown(timg, pyr, 7);
// //cvPyrUp(pyr, timg, 7);
// tgray = cvCreateImage(sz, 8, 1);
//
// // 红绿蓝3色分别尝试提取
// //for (c = 0; c < 3; c++)
// {
// // 提取 the c-th color plane
// //cvSetImageCOI(timg, c + 1);
// cvCvtColor(timg, tgray, CV_RGB2GRAY);
// //cvCopy(timg, tgray, 0);
//
// // 尝试各种阈值提取得到的(N=11)
// for (l = 1; l < N; l++)
// {
// // apply Canny. Take the upper threshold from slider
// // Canny helps to catch squares with gradient shading
// if (l == 0)
// {
// cvCanny(tgray, gray, 0.2, 0.75, 5);
// //使用任意结构元素膨胀图像
// cvDilate(gray, gray, 0, 1);
// }
// else
// {
// // apply threshold if l!=0:
// cvThreshold(tgray, grayTMP, (l + 1) * 255 / N, 255, CV_THRESH_BINARY);
// }
//
// cvCanny(grayTMP, gray, 0.2, 0.75, 5);
//
// // 找到所有轮廓并且存储在序列中
// cvFindContours(gray, storage, &contours, sizeof(CvContour),
// CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
//
// // 遍历找到的每个轮廓contours
// while (contours)
// {
// //用指定精度逼近多边形曲线
// result = cvApproxPoly(contours, sizeof(CvContour), storage,
// CV_POLY_APPROX_DP, cvContourPerimeter(contours)*0.02, 0);
//
//
// if (result->total == 4 &&
// fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 500 &&
// fabs(cvContourArea(result, CV_WHOLE_SEQ)) < 100000 &&
// cvCheckContourConvexity(result))
// {
// s = 0;
//
// for (i = 0; i < 5; i++)
// {
// // find minimum angle between joint edges (maximum of cosine)
// if (i >= 2)
// {
// t = fabs(angle(
// (CvPoint*)cvGetSeqElem(result, i),
// (CvPoint*)cvGetSeqElem(result, i - 2),
// (CvPoint*)cvGetSeqElem(result, i - 1)));
// s = s > t ? s : t;
// }
// }
//
// // if 余弦值 足够小,可以认定角度为90度直角
// //cos0.1=83度,能较好的趋近直角
// if (s < 0.1)
// for (i = 0; i < 4; i++)
// cvSeqPush(squares,
// (CvPoint*)cvGetSeqElem(result, i));
// }
//
// // 继续查找下一个轮廓
// contours = contours->h_next;
// }
// }
// }
// cvReleaseImage(&gray);
// cvReleaseImage(&pyr);
// cvReleaseImage(&tgray);
// cvReleaseImage(&timg);
//
// return squares;
//}
//
////drawSquares函数用来画出在图像中找到的所有正方形轮廓
//void drawSquares(IplImage* img, CvSeq* squares)
//{
// CvSeqReader reader;
// IplImage* cpy = cvCloneImage(img);
// int i;
// cvStartReadSeq(squares, &reader, 0);
//
// // read 4 sequence elements at a time (all vertices of a square)
// for (i = 0; i < squares->total; i += 4)
// {
// CvPoint pt[4], *rect = pt;
// int count = 4;
//
// // read 4 vertices
// CV_READ_SEQ_ELEM(pt[0], reader);
// CV_READ_SEQ_ELEM(pt[1], reader);
// CV_READ_SEQ_ELEM(pt[2], reader);
// CV_READ_SEQ_ELEM(pt[3], reader);
//
// // draw the square as a closed polyline
// cvPolyLine(cpy, &rect, &count, 1, 1, CV_RGB(0, 255, 0), 2, CV_AA, 0);
// }
//
// cvShowImage(wndname, cpy);
// /*const char* filename = "111111111111111111111.jpg";
// const CvArr* image = cpy;
// int cvSaveImage("1111111111111111111111.jpg",image);*/
// char* filename2 = "G:\\yy2.bmp"; //图像名
// cvSaveImage(filename2, cpy);//把图像写入文件
//
//
//
// //Mat mtx(cpy);
//
// cvReleaseImage(&cpy);
//}
//
//
//char* names[] = { "G:\\YY.bmp", 0 };
//
//
//
//
//int main(int argc, char** argv)
//{
// int i, c;
// storage = cvCreateMemStorage(0);
//
//
//
//
// for (i = 0; names[i] != 0; i++)
// {
// img0 = cvLoadImage(names[i], 1);
// if (!img0)
// {
// cout << "不能载入" << names[i] << "继续下一张图片" << endl;
// continue;
// }
// img = cvCloneImage(img0);
// cvNamedWindow(wndname, 1);
//
// // find and draw the squares
// drawSquares(img, findSquares4(img, storage));
//
// c = cvWaitKey(0);
//
// cvReleaseImage(&img);
// cvReleaseImage(&img0);
//
// cvClearMemStorage(storage);
//
// if ((char)c == 27)
// break;
// }
//
// cvDestroyWindow(wndname);
// return 0;
//}
//
//