專案名稱: RGB2GrayQt5OpenCV246
將下列加入專案設定(.pro)
INCLUDEPATH += J:\OpenCV-2.4.6\opencv\include\
LIBS += -LJ:\OpenCV-2.4.6\opencv\lib \
-lopencv_core246 \
-lopencv_highgui246 \
-lopencv_imgproc246 \
-lopencv_features2d246 \
-lopencv_calib3d246
將下列加入mainwindow.cpp, 讓繁體中文不再顯示亂碼
#pragma execution_character_set("utf-8")
新增一個Tab Widget
點選Tab1 修改CurrentTabText屬性值=彩色
點選Tab2 修改CurrentTabText屬性值=灰階
新增Label用來顯示影像
加入一個Push Button
將下列加入mainwindow.h
#include <QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
加入兩個影像
cv::Mat img_rgb;//類別私有成員img
cv::Mat img_gray;
加入ImageUtility.h和ImageUtility.cpp
跳到clicked() Callback function
void MainWindow::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, "開啟檔案",".","檔案格式(*.png*.jpg*.jpeg*.bmp*.tiff)");
img_rgb = cv::imread(fileName.toStdString().data());
QImage qimg_rgb = Mat2QImage(img_rgb);//Mat轉換至QImage
imshow(ui->label,qimg_rgb);//顯示彩色影像
//----------------------------------------------------
cv::cvtColor(img_rgb, img_gray, CV_BGR2GRAY);//彩色轉灰階
QImage qimg_gray = Mat2QImage(img_gray);//Mat轉換至QImage
imshow(ui->label_2, qimg_gray);//顯示灰階影像
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h> // 呼叫cv::cvtColor會用到
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
cv::Mat3b img_rgb; // 類別私有成員img
cv::Mat1b img_gray;
};
#endif // MAINWINDOW_H
----------------------------------------------------------------------------------------------------------------------------------------------------------
ImageUtility.h
#ifndefIMAGEUTILITY_H
#defineIMAGEUTILITY_H
#include <QMainWindow>
#include <QLabel>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
QImage Mat2QImage(constcv::Mat3b&src); //將Mat轉成QImage物件(彩色)
QImage Mat2QImage(constcv::Mat1b&src); //將Mat轉成QImage物件(灰階)
void imshow(QLabel* const label, const QImage &qimg);//顯示QImage影像(在QLabel)
#endif//IMAGEUTILITY_H
----------------------------------------------------------------------------------------------------------------------------------------------------------
ImageUtility.cpp
#include "ImageUtility.h"
// Mat轉QImage
QImage Mat2QImage(const cv::Mat3b &src) {
// 創建一個QImage, 寬:src.cols, 高src.rows, 格式:QImage::Format_ARGB32
QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
// (1)垂直方向
for (int y = 0; y < src.rows; ++y) {
// 來源: 宣告一個Vec3b指標 cv::Vec3b *srcrow;
const cv::Vec3b *srcrow = src[y];
// 目標: 宣告一個QRgb指標 QRgb *destrow
QRgb *destrow = (QRgb*)dest.scanLine(y);
// (2)水平方向
for (int x = 0; x < src.cols; ++x) {
destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);
}
}
return dest;
}
//------------------------------------------------------------------------------
QImage Mat2QImage(const cv::Mat1b &src) {
// 創建一個QImage, 寬:src.cols, 高src.rows, 格式:QImage::Format_ARGB32
QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
// (1)垂直方向
for (int y = 0; y < src.rows; ++y) {
// 來源: 宣告一個Vec3b指標 cv::Vec3b *srcrow;
const uchar *srcrow = src[y];
// 目標: 宣告一個QRgb指標 QRgb *destrow
QRgb *destrow = (QRgb*)dest.scanLine(y);
// (2)水平方向
for (int x = 0; x < src.cols; ++x) {
destrow[x] = qRgba(srcrow[x], srcrow[x], srcrow[x], 255);
}
}
return dest;
}
//------------------------------------------------------------------------------
QImage Mat2QImage(const cv::Mat_<double> &src)
{
double scale = 255.0;
QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
for (int y = 0; y < src.rows; ++y) {
const double *srcrow = src[y];
QRgb *destrow = (QRgb*)dest.scanLine(y);
for (int x = 0; x < src.cols; ++x) {
unsigned int color = srcrow[x] * scale;
destrow[x] = qRgba(color, color, color, 255);
}
}
return dest;
}
//------------------------------------------------------------------------------
void imshow(QLabel* const label, const QImage &qimg) // 顯示影像
{
label->move(0,0);
// display on label
label->setPixmap(QPixmap::fromImage(qimg));
// resize the label to fit the image
label->resize(label->pixmap()->size());
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
執行測試
開啟一張彩色照片 Caraunt HM550.jpg
點選<彩色>頁面
點選<灰階>頁面
原始程式碼: RGB2GrayQt5OpenCV246
留言列表