编写按钮槽函数
按钮槽函数实现选择图片,并对图片类型进行简单的判断,通过消息框提示错误,选择完成后将图片显示在【显示图片】区。
void MainWindow::on_pushButton_clicked(){ QString FileName = QFileDialog::getOpenFileName(this, tr("文件对话框"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), tr("ALL files (*);;Excel Files(*.jpg)")); if(FileName.contains(".jpg", Qt::CaseSensitive) != true) { qDebug()<<"路径错误!"; QMessageBox::information(nullptr, "错误", "路径错误,\n请重新选择!", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); return; } srcImg = imread(FileName.toLocal8Bit().toStdString()); cvtColor(srcImg, grayImg, COLOR_BGR2GRAY); blur(grayImg, grayImg, Size(3,3)); Mat img; if(!isGray) { cvtColor(srcImg, img, CV_BGR2RGB); qtemp = QImage(img.data, img.cols, img.rows, QImage::Format_RGB888); }else{ cvtColor(grayImg, img, CV_GRAY2RGB); qtemp = QImage(img.data, img.cols, img.rows, QImage::Format_RGB888); } QPixmap fiximg = QPixmap::fromImage(qtemp); QPixmap fiximg1 = fiximg.scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); //按比例缩放// QPixmap fiximg1 = fiximg.scaled(ui->label->width(), ui->label->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); //饱满填充 ui->label->setPixmap(fiximg1); ui->label->show();// GaussianBlur(grayImg, grayImg, Size(9, 9), 0, 0); handlefunc(100);}
编写边缘检测函数
使用Canny边缘检测算法对图像进行边缘检测,画出检测轮廓,并将
检测结果显示在【显示】区。
void MainWindow::handlefunc(int thresh){ RNG rng(12345); Mat canny_output; vector> contours; vector hierarchy; // canny 边缘检测 Canny(grayImg, canny_output, thresh, thresh*2, 3); // 寻找轮廓 findContours( canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0)); Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3); // 画出轮廓 for( size_t i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255)); drawContours(drawing, contours, (int) i, color, 2, 8, hierarchy, 0, Point() ); } qtemp = QImage(drawing.data, drawing.cols, drawing.rows, QImage::Format_RGB888); QPixmap fiximg2 = QPixmap::fromImage(qtemp); QPixmap fiximg3 = fiximg2.scaled(ui->labelShow->width(), ui->labelShow->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); //按比例缩放// QPixmap fiximg1 = fiximg.scaled(ui->labelShow->width(), ui->labelShow->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); //饱满填充 ui->labelShow->setPixmap(fiximg3); ui->labelShow->show();}
滑动条设置滞后阈值
滑动条修改滞后阈值,观察边缘检测效果。void MainWindow::on_verticalSlider_sliderMoved(int position){ qDebug()< handlefunc(position);}
canny边缘检测_QT5.13+OpenCV实现图像边缘检测
最新推荐文章于 2024-06-08 17:59:30 发布