QML的酷炫控件,适合移动设备开发。

qt-creator的跨平台是QML与opencv的粘合剂。


关键:

QImage有若干种格式,转化为相应的Mat。

Mat处理完后,还要正确得还原为原来格式的QImage。

关键在于:QImagecvMat(image);cvmatqimage(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 = QImagecvMat(image); cvtColor(mat, mat ,CV_BGR2GRAY);
image = cvmatqimage(mat); qDebug() << "openCV done." << endl; #else
  /*
  /* 就没必要使用/关心 QT自身的图像处理接口。
   */ int width = image.width();
int height = image.height();
QRgb color;
int gray;
for(int i = ; i < width; i++)
{
for(int j= ; 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 QImagecvMat(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_8UC, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC, (void*)image.constBits(), image.bytesPerLine());
break;
default:
qDebug() << "What's the format?";
break;
}
return mat;
} /* 2. Mat --> Qimage */
QImage cvMatQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount();
for(int i = ; i < ; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = ; 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_8UC)
{
// Copy input Mat
const uchar *pSrc = (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_8UC)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (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的更多相关文章

  1. Qt5——从零开始的Hello World教程(Qt Creator)

    简单Qt教程 一.打开Qt Creator 本次的目的是用Qt Creator建立一个Hello World项目,在安装Qt之后,首先要打开Qt Creator. 就是它啦,打开后会显示如下页面. 二 ...

  2. how to deal with &quot;no such file error or diretory&quot; error for a new programmer in QT creator

    when i try to develop a hello demo in QT creator with the code following : #include<QApplication& ...

  3. Qt Creator 4.3.0,Quick Designer里面也看以同时看到和编辑qml code了(Qt5.9的配套IDE)

    作者:Summer Fang链接:https://www.zhihu.com/question/60486611/answer/177584284来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...

  4. QT笔记之解决QT5.2.0和VS2012中文乱码 以及在Qt Creator中文报错

    转载:http://bbs.csdn.net/topics/390750169 VS2012 中文乱码 1.方法一: 包含头文件 #include <QTextCodec> ....... ...

  5. mac book pro macOS10.13.3安装qt、qt creator C++开发环境,qt5.11.1,并解决cmake构建:qt mac this file is not part of any project the code

    因为之前在Ubuntu下使用的是qtcreator开发,现在想在mac上装一个系统,因为许久未装了,还是花了点时间,不如写个博客,下次就更快安装了.在Mac OS X下使用Qt开发,需要配置Qt库和编 ...

  6. qt creator在Qt5中中文显示的问题

    当我们用Qt Creater时,经常出会出现如下问题: 处理方法如下:用记事本打开你的源代码,然后点另存为,utf-8,编码覆盖,这时中文就没问题了但是会乱码.在字符串前加个宏QStringLiter ...

  7. Qt Creator介绍

    简介 Qt Creator是使用Qt开发的IDE.Qt支持Windows.Linux/Unix.Mac OS X.Android.BlackBerry.QNX等多种平台,Qt Creator为不同平台 ...

  8. 【Qt】Qt Creator介绍【转】

    简介 Qt Creator是使用Qt开发的IDE.Qt支持Windows.Linux/Unix.Mac OS X.Android.BlackBerry.QNX等多种平台,Qt Creator为不同平台 ...

  9. OpenCV编译以及QT Creator配置

    OpenCV编译以及QT Creator配置 在进行编译前,需下载以下工具和源码: CMake ---- 用于编译: 下载地址; https://cmake.org/ 安装在D:\Program Fi ...

随机推荐

  1. hdoj 1022 Train Problem I

    #include<stdio.h> int main() { int n,i,j,k; ],]; ]; while(scanf("%d %s %s",&n,in ...

  2. 开源的EtherCAT Master简介

    EtherCAT的主站开发是基于EtherCAT机器人控制系统的开发中非常重要的环节.目前常见开源的主站代码为的RT-LAB开发的SOEM (Simple OpenSource EtherCAT Ma ...

  3. 【C++】自绘控件基础

    由于我们对控件的功能.外观的需求,公共控件并不能很好地满足这一点,所以我们就得自绘控件. 自绘控件有许多方法,比如:处理WM_PAINT消息,设置ownDraw风格,处理WM_CTLCOLOR消息,等 ...

  4. Logger的等级输出

    转自:http://zhidao.baidu.com/link?url=7HnNxxUei6m3X3JOLfK4yShElbu5xwvU9Z7ipBAaIvQOi3Bc30N-7o7JgfnZDEg3 ...

  5. Smack 3.3.1 发布,Java 的 XMPP 开发包

    Smack 3.3.1 发布了,这是一个小更新版本,主要更新包括: [SMACK-441] - Memory leak in KeepAliveManager [SMACK-447] - Compre ...

  6. 查看Mysql表分区语句

    SELECT partition_name part, partition_expression expr, partition_description descr, table_rows FROM ...

  7. [Xamarin] 動態載入Fragment (转帖)

    這篇我們來動態加入,一樣務求好懂簡單 1.一樣先將專案調整成3.0以上版本 2.首先建立自定Control的Layout \Resources\Layout\MyControlLayout1.axml ...

  8. UWP滑动后退

    经过近些年智能手机App的不断发展,用户已经不仅仅满足于功能上的需求.UI.设计等非功能点逐渐在App体验中占了大多数的分数.不知从何时起,滑动手势就成为了App的一个标配.他不仅仅是一个功能,更是一 ...

  9. 曲演杂坛--特殊字符/生僻字与varchar

    对于中文版的SQL SERVER,默认安装后使用的默认排序规则为Chinese_PRC_CI_AS,在此排序规则下,使用varchar类型来可以“正常存取”存放中文字符以及一些东南亚国家的字符,同时v ...

  10. 如何在施工物料管理Web系统中处理大量数据并显示

    最近在开发施工物料管理系统,其中涉及大量的物料信息需要管理和汇总,数据量非常庞大.之前尝试自己通过将原始数据,加工处理建模,在后台代码中通过分组.转置再显示到 Web 页面中,但自己编写的代码量非常大 ...