-
Notifications
You must be signed in to change notification settings - Fork 1
/
func.cpp
147 lines (141 loc) · 4.09 KB
/
func.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QImage>
#include <QString>
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <assert.h>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
QImage Mat2QImage(cv::Mat mat)
{
//单通道
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
//3通道
else if(mat.type() == CV_8UC3)
{
const uchar *pSrc = (const uchar*)mat.data;
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
//4通道
else if(mat.type() == CV_8UC4)
{
const uchar *pSrc = (const uchar*)mat.data;
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
return QImage();
}
}
void auto_size(cv::Mat &src)
{
int w;
int h;
if(src.cols > 640)
{
h = round(double(src.rows)/double(src.cols)*double(640.0));
cv::resize(src,src,cv::Size(640,h),3);
}
if(src.rows > 480)
{
w = round(double(src.cols)/double(src.rows)*double(480.0));
cv::resize(src,src,cv::Size(w,480),3);
}
if(src.cols < 640 || src.rows < 480)
{
if(src.cols < src.rows)
{
w = round(double(src.cols)/double(src.rows)*double(480.0));
cv::resize(src,src,cv::Size(w,480),3);
}
else
{
h = round(double(src.rows)/double(src.cols)*double(640.0));
cv::resize(src,src,cv::Size(640,h),3);
}
}
}
void hough_lines(cv::Mat &src, int t, int th1, int th2, cv::Scalar color)
{
std::vector<cv::Vec2f> lines;
//图像,线,距离精度,角度精度,阈值
cv::Mat temp;
if(src.channels() == 1)
{
cv::HoughLines(src,lines,1.0,CV_PI/180,t,0,0,0);
}
else
{
cv::Canny(src,temp,th1,th2);
cv::HoughLines(temp,lines,1.0,CV_PI/180,t,0,0,0);
}
//画线
if(src.channels() == 1)
cv::cvtColor(src,src,CV_GRAY2BGR);
for(size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
cv::Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
cv::line(src,pt1,pt2,color,1,cv::LINE_AA);
}
}
cv::Mat hough_circles(cv::Mat &src ,cv::Scalar color, int th1, int th2, int distance)
{
std::vector<cv::Vec3f> circles;
cv::Mat temp;
//复制图像用于画圆
cv::Mat output = src.clone();
if(src.channels() != 1)
{
cv::cvtColor(src,temp,CV_BGR2GRAY);
cv::HoughCircles(temp,circles,cv::HOUGH_GRADIENT,1.5,distance,th1,th2,0,0);
}
else
{
cv::HoughCircles(src,circles,cv::HOUGH_GRADIENT,1.5,distance,th1,th2,0,0);
}
if(src.channels() == 1)
cv::cvtColor(output,output,CV_GRAY2BGR);
for(size_t i = 0; i < circles.size(); i++)
{
cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
cv::circle(output,center,1,color,1,8,0);
cv::circle(output,center,radius,color,1,8,0);
}
return output;
}
void blur_img_process(cv::Mat &src, cv::Mat &blur_img,int size, int sigmaX, int sigmaY )
{
cv::GaussianBlur(src,blur_img,cv::Size(size,size),sigmaX,sigmaY);
}