#include "dialog.h"
#include "ui_dialog.h"
#include<QFileDialog>
#include"Ellipse.cpp"
#include<iostream>
#include<cmath>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
/*----------------------------
* 功能 : 响应用户点击打开图片按钮事件
*----------------------------
* 函数 : on_openImgBtn_clicked
* 访问 : private
* 返回 : void
*
*/
void Dialog::on_openImgBtn_clicked()
{
//打开原始图片
fileName = QFileDialog::getOpenFileName(this,"Open Image",".","Image File(*.png *.jpg *.jpeg *.bmp)");
srcImg = imread(fileName.toLatin1().data(),IMREAD_GRAYSCALE);
if(srcImg.empty())
return;
//在QLabel中显示图像
QPixmap *pixmap = new QPixmap(fileName);
pixmap->scaled(ui->oriImgLabel->size(), Qt::KeepAspectRatio);
ui->oriImgLabel->setScaledContents(true);
ui->oriImgLabel->setPixmap(*pixmap);
}
/*----------------------------
* 功能 : 响应用户点击直线检测按钮事件
*----------------------------
* 函数 : on_lineImgBtn_clicked
* 访问 : private
* 返回 : void
*
*/
void Dialog::on_lineImgBtn_clicked()
{
uchar index = ui->lineComBox->currentIndex();//获取选择的算子的标号
Mat lineImg,color_lineImg;
switch (index) {
case 0:
{
//概率霍夫变换
Canny( srcImg, lineImg, 50, 200, 3 );
cvtColor( lineImg, color_lineImg, CV_GRAY2BGR );
vector<Vec4i> lines;
HoughLinesP( lineImg, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( color_lineImg, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
imshow( "Detected Lines", color_lineImg );
break;
}
case 1:
{
//霍夫变换
Canny( srcImg, lineImg, 50, 200, 3 );
cvtColor( lineImg, color_lineImg, CV_GRAY2BGR );
vector<Vec2f> lines;
HoughLines( lineImg, lines, 1, CV_PI/180, 100 );
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0];
float theta = lines[i][1];
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
Point pt1(cvRound(x0 + 1000*(-b)),
cvRound(y0 + 1000*(a)));
Point pt2(cvRound(x0 - 1000*(-b)),
cvRound(y0 - 1000*(a)));
line( color_lineImg, pt1, pt2, Scalar(0,0,255), 3, 8 );
}
break;
}
default:
{
break;
}
}
imwrite("line.png",color_lineImg);
//在QLabel中显示图像
QPixmap *pixmap = new QPixmap("line.png");
pixmap->scaled(ui->lineImgLabel->size(), Qt::KeepAspectRatio);
ui->lineImgLabel->setScaledContents(true);
ui->lineImgLabel->setPixmap(*pixmap);
}
/*----------------------------
* 功能 : 响应用户点击圆检测按钮事件
*----------------------------
* 函数 : on_cirImgBtn_clicked
* 访问 : private
* 返回 : void
*
*/
void Dialog::on_cirImgBtn_clicked()
{
Mat grayImg = srcImg.clone();
Mat colorImg;
cvtColor(grayImg, colorImg, CV_GRAY2BGR);
//平滑图像
GaussianBlur( grayImg, grayImg, Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles(grayImg, circles, CV_HOUGH_GRADIENT,
2, grayImg.rows/4, 200, 150 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
//绘制圆心
circle( colorImg, center, 3, Scalar(0,255,0), -1, 8, 0 );
//绘制圆的边
circle( colorImg, center, radius, Scalar(0,0,255), 3, 8, 0 );
}
imwrite("circle.png",colorImg);
//在QLabel中显示图像
QPixmap *pixmap = new QPixmap("circle.png");
pixmap->scaled(ui->cirImgLabel->size(), Qt::KeepAspectRatio);
ui->cirImgLabel->setScaledContents(true);
ui->cirImgLabel->setPixmap(*pixmap);
}
/*----------------------------
* 功能 : 响应用户点击椭圆检测按钮事件
*----------------------------
* 函数 : on_elliImgBtn_clicked
* 访问 : private
* 返回 : void
*
*/
void Dialog::on_elliImgBtn_clicked()
{
Mat grayImg = srcImg.clone();
Mat colorImg;
cvtColor(grayImg, colorImg, CV_GRAY2BGR);
GaussianBlur(grayImg, grayImg, Size(3,3), 0, 0); //高斯平滑
Canny(grayImg, grayImg, 100, 200, 3); //Canny边缘检测
namedWindow("Canny", CV_WINDOW_AUTOSIZE);
imshow("Canny", grayImg);
//提取轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(grayImg,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
//绘制查找到的轮廓
//drawContours(colorImg, contours, -1, Scalar(0,255,0));
//imshow("cour.bmp",colorImg);
//Hough变换
int accumulate; // Hough空间最大累积量
Ellipse myellipse;
Mat elliImg(grayImg.size(),CV_8UC3,Scalar(0));
for(int i=0;i< contours.size();i++)
{
myellipse.Computer_axy(contours[i],grayImg.size());
accumulate=myellipse.hough_ellipse(contours[i]);
//cout<<"accumulate:"<<accumulate<<endl;
//cout<<"contours[i].size():"<<contours[i].size()<<endl;
// 判断是否超过给定阈值,判断是否为椭圆,并通过长轴与短轴的轴长差去除干扰
if(accumulate>=contours[i].size()*0.25 && abs(myellipse.getLongAxis()-myellipse.getShortAxis())>2.0
&& abs(myellipse.getLongAxis()-myellipse.getShortAxis())<50.0)
elliImg=myellipse.draw_Eliipse(colorImg);
}
imwrite("ellipse.png",elliImg);
//在QLabel中显示图像
QPixmap *pixmap = new QPixmap("ellipse.png");
pixmap->scaled(ui->elliImgLabel->size(), Qt::KeepAspectRatio);
ui->elliImgLabel->setScaledContents(true);
ui->elliImgLabel->setPixmap(*pixmap);
}