新手上路
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2020-5-8
- 在线时间
- 7 小时
|
1金钱
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* 主窗口初始化 */
this->setGeometry(0, 0, 800, 480);
this->setWindowFlags(Qt::FramelessWindowHint);
/* QProcess通信初始化 */
qprocessCommunicationInit();
/* 加载图片并显示原图 */
matData = imread("./resources/image/image.jpg");
imgLabel = new QLabel(this);
imgLabel->setGeometry(0, 0, 800, 480);
originalImgShow();
/* 顶部菜单栏部件初始化 */
topMenuInit();
/* 按钮初始化 */
CV_Menu = new QWidget(this);
CV_Menu->setGeometry(640, 50, 160, 430);
CV_Menu->setStyleSheet("QWidget{ background-color: rgba(0, 0, 0, 80%); }");
btnArr.clear();
splitter = new QSplitter(CV_Menu); // 使用分裂器垂直布局
splitter->setOrientation(Qt::Vertical); // 垂直布局
splitter->setGeometry(0, 0, 160, 430);
splitter->setHandleWidth(4); // 间隔大小
QPushButton *originalBtn = new QPushButton(CV_Menu);
originalBtn->setText("原图");
connect(originalBtn, SIGNAL(clicked()), this, SLOT(originalImgShow()));
btnArr.append(originalBtn);
QPushButton *cannyBtn = new QPushButton(CV_Menu);
cannyBtn->setText("检测");
connect(cannyBtn, SIGNAL(clicked()), this, SLOT(CV_Canny()));
btnArr.append(cannyBtn);
for (int i = 0; i < btnArr.count(); i++) {
QPushButton *btn = btnArr.at(i);
btn->setStyleSheet("QPushButton{ background-color:rgba(255, 255, 255, 30%); outline:none;"
" color:rgb(100, 100, 200); }");
splitter->addWidget(btn);
}
/* 固定分割线不能移动 */
QSplitterHandle *splitterHandle = NULL;
for (int i = 1; i <= 6; i++) {
splitterHandle = splitter->handle(i);
splitterHandle->setDisabled(true);
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::CV_Canny(void)
{
Mat temp;
cvtColor(matData, temp, COLOR_BGR2GRAY);
detect(temp);
QImage img = QImage((const unsigned char *)(temp.data),
temp.cols,
temp.rows,
static_cast<int>(temp.step),
QImage::Format_Grayscale8);
QPixmap pixmap = QPixmap(QPixmap::fromImage(img.rgbSwapped()));
imgLabel->setPixmap(pixmap);
}
enum METHOD { MEAN, GAUSS, MEDIAN };
Mat adaptiveThresh(Mat I, int radius, float ratio, METHOD method = MEAN)
{
Mat smooth;
switch (method)
{
case MEAN:
boxFilter(I, smooth, CV_32FC1, Size(2 * radius + 1, 2 * radius + 1));
break;
case GAUSS:
GaussianBlur(I, smooth, Size(2 * radius + 1, 2 * radius + 1), 0, 0);
break;
case MEDIAN:
medianBlur(I, smooth, 2 * radius + 1);
break;
default:
break;
}
I.convertTo(I, CV_32FC1);
smooth.convertTo(smooth, CV_32FC1);
Mat diff = I - (1.0 - ratio)*smooth;
Mat out = Mat::zeros(diff.size(), CV_8UC1);
for (int r = 0; r < out.rows; r++)
{
for (int c = 0; c < out.cols; c++)
{
if (diff.at<float>(r, c) >= 0)
out.at<uchar>(r, c) = 255;
}
}
return out;
}
//
void detect(InputArray src)
{
Ptr<CLAHE> clahe = createCLAHE(2.0, Size(8, 8));
Mat dst1; Mat dst2;
clahe->apply(src, dst1);//自适应直方图均衡化
dst2 = adaptiveThresh(dst1, 10, 0.15, GAUSS);//自适应阈值分割
Mat dst3;
Mat element;
element = getStructuringElement(MORPH_RECT, Size(3, 3));
morphologyEx(dst2, dst3, MORPH_CLOSE, element, Point(-1, -1), 1);
Mat edge;
Canny(dst3, edge, 40, 150, 3, true);
Mat contoursImg = Mat::zeros(src.size(), CV_8UC3);
Mat out(src.size(), src.type());
out = Scalar::all(255);
vector<Vec3f> circles;
HoughCircles(edge, circles, CV_HOUGH_GRADIENT, 1.5, 30, 150, 38, 120, 200);
double num = 0.0;
for (size_t i = 0; i < circles.size(); i++)//把霍夫变换检测出的圆画出来
{
Point center(cvRound(circles[0]), cvRound(circles[1]));
double radius = cvRound(circles[2]);
circle(out, center, 0, Scalar(0, 255, 0), -1, 8, 0);
circle(out, center, radius, Scalar(0, 0, 255), 1, 8, 0);
double rect = radius * radius*3.14;
num = num + rect;
cout << "【用轮廓面积计算函数计算出来的第" << i << "个圆的面积为:】" << rect << endl;
}
cout << "num:" << num << endl;
imshow("特征提取", out);
cout << circles.size() << "," << endl;
}
void MainWindow:riginalImgShow(void)
{
QImage img = QImage((const unsigned char *)(matData.data),
matData.cols,
matData.rows,
static_cast<int>(matData.step),
QImage::Format_RGB888);
QPixmap pixmap = QPixmap(QPixmap::fromImage(img.rgbSwapped()));
imgLabel->setPixmap(pixmap);
}
void MainWindow::slotHomeBtnClicked()
{
QFile f_out;
f_out.open(stdout, QIODevice::WriteOnly);
f_out.write("show");
f_out.close();
hide();
}
void MainWindow::slotQuitBtnClicked()
{
slotHomeBtnClicked();
qApp->quit();
}
void MainWindow::slotActivated(int fd)
{
if(fd != qproComcation.stdIn.handle()) //
return;
char buffer[50];
read(fd, buffer, 10); // 读取消息
if (!strcmp(buffer, "show")) // 匹配消息并执行相应指令
this->show();
if (!strcmp(buffer, "quit"))
qApp->quit();
}
void MainWindow::topMenuInit()
{
topMenu.topMenuBar = new QWidget(this);
topMenu.topMenuBar->setGeometry(0, 0, 800, 40);
topMenu.topMenuBar->setStyleSheet("QWidget{ background-color:rgba(0, 0, 0, 80%); }");
topMenu.topTitle = new QLabel(topMenu.topMenuBar);
topMenu.topTitle->setGeometry(300, 0, 200, 40);
topMenu.topTitle->setAlignment(Qt::AlignCenter);
topMenu.topTitle->setStyleSheet("QLabel{ background-color:transparent; color:white;"
" font:20pt \"Microsoft YaHei\"; }");
topMenu.topTitle->setText("OpenCV演示");
topMenu.quitBtn = new QPushButton(topMenu.topMenuBar);
topMenu.quitBtn->setGeometry(740, 0, 40, 40);
topMenu.quitBtn->setStyleSheet("QPushButton{ border-image:url(:/resources/icon/quit_2.png);"
" background-color:transparent; outline: none; }"
"QPushButton:pressed{ border-image:url(:/resources/icon/quit_1.png); }");
topMenu.homeBtn = new QPushButton(topMenu.topMenuBar);
topMenu.homeBtn->setGeometry(10, 0, 40, 40);
topMenu.homeBtn->setStyleSheet("QPushButton{ border-image:url(:/resources/icon/home.png);"
" background-color:transparent; outline: none; }");
connect(topMenu.quitBtn, SIGNAL(clicked()), this, SLOT(slotQuitBtnClicked()));
connect(topMenu.homeBtn, SIGNAL(clicked()), this, SLOT(slotHomeBtnClicked()));
}
void MainWindow::qprocessCommunicationInit()
{
qproComcation.stdIn.open(stdin, QIODevice::ReadOnly);
qproComcation.sn = new QSocketNotifier(qproComcation.stdIn.handle(), QSocketNotifier::Read, this);
connect(qproComcation.sn, SIGNAL(activated(int)), this, SLOT(slotActivated(int)));
}
以上是我根据正点原子里的移植opencv那个源代码改的 改动的不多 因为需要的功能也只是检测图片中的圆并输出所有检测得到的圆的面积,小白一个 只能这样试试了 请问这样改动后 可以编译并复制进板子的root目录下运行了么? 如果哪里有错 不行的话 请帮忙指出来可以么 万分感谢
|
|