Android opencv使用Core.hconcat 进行图像拼接

Android 集成OpenCV-CSDN博客

java 复制代码
import org.opencv.android.Utils;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ImageStitchingActivity extends AppCompatActivity {

    private static final String TAG = "ImageStitchingActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // 你的布局文件

        ImageView imageView = findViewById(R.id.imageView); // 你的ImageView

        try {
            Bitmap bitmap1 = loadBitmapFromResource(R.drawable.image1); // 替换成你的资源ID
            Bitmap bitmap2 = loadBitmapFromResource(R.drawable.image2); // 替换成你的资源ID

            if (bitmap1 == null || bitmap2 == null) {
                Log.e(TAG, "图像加载失败");
                return;
            }

            Mat mat1 = convertBitmapToMat(bitmap1);
            Mat mat2 = convertBitmapToMat(bitmap2);

            if (mat1 == null || mat2 == null) {
                Log.e(TAG, "Bitmap 转 Mat 失败");
                return;
            }

            Mat stitchedMat = stitchImages(mat1, mat2);

            if (stitchedMat == null) {
                Log.e(TAG, "图像拼接失败");
                return;
            }

            Bitmap stitchedBitmap = convertMatToBitmap(stitchedMat);

            if (stitchedBitmap == null) {
                Log.e(TAG, "Mat 转 Bitmap 失败");
                return;
            }

            imageView.setImageBitmap(stitchedBitmap);


            // 释放资源
            mat1.release();
            mat2.release();
            stitchedMat.release();
            bitmap1.recycle();
            bitmap2.recycle();


        } catch (Exception e) {
            Log.e(TAG, "拼接过程中出现错误: " + e.getMessage());
            // 处理异常,例如显示错误消息
        }
    }

    private Bitmap loadBitmapFromResource(int resourceId) {
        InputStream inputStream = getResources().openRawResource(resourceId);
        try {
            return BitmapFactory.decodeStream(inputStream);
        } catch (Exception e) {
            Log.e(TAG, "资源加载失败", e);
            return null;
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                Log.e(TAG, "流关闭失败", e);
            }
        }
    }


    private Mat convertBitmapToMat(Bitmap bitmap) {
        Mat mat = new Mat();
        Utils.bitmapToMat(bitmap, mat);
        return mat;
    }

    private Bitmap convertMatToBitmap(Mat mat) {
        Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
        Utils.matToBitmap(mat, bitmap);
        return bitmap;
    }

    private Mat stitchImages(Mat mat1, Mat mat2) {
        if (mat1.rows() != mat2.rows()) {
            Log.e(TAG, "图像高度不一致,无法拼接");
            return null;
        }
        List<Mat> mats = new ArrayList<>();
        mats.add(mat1);
        mats.add(mat2);
        Mat stitchedMat = new Mat();
        Core.hconcat(mats, stitchedMat);
        return stitchedMat;
    }
}

代码说明:

  • 错误处理: 代码中加入了大量的错误检查,例如图像加载失败、Bitmap 到 Mat 的转换失败、图像高度不一致等情况,并在发生错误时打印日志信息并返回,避免程序崩溃。
  • 资源释放: mat1.release(), mat2.release(), stitchedMat.release(), bitmap1.recycle(), bitmap2.recycle() 这些语句用于释放资源,防止内存泄漏。
  • 类型转换: convertBitmapToMatconvertMatToBitmap 函数封装了 Bitmap 和 Mat 之间的转换,提高代码可读性和可维护性。
  • 图像加载: 使用 loadBitmapFromResource 函数从资源加载图片,并进行错误处理。
  • 高度一致性检查:stitchImages 函数中,添加了对输入图像高度是否一致的检查,这是 Core.hconcat 的必要条件。

使用前请注意:

  • 替换 R.drawable.image1R.drawable.image2 为你实际的图像资源 ID。
  • 确保你的项目已正确配置 OpenCV 库。
  • 在你的布局文件中添加一个 ImageView,并在代码中使用 findViewById 获取其引用。

这个改进后的代码更加健壮,可以更好地避免运行时崩溃,并提供更清晰的错误信息。 即使出现错误,它也只会打印日志并停止执行,不会导致应用程序崩溃。 记住,在处理大型图像时,尤其要注意内存管理,避免内存溢出 (OOM) 错误。

相关推荐
繁依Fanyi1 小时前
Animaster:一次由 CodeBuddy 主导的 CSS 动画编辑器诞生记
android·前端·css·编辑器·codebuddy首席试玩官
奔跑吧 android3 小时前
【android bluetooth 框架分析 02】【Module详解 6】【StorageModule 模块介绍】
android·bluetooth·bt·aosp13·storagemodule
那雨倾城4 小时前
使用 OpenCV 将图像中标记特定颜色区域
人工智能·python·opencv·计算机视觉·视觉检测
whoarethenext4 小时前
c/c++的opencv的图像预处理讲解
人工智能·opencv·计算机视觉·预处理
一律清风4 小时前
【Opencv】canny边缘检测提取中心坐标
c++·opencv
jndingxin7 小时前
OpenCV CUDA模块中矩阵操作------降维操作
人工智能·opencv
田一一一7 小时前
Android framework 中间件开发(三)
android·中间件·framework·jni
jndingxin10 小时前
OpenCV CUDA模块中矩阵操作------归一化与变换操作
人工智能·opencv
androidwork11 小时前
掌握 Kotlin Android 单元测试:MockK 框架深度实践指南
android·kotlin
白熊18812 小时前
【计算机视觉】OpenCV实战项目:基于OpenCV的车牌识别系统深度解析
人工智能·opencv·计算机视觉