美文网首页
OpenCvForAndroid接入并测试转成灰度图

OpenCvForAndroid接入并测试转成灰度图

作者: 坑逼的严 | 来源:发表于2019-09-25 15:45 被阅读0次

OpenCV是图像操作的工具,针对于Android平台,他们也出了相关的sdk,就用最新的4.1.1接入并学习。
下载:https://opencv.org/releases/

image.png
当然这个需要翻墙,速度会很慢,这里我也准备了现成的,已放入百度云盘:
链接:https://pan.baidu.com/s/1ITvcA9m01u-EP6AhIMwNKw
提取码:och1
复制这段内容后打开百度网盘手机App,操作更方便哦
正式接入:

1、创建一个支持ndk开发的工程。

2、编辑cmake文件

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
#支持-std=gnu++11
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")


#配置加载native依赖
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/opencv/include)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

#动态方式加载
add_library( opencv_java4 SHARED IMPORTED )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

#引入libopencv_java4.so文件
set_target_properties(opencv_java4
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libopencv_java4.so
        )

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib
        opencv_java4
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

注意点:
a、opencv得支持-std=gnu++11

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

b、添加所有的头文件,设置他的引入路径

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/opencv/include)

c、动态方式加载opencv

add_library( opencv_java4 SHARED IMPORTED )

这里的opencv_java4这个命名必须是这样,他其实是我们依赖的libopencv_java4.so去掉lib头去掉.so后缀留下的,用其他的名字比如随便命名opencv就会报libopencv.so找不到。这个要特别注意。
d、依赖opencv的so文件

set_target_properties(opencv_java4
        PROPERTIES IMPORTED_LOCATION
        ${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/armeabi-v7a/libopencv_java4.so
        )

这里的${CMAKE_CURRENT_SOURCE_DIR}指的是你的cmake文件存放路径,然后基于这个路径自己设置找到so文件路径。
e、最后连接依赖关系

target_link_libraries( # Specifies the target library.
        native-lib
        opencv_java4
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

3、放入opencv sdk文件

1、根据cmake先来放入我们的头文件


image.png

将整个include文件夹存入到cpp文件中,这个我为了分类,所以多创了一个opencv的文件夹并放入,毕竟我们一个项目不止一个三方依赖库。


image.png
2、存放so文件到工程
image.png

这里,我只想支持arm-v7a的cpu,所以我只创了这个文件夹,libc++_shared.so文件不属于opencv中,那么他在哪呢?进入我们的ndk目录,他就在这,ndk目录\sources\cxx-stl\llvm-libc++\libs\armeabi-v7a,注意一定要是armeabi-v7a,因为我们应用的是这个,不然会报错,是64位和32位不同报错。


image.png
3、gradle配置ndk限制以及cmake的ndk设置以及支持c++11
image.png
源码:
externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
                abiFilters 'armeabi-v7a'
            }
        }
        ndk {
            abiFilters "armeabi-v7a"
        }

4、gradle.properties文件中允许对ndk限制

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useDeprecatedNdk=true

4、测试图片转为灰度图

1、创建自己的工具类
OpencvImageUtile引入两个so

public class OpencvImageUtile {
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("opencv_java4");
    }
    //转灰度
    public static Bitmap goGray(Bitmap bitmap){
        Mat sSrc =new Mat();
        Mat sDst=new Mat();
        org.opencv.android.Utils.bitmapToMat(bitmap, sSrc);
        Imgproc.cvtColor(sSrc, sDst, Imgproc.COLOR_BGRA2GRAY);
        org.opencv.android.Utils.matToBitmap(sDst, bitmap);
        sSrc.release();
        sDst.release();
        return bitmap;
    }
}

然后他会报org.opencv.android.Utils等文件找不到,我们可以去opencv sdk中找


image.png

2、activity调用


image.png
就是布局放个imageview然后将灰度图设置给他。
最终效果图:
微信图片_20190912115741.jpg

5、工程git

https://github.com/yanjinloving/OpencvDemo

相关文章

  • OpenCvForAndroid接入并测试转成灰度图

    OpenCV是图像操作的工具,针对于Android平台,他们也出了相关的sdk,就用最新的4.1.1接入并学习。下...

  • 微服务网关zuul

    zuul功能 统一接入:智能路由、AB测试、灰度测试、负载均衡、容灾处理、日志埋点流量监控:限流处理、服务降级安全...

  • 9.4直方图均衡化

    原图 原图直方图 灰度图 灰度直方图 均衡灰度图 均衡灰度直方图 均衡彩色图 均衡彩色直方图

  • iOS中利用TestFlight进行灰度测试

    iOS中利用TestFlight进行灰度测试 iOS中利用TestFlight进行灰度测试

  • 微信测试号的接口配置信息--配置失败

    问题:微信测试号的接入问题 接入一直提示配置失败 //这个是成功图 debug思路: 查看apache日志,err...

  • 基础 - 话术

    灰度图灰度图 RGB 三个值相同的图片,灰度的方法有=R、=G、=B、平均、加权平均等。 [灰度化] (https...

  • 灰度测试

    灰度测试是指如果软件要在不久的将来推出一个全新的功能,或者做一次比较重大的改版的话,要先进行一个小范围的尝试工作,...

  • 网站图片优化技巧

    # 图片格式 图片尽量转成 jpg 格式,透明图转成 gif 格式 # 图片大小 减少图片的质量以减少大小,并通过...

  • iOS 图片灰度处理

    利用kCGBlendModeLuminosit混合模式 利用灰度公式将每个像素的颜色转成灰色 利用CGColorS...

  • K均值区域分割

    RGB转灰度图:B' = 0.299 R + 0.587 G + 0.114 B 灰度图转RGB: B = (B'...

网友评论

      本文标题:OpenCvForAndroid接入并测试转成灰度图

      本文链接:https://www.haomeiwen.com/subject/sicgyctx.html