Logo
Android Computer Vision

OpenCV on Android: Image Sharpening

Integrate OpenCV into an Android project using Kotlin. Learn to convert Bitmaps to OpenCV Mat objects, apply the sharpening filter, and render the result on a mobile screen.

Step 1: SDK Integration

Add OpenCV to your build.gradle dependencies

Gradle Configuration

The modern way is to use the QuickBird OpenCV Maven artifact (or import the official AAR module).

app/build.gradle.kts
dependencies {
    // 引入 OpenCV 4.9.0 Android SDK
    implementation("com.quickbirdstudios:opencv:4.9.0")
    implementation("androidx.core:core-ktx:1.12.0")
    // ...其他依赖
}

Step 2: Kotlin Implementation

Bitmap to Mat conversion and kernel application

ImageProcessor.kt
import android.graphics.Bitmap
import org.opencv.android.Utils
import org.opencv.core.CvType
import org.opencv.core.Mat
import org.opencv.imgproc.Imgproc

fun sharpenBitmap(inputBitmap: Bitmap): Bitmap {
    // 1. 将 Android Bitmap 转换为 OpenCV Mat 矩阵
    val srcMat = Mat()
    Utils.bitmapToMat(inputBitmap, srcMat)

    // 2. 创建 3x3 锐化卷积核 (32位浮点型)
    val kernel = Mat(3, 3, CvType.CV_32F)
    // 填入权重参数: 0, -1, 0 / -1, 5, -1 / 0, -1, 0
    kernel.put(0, 0, 
        0.0, -1.0,  0.0,
       -1.0,  5.0, -1.0,
        0.0, -1.0,  0.0
    )

    // 3. 执行空间滤波
    val dstMat = Mat()
    Imgproc.filter2D(srcMat, dstMat, srcMat.depth(), kernel)

    // 4. 将处理后的 Mat 转换回 Bitmap
    val outputBitmap = Bitmap.createBitmap(
        dstMat.cols(), dstMat.rows(), Bitmap.Config.ARGB_8888
    )
    Utils.matToBitmap(dstMat, outputBitmap)

    // 释放内存 (移动端必做操作)
    srcMat.release()
    dstMat.release()
    kernel.release()

    return outputBitmap
}

Step 3: Initialization & Usage

Initialize OpenCV in MainActivity and update the ImageView

MainActivity.kt Snippet

Crucial: Always ensure OpenCVLoader.initDebug() returns true before using any OpenCV classes.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    // 核心:加载底层的 C++ .so 库
    if (!OpenCVLoader.initDebug()) {
        Log.e("OpenCV", "Unable to load OpenCV")
        return
    }

    val imageView = findViewById<ImageView>(R.id.imageView)
    val blurryBitmap = BitmapFactory.decodeResource(resources, R.drawable.blurry_photo)

    // 调用我们刚刚写的锐化扩展函数
    val sharpBitmap = sharpenBitmap(blurryBitmap)
    imageView.setImageBitmap(sharpBitmap)
}

Explore More#mobile-dev

Integrating FFmpeg 7+ with Kotlin via JNI

Article

A complete, step-by-step visual guide to integrating pre-compiled FFmpeg 7 dynamic libraries (.so) into a modern Android project using CMake, JNI, and Jetpack Compose.

Android FFmpeg + C++: Video Cropping and Filter Processing

Article

A detailed demonstration of how to use FFmpeg and C++ in an Android project to implement video cropping, filter processing, and progress callbacks.

Integrating OpenCV 4.x in Android Studio

Article

A detailed step-by-step guide on how to integrate OpenCV 4.x into an Android Studio project, configure CMake and JNI, and implement image processing features.

Using OpenCV for Image Sharpening in Android

Article

A detailed step-by-step guide on how to integrate OpenCV into an Android project and implement image sharpening functionality.

Android OpenCV + FFmpeg 7: Making Videos Clearer from Blurry

Article

A detailed demonstration of how to use OpenCV and FFmpeg 7 in an Android project for video processing, achieving clarity from blurriness.

FFmpeg Android + OpenSSL Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source and integrate OpenSSL on the Android platform for HTTPS streaming support.

FFmpeg Xcode + Swift Compilation Guide

Article

A detailed step-by-step guide on how to compile FFmpeg from source using Xcode and Swift on macOS, and integrate it into an iOS project.

Android Image Similarity Detection and Grouping

Article

A detailed demonstration of how to use OpenCV in an Android project to implement image similarity detection and grouping features.