基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理

《基于qml创建最简单的图像处理程序》系列课程及配套代码
基于qml创建最简单的图像处理程序(1)-基于qml创建界面
http://www.cnblogs.com/jsxyhelu/p/8343310.html
课程1附件
https://files.cnblogs.com/files/jsxyhelu/%E9%98%B6%E6%AE%B5%E4%BB%A3%E7%A0%811.zip
基于qml创建最简单的图像处理程序(2)-使用c++&qml进行图像处理
http://www.cnblogs.com/jsxyhelu/p/8361441.html
课程2附件
https://files.cnblogs.com/files/jsxyhelu/%E9%98%B6%E6%AE%B5%E4%BB%A3%E7%A0%812.zip
基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理
http://www.cnblogs.com/jsxyhelu/p/8361443.html
课程3附件
https://files.cnblogs.com/files/jsxyhelu/%E9%98%B6%E6%AE%B5%E4%BB%A3%E7%A0%813.zip

    在qml的系统中引入OpenCV,最复杂的地方在于环境的配置—一旦错误,系统即使能够编译成功,但是也会在启动的闪退;本文主要就是讲解在qml的系统(目标平台为Android,编码平台为windows)中如何正确配置OpenCV环境,并且将(2)中的图像处理结果进一步修改成为使用OpenCV来进行图像处理的结果;对于QT来说,实际上说的就是如何配置.pro文件。

 
一、环境配置,可以参考:
QT += quick
CONFIG += c++11

android {
message("Using android configuration")
ANDROID_OPENCV = D:/OpenCV-android-sdk/sdk/native

INCLUDEPATH += \
$$ANDROID_OPENCV/jni/include/opencv    \
$$ANDROID_OPENCV/jni/include/opencv2    \
$$ANDROID_OPENCV/jni/include            \

LIBS += \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_ml.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_objdetect.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_calib3d.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_video.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_features2d.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_highgui.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_flann.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_imgproc.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_core.a     \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjpeg.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibpng.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibtiff.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjasper.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtbb.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtegra_hal.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_java3.so
}

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += main.cpp \
    imageProcessor.cpp

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

HEADERS += \
    imageProcessor.h

DISTFILES += \
    android/AndroidManifest.xml \
    android/gradle/wrapper/gradle-wrapper.jar \
    android/gradlew \
    android/res/values/libs.xml \
    android/build.gradle \
    android/gradle/wrapper/gradle-wrapper.properties \
    android/gradlew.bat

contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        D:/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_java3.so

}

ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
 
 
其中标红加粗的两个地方,是尤其需要注意进行修改的地方,分别对于
配置环境变量,当然你要根据你实际的地址来修改
ANDROID_OPENCV = D:/OpenCV-android-sdk/sdk/native
配置InClude地址
INCLUDEPATH += \
$$ANDROID_OPENCV/jni/include/opencv    \
$$ANDROID_OPENCV/jni/include/opencv2    \
$$ANDROID_OPENCV/jni/include            \
配置Libs地址
LIBS += \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_ml.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_objdetect.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_calib3d.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_video.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_features2d.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_highgui.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_flann.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_imgproc.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_core.a     \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjpeg.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibpng.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibtiff.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/liblibjasper.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtbb.a \
$$ANDROID_OPENCV/3rdparty/libs/armeabi-v7a/libtegra_hal.a \
$$ANDROID_OPENCV/libs/armeabi-v7a/libopencv_java3.so
尤其需要注意,这个顺序不能够错。
最后,还要将libopencv_java3.so当都付到.apk文件中
contains(ANDROID_TARGET_ARCH,armeabi-v7a) {
    ANDROID_EXTRA_LIBS = \
        D:/OpenCV-android-sdk/sdk/native/libs/armeabi-v7a/libopencv_java3.so
 
二、引入头文件和命名空间,修改算法函数:
直接在头文件中引入头文件,并且表明命名空间
#ifndef IMAGEPROCESSOR_H
#define IMAGEPROCESSOR_H
#include <QObject>
#include <QString>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
 
按照前文所说,修改相关算法函数。因为本来函数都是以路径作为输入输出的,所以这里的操作比较简单。这里得到的算法名称可能和实际不一致,问题不大:
 
//具体的图像处理算法,注意图片处理的结果直接保存到了destFile中去//
static void _gray(QString sourceFile, QString destFile)
{
    Mat src = imread(sourceFile.toStdString());
    cv::cvtColor(src,src,COLOR_BGR2GRAY);
    imwrite(destFile.toStdString(),src);
}

static void _binarize(QString sourceFile, QString destFile)
{
    Mat src = imread(sourceFile.toStdString());
    cv::cvtColor(src,src,COLOR_BGR2GRAY);
    threshold(src,src,100,255,THRESH_OTSU);
    imwrite(destFile.toStdString(),src);
}


static void _emboss(QString sourceFile, QString destFile)
{
    Mat src = imread(sourceFile.toStdString());
    cv::cvtColor(src,src,COLOR_BGR2GRAY);
    int thresh = 100;
    Canny( src, src, thresh, thresh*23 );
    imwrite(destFile.toStdString(),src);
}
//END 具体的图像处理算法,注意图片处理的结果直接保存到了destFile中去//
 
这里得到的就是一个canny的效果,也是通过调用OpenCV才得到的结果。那么这样一个框架就建立了。感谢阅读至此!希望有所帮助

 

posted @ 2018-01-26 20:05 jsxyhelu 阅读(...) 评论(...) 编辑 收藏