首页 > 代码库 > [Qt5] Develop openCV3 by QML on Qt-creator
[Qt5] Develop openCV3 by QML on Qt-creator
QML的酷炫控件,适合移动设备开发。
qt-creator的跨平台是QML与opencv的粘合剂。
关键:
QImage有若干种格式,转化为相应的Mat。
Mat处理完后,还要正确得还原为原来格式的QImage。
关键在于:QImage2cvMat(image);cvmat2qimage(mat);的定义。
图像格式的转化:
static void _gray(QString sourceFile, QString destFile)
{
QImage image(sourceFile); if(image.isNull()) { qDebug() << "load " << sourceFile << " failed! "; return; } qDebug() << "depth - " << image.depth();#if 1 /* test */ qDebug() << "Let‘s openCV." << endl; cv::Mat mat = QImage2cvMat(image); cvtColor(mat, mat ,CV_BGR2GRAY); image = cvmat2qimage(mat); qDebug() << "openCV done." << endl;#else
/*
/* 就没必要使用/关心 QT自身的图像处理接口。
*/
int width = image.width(); int height = image.height(); QRgb color; int gray; for(int i = 0; i < width; i++) { for(int j= 0; j < height; j++) { color = image.pixel(i, j); gray = qGray(color); image.setPixel(i, j, qRgba(gray, gray, gray, qAlpha(color))); } }#endif image.save(destFile);}
常用的tool_function:
/* 1. Qimage --> Mat */cv::Mat QImage2cvMat(QImage image){ cv::Mat mat; qDebug() << image.format(); 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()); 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;
default: qDebug() << "What‘s the format?"; break; } return mat;}/* 2. Mat --> Qimage */QImage cvMat2QImage(const cv::Mat& mat){ // 8-bits unsigned, NO. OF CHANNELS = 1 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 =http://www.mamicode.com/ 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; } // 8-bits unsigned, NO. OF CHANNELS = 3 else if(mat.type() == CV_8UC3) { // Copy input Mat const uchar *pSrc = http://www.mamicode.com/(const uchar*)mat.data; // Create QImage with same dimensions as input Mat QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); return image.rgbSwapped(); } else if(mat.type() == CV_8UC4) { qDebug() << "CV_8UC4"; // Copy input Mat const uchar *pSrc = http://www.mamicode.com/(const uchar*)mat.data; // Create QImage with same dimensions as input Mat QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32); return image.copy(); } else { qDebug() << "ERROR: Mat could not be converted to QImage."; return QImage(); }}
[Qt5] Develop openCV3 by QML on Qt-creator
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。