I have a question about image filtering using adnroid openCv.

Asked 1 years ago, Updated 1 years ago, 80 views

Refer to http://webnautes.tistory.com/1054 to learn how to use C language on Android. In this example, when you take a picture, you put a filter on the camera What I want to do is to filter the images that have already been taken, so I looked for other examples. http://webnautes.tistory.com/1090 I referred to this.

In this case, as I understand it, let me explain Create an asset folder in the src/main path to create an image file in it Copy and paste the image into the storage of the built cell phone, load the image on the c language code, create it as a bitmap, and filter the image.

The code is as follows.

filterActivity.java

private void copyFile(String filename) {// Save file to Android
        String baseDir = Environment.getExternalStorageDirectory().getPath();
        String pathDir = baseDir + File.separator + filename;

        AssetManager AssetManager = this.getAsset(); // Objects that allow access to the asset file

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            Log.d ("opencv", "copyFile:: Copy file to path" + pathDir);
            inputStream = assetManager.open(filename);
            outputStream = new FileOutputStream(pathDir);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, read);
            }
            inputStream.close();
            inputStream = null;
            outputStream.flush();
            outputStream.close();
            outputStream = null;
        } } catch (Exception e) {
            Log.d ("opencv", "Exception occurred while copying file" + e.toString();
        }

    }

    private void read_image_file() {
        copyFile("ball.jpg");

        img_input = new Mat();
        img_output = new Mat();

        loadImage("ball.jpg", img_input.getNativeObjAddr(); // Go to the saved path and load the image
    }

    Private void image process_and_showResult() { // Filter Images

        imageprocessing(img_input.getNativeObjAddr(), img_output.getNativeObjAddr());

        Bitmap bitmapInput = Bitmap.createBitmap(img_input.cols(), img_input.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(img_input, bitmapInput); // Convert to bmp class using Util class
        filterIv.setImageBitmap(bitmapInput); // Before filtering

        bitmapCandy = Bitmap.createBitmap(img_output.cols(), img_output.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(img_output, bitmapCandy);
        outlv.setImageBitmap(bitmapCandy); // After filtering
    }




#include <jni.h>
#include <opencv2/opencv.hpp>
#include <android/asset_manager_jni.h>
#include <android/log.h>

using namespace cv;
using namespace std;


extern "C" {
JNIEXPORT jstring JNICALL
Java_com_blank_android_crafter_FilterImgActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
JNIEXPORT void JNICALL
Java_com_sd_android_sd_write_Activity_loadImage(JNIEnv *env, jobject instance,
                                                                 jstring imageFileName_,
                                                                 jlong addrImage) {
    Mat &img_input = *(Mat *) addrImage;

    const char *nativeFileNameString = env->GetStringUTFChars(imageFileName_, JNI_FALSE);

    string baseDir("/storage/emulated/0/");
    baseDir.append(nativeFileNameString);
    const char *pathDir = baseDir.c_str();

    img_input = imread(pathDir, IMREAD_COLOR);
}

JNIEXPORT void JNICALL
Java_com_sd_android_sd_write_Activity_imageprocessing(JNIEnv *env,
                                                                       jobject instance,
                                                                       jlong addrInputImage,
                                                                       jlong addrOutputImage) {

    Mat &img_input = *(Mat *) addrInputImage;
    Mat &img_output = *(Mat *) addrOutputImage;

    cvtColor(img_input, img_input, CV_BGR2RGB);
    cvtColor(img_input, img_output, CV_RGB2GRAY);
    blur(img_output, img_output, Size(5, 5));
    Canny(img_output, img_output, 50, 150, 5);

}
}

I created a picture taken with a camera as a bitmap Pass bitmap with int to filterActivity.

Intent intent = getIntent();
bm = (Bitmap) intent.getParcelableExtra("bitmap");

I want to filter the image with this bitmap, but I don't know what to do. First, I put in BM instead of the bitmapCandy object, and there was an error.ㅠ<

 private void image process_and_showResult() { // image filtering

        imageprocessing(img_input.getNativeObjAddr(), img_output.getNativeObjAddr());

        Bitmap bitmapInput = Bitmap.createBitmap(img_input.cols(), img_input.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(img_input, bitmapInput); // Convert to bmp class using Util class
        filterIv.setImageBitmap(bitmapInput); // Before filtering

        bitmapCandy = Bitmap.createBitmap(img_output.cols(), img_output.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(img_output, bm);
        outlv.setImageBitmap(bm); // After filtering
    }

No matter how hard I try, I don't know what to do I'd appreciate it if you let me know.

opencv android jni

2022-09-22 16:13

1 Answers

It's a self-answer. The bitmap transferred to int is implemented by storing it on the cell phone and filtering the image as in the example.

I would appreciate it if you could answer if you know how to implement it without saving it.


2022-09-22 16:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.