【问题标题】:How to crop a image from the selected contour in opencv android如何从opencv android中的选定轮廓裁剪图像
【发布时间】:2015-11-18 05:33:03
【问题描述】:

在我的项目中,我想从实时摄像机源中选择一个对象,然后仅为该选定区域创建一个 JPEG 文件。所以在 onCameraFrame 方法中,我在Imgproc.drawContours(mRgba, contours, 0, CONTOUR_COLOR); 中设置了参数 0 来选择第一个项目,即所选项目。然后我尝试为该项目实现一个绑定矩形。

OnCameraFrame()

公共垫 onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba();

    if (mIsColorSelected) {
        mDetector.process(mRgba);
        List<MatOfPoint> contours = mDetector.getContours();
        Log.e(TAG, "Contours count: " + contours.size());
        Imgproc.drawContours(mRgba, contours, 0, CONTOUR_COLOR);

        Mat colorLabel = mRgba.submat(4, 68, 4, 68);
        colorLabel.setTo(mBlobColorRgba);



        MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(0).toArray());
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);
        Mat selectedRegion = mRgba.submat(rect);

        Bitmap bmp = null;
        try {

            bmp = Bitmap.createBitmap(selectedRegion.cols(), selectedRegion.rows(), Bitmap.Config.ARGB_8888);
            convertBitmapToImage(bmp);

        } catch (CvException e) {
            Log.d("Exception", e.getMessage());
        }


    }


    return mRgba;
}  

然后将mat对象转换为位图并保存为JPEG格式

convertBitmapToImage

private void convertBitmapToImage(Bitmap bmp) {

        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Dummy");
        if (!root.exists()) {
            root.mkdirs();
        } else {
            System.out.print("Exists");
        }

        File f = new File(root, "filename.jpeg");
        //Convert bitmap to byte array
        Bitmap bitmap = bmp;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 60, bos);
        byte[] bitmapdata = bos.toByteArray();


        try {

            f.createNewFile();

        } catch (IOException e) {
            e.printStackTrace();
        }



        //write the bytes in file
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            fos.write(bitmapdata);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }


    } 

但它会创建一个空白的 JPEG 文件

我做错了什么或需要做什么

【问题讨论】:

  • 检查该图像是否保存在外部存储中并且您正在获取该图像的路径
  • 这是我刚刚发布的图片路径中保存的图片!!

标签: android opencv


【解决方案1】:

我没有检查您的代码,但我认为问题出在您的 bmp 上。 你应该这样使用:

Bitmap bmp = Bitmap.createBitmap(src.cols(), src.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(src, bmp);

在您的 convertBitmapToImage 方法之前。 src 是您想要保存为图像文件的 Mat 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 2014-04-24
    • 2019-10-16
    • 1970-01-01
    • 2020-09-07
    • 2021-09-06
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多