【问题标题】:android opencv crop top image portionandroid opencv裁剪顶部图像部分
【发布时间】:2014-01-05 16:58:56
【问题描述】:

使用 opencv 我想裁剪顶部图像部分并编写此代码。

// crop image
Integer halfWidth = width / 2;
Integer HalfHeight = height / 2;
Integer startX = halfWidth - (halfWidth / 2);
Mat mRgba = new Mat(HalfHeight, halfWidth, CvType.CV_8UC1);
Rect roi = new Rect(0, HalfHeight, width, height);

结果我得到了这个结果并且我的程序关闭了。

Mat [ 360*480*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x5c54da18, dataAddr=0x60aa8010 ]
1549064728
{0, 360, 960x720}

我的旧对象比第一个高。我该如何解决?

【问题讨论】:

    标签: android opencv


    【解决方案1】:

    根据您提供的代码,我猜您的投资回报率应该是:

    Rect roi = new Rect(0, HalfHeight, width, height-HalfHeight);
    

    【讨论】:

    • 现在出现同样的错误 0 Mat [ 360*480*CV_8UC1, isCont=true, isSubmat=false, nativeObj=0x5c430660, dataAddr=0x60b64010 ] 1547896416 {0, 360, 960x360} 360 960跨度>
    【解决方案2】:

    我的完整代码

    protected void calculate(int width, int height, byte[] data)
        {
            Mat mYuv = new Mat(height + height / 2, width, CvType.CV_8UC1);
            Mat thresholdImage = new Mat(height + height / 2, width, CvType.CV_8UC1);
            mYuv.put(0, 0, data);       
            // crop image
            Integer halfWidth = width / 2;
            Integer HalfHeight = height / 2;
            Integer startX = halfWidth - (halfWidth / 2);
            Mat mRgba = new Mat(HalfHeight, halfWidth, CvType.CV_8UC1);
            Rect roi = new Rect(0, HalfHeight, width, height-HalfHeight);
            mRgba = new Mat(mRgba, roi);
            Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420p2BGR, 4);
            //convert to grayscale
            Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_mRGBA2RGBA, 4);
    //       Perform a Gaussian blur (convolving in 5x5 Gaussian) & detect edges
            //Imgproc.GaussianBlur(mRgba, mRgba, new Size(5,5), 2.2, 2);
            Imgproc.Canny(mRgba, thresholdImage, VActivity.CANNY_MIN_TRESHOLD, VActivity.CANNY_MAX_THRESHOLD);
            Mat lines = new Mat();
    
            double rho = 0.15;
            double theta = Math.PI/180;
            int threshold = 50;
    
            //do Hough transform to find lanes
            Imgproc.HoughLinesP(thresholdImage, lines, rho, theta, threshold, VActivity.HOUGH_MIN_LINE_LENGTH, VActivity.HOUGH_MAX_LINE_GAP);
            for (int x = 0; x < lines.cols() && x < 5; x++)
            {
                double[] vec = lines.get(0, x);
                double x1 = vec[0],
                        y1 = vec[1],
                        x2 = vec[2],
                        y2 = vec[3];
                Point start = new Point(x1, y1);
                Point end = new Point(x2, y2);
    
                Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3);
            }
    
            if (bitmap == null)
                bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
            Utils.matToBitmap(mRgba, bitmap);
    
            drawThread.setBitmap(bitmap);
        }
    

    并得到同样的错误。也许在这我需要发送另一个参数或只是克隆到 mRgba

    Mat mRgba = new Mat(HalfHeight, halfWidth, CvType.CV_8UC1);
            Rect roi = new Rect(0, HalfHeight, width, height-HalfHeight);
            mRgba = new Mat(mRgba, roi);
    

    【讨论】:

    • 我觉得这个话题对你有用:stackoverflow.com/questions/16547376/…
    • 你想过这个主意吗? mRgba.copyTo(mRgba.submat(roi));?还是其他?
    • 我认为您应该为 submat 创建另一个 Mat。当您将一个矩阵复制到另一个矩阵时,它们的大小必须相等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多