若該文爲原創文章,未經容許不得轉載
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客導航:http://www.javashuo.com/article/p-wxwjppoc-mo.html
本文章博客地址:http://www.javashuo.com/article/p-dgiwcywp-ks.html
紅胖子(紅模仿)的博文大全:開發技術集合(包含Qt實用技術、樹莓派、三維、OpenCV、OpenGL、ffmpeg、OSG、單片機、軟硬結合等等)持續更新中…(點擊傳送門)算法
作算法過程當中,須要一個平臺來實時查看效果,記錄處理過程,能夠一鍵查看效果;
OpenCV的各類算法在Qt效果調試;
持續升級版本,敬請期待...app
基於Qt的OpenCV開發,依託Qt做爲界面,OpenCV進行圖像處理。ide
《OpenCV開發筆記(三十四):紅胖子帶你傻瓜式編譯Qt+openCV3.4.1+opencv_contrib(全網最淺顯易懂)》
《OpenCV開發筆記(四):OpenCV圖片和視頻數據的讀取與存儲》
《OpenCV開發筆記(十三):OpenCV圖像對比度、亮度的調整》
《OpenCV開發筆記(十四):算法基礎之線性濾波-方框濾波》
《OpenCV開發筆記(十五):算法基礎之線性濾波-均值濾波》
《OpenCV開發筆記(十六):算法基礎之線性濾波-高斯濾波》
《OpenCV開發筆記(十八):算法基礎之非線性濾波-中值濾波》
《OpenCV開發筆記(十九):算法基礎之非線性濾波-雙邊濾波》
《OpenCV開發筆記(二十一):算法基礎之形態學濾波-膨脹》
《OpenCV開發筆記(二十二):算法基礎之形態學濾波-腐蝕》ui
CSDN:https://download.csdn.net/download/qq21497936/12570673
QQ羣:1047134658(點擊「文件」搜索「qtOpenCVTools」,羣內與博文同步更新)this
#ifndef COMMON_H #define COMMON_H #include <QImage> #include <QDebug> #include <QFileDialog> #include <QColorDialog> #include <QMessageBox> #include <QHash> // opencv #include "opencv/highgui.h" #include "opencv/cxcore.h" #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/opencv.hpp" #include "opencv2/xphoto.hpp" // opencv_contrib #include <opencv2/xphoto.hpp> #include <opencv2/ximgproc.hpp> #include <opencv2/calib3d.hpp> cv::Mat image2Mat(QImage image); // Qimage 轉 cv::Mat QImage mat2Image(cv::Mat mat); // cv::Mat 轉 QImage #endif // COMMON_H
#include "common.h" cv::Mat image2Mat(QImage image) { cv::Mat mat; switch(image.format()) { case QImage::Format_ARGB32: case QImage::Format_RGB32: case QImage::Format_ARGB32_Premultiplied: mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine()); cv::cvtColor(mat, mat, CV_BGRA2BGR); break; case QImage::Format_RGB888: mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine()); cv::cvtColor(mat, mat, CV_BGR2RGB); break; case QImage::Format_Indexed8: mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine()); break; } return mat; } QImage mat2Image(cv::Mat mat) { if(mat.type() == CV_8UC1) { QImage image(mat.cols, mat.rows, QImage::Format_Indexed8); // Set the color table (used to translate colour indexes to qRgb values) image.setColorCount(256); for(int i = 0; i < 256; i++) { image.setColor(i, qRgb(i, i, i)); } // Copy input Mat 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; } 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(); } 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(); } }
#ifndef DEMOCONTRASTANDBRIGHTNESSWIDGET_H #define DEMOCONTRASTANDBRIGHTNESSWIDGET_H #include <QWidget> #include "common.h" namespace Ui { class DemoContrastAndBrightnessWidget; } class DemoContrastAndBrightnessWidget : public QWidget { Q_OBJECT public: explicit DemoContrastAndBrightnessWidget(QWidget *parent = 0); ~DemoContrastAndBrightnessWidget(); public: void setFilePath(QString filePath); protected: void updateInfo(); void updateImage(); private slots: void on_pushButton_openFile_clicked(); void on_horizontalSlider_beta_sliderMoved(int position); void on_horizontalSlider_alpha_sliderMoved(int position); void on_pushButton_broswer_clicked(); void on_spinBox_beta_valueChanged(int arg1); void on_doubleSpinBox_alpha_valueChanged(double arg1); void on_pushButton_showFile_clicked(); void on_pushButton_backgroundColor_clicked(); private: Ui::DemoContrastAndBrightnessWidget *ui; private: QString _filePath; cv::Mat _srcMat; QImage _srcImage; cv::Mat _dstMat; QImage _dstImage; }; #endif // DEMOCONTRASTANDBRIGHTNESSWIDGET_H
#include "DemoContrastAndBrightnessWidget.h" #include "ui_DemoContrastAndBrightnessWidget.h" DemoContrastAndBrightnessWidget::DemoContrastAndBrightnessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::DemoContrastAndBrightnessWidget) { ui->setupUi(this); } DemoContrastAndBrightnessWidget::~DemoContrastAndBrightnessWidget() { delete ui; } void DemoContrastAndBrightnessWidget::setFilePath(QString filePath) { _filePath = filePath; ui->lineEdit_filePath->setText(_filePath); } void DemoContrastAndBrightnessWidget::updateInfo() { if(_srcMat.data == 0) { return; } // mat行列與圖片高度是對角線反向的 ui->label_size->setText(QString("%1 x %2").arg(_srcMat.rows).arg(_srcMat.cols)); ui->label_channels->setText(QString("%1").arg(_srcMat.channels())); ui->label_depth->setText(QString("%1").arg(_srcMat.depth())); ui->label_type->setText(QString("%1").arg(_srcMat.type())); } void DemoContrastAndBrightnessWidget::updateImage() { if(_srcImage.isNull()) { return; } cv::Mat srcMat = image2Mat(_srcImage); // 加強對比度 float r; float g; float b; _dstMat = cv::Mat::zeros(srcMat.size(), srcMat.type()); int alpha = ui->horizontalSlider_alpha->value(); // 小於1,則下降對比度 int beta = ui->horizontalSlider_beta->value(); // 負數,則下降亮度 for(int row = 0; row < srcMat.rows; row++) { for(int col = 0; col < srcMat.cols; col++) { b = srcMat.at<cv::Vec3b>(row, col)[0]; g = srcMat.at<cv::Vec3b>(row, col)[1]; r = srcMat.at<cv::Vec3b>(row, col)[2]; // 對比度、亮度計算公式 cv::saturate_cast<uchar>(value):防止溢出 _dstMat.at<cv::Vec3b>(row, col)[0] = cv::saturate_cast<uchar>(b * alpha / 100.0f + beta); _dstMat.at<cv::Vec3b>(row, col)[1] = cv::saturate_cast<uchar>(g * alpha / 100.0f + beta); _dstMat.at<cv::Vec3b>(row, col)[2] = cv::saturate_cast<uchar>(r * alpha / 100.0f + beta); } } _dstImage = mat2Image(_dstMat); ui->widget_image->setImage(_dstImage); } void DemoContrastAndBrightnessWidget::on_pushButton_openFile_clicked() { if(!_srcImage.load(_filePath)) { qDebug() << __FILE__ << __LINE__ << "Failed to load image:" << _filePath; return; } qDebug() << __FILE__<< __LINE__ << (int)_srcImage.format(); _srcMat = image2Mat(_srcImage); updateInfo(); updateImage(); } void DemoContrastAndBrightnessWidget::on_horizontalSlider_beta_sliderMoved(int position) { ui->spinBox_beta->setValue(position); updateImage(); } void DemoContrastAndBrightnessWidget::on_horizontalSlider_alpha_sliderMoved(int position) { ui->doubleSpinBox_alpha->setValue(position / 100.0f); updateImage(); } void DemoContrastAndBrightnessWidget::on_pushButton_broswer_clicked() { QString dir = ui->lineEdit_filePath->text(); dir = dir.mid(0, dir.lastIndexOf("/")); QString filePath = QFileDialog::getOpenFileName(0, "打開圖片", dir, "PNG;JPEG;BMP(*.png;*.jpg;*.bmp);;JPEG(*.jpg);;PNG(*.png);;BMP(*.bmp)"); if(filePath.isEmpty()) { return; } _filePath = filePath; ui->lineEdit_filePath->setText(_filePath); } void DemoContrastAndBrightnessWidget::on_spinBox_beta_valueChanged(int arg1) { ui->horizontalSlider_beta->setValue(arg1); updateImage(); } void DemoContrastAndBrightnessWidget::on_doubleSpinBox_alpha_valueChanged(double arg1) { ui->horizontalSlider_alpha->setValue(arg1 * 100); updateImage(); } void DemoContrastAndBrightnessWidget::on_pushButton_showFile_clicked() { if(_dstMat.data == 0) { QMessageBox::information(this, "提示", "使用opencv顯示圖片失敗"); return; } cv::imshow("showFile", _dstMat); } void DemoContrastAndBrightnessWidget::on_pushButton_backgroundColor_clicked() { QColor backgroundColor = ui->widget_image->getBackgroundColor(); backgroundColor = QColorDialog::getColor(backgroundColor, this, "底色"); if(!backgroundColor.isValid()) { return; } QColor color(backgroundColor.red(), backgroundColor.green(), backgroundColor.blue()); ui->widget_image->setBackgroundColor(color); }
原博主博客地址:https://blog.csdn.net/qq21497936
原博主博客導航:http://www.javashuo.com/article/p-wxwjppoc-mo.html
本文章博客地址:http://www.javashuo.com/article/p-dgiwcywp-ks.htmlspa