android opencv裁剪顶部图像部分

时间:2014-01-05 16:58:57

标签: android opencv

使用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}

我的旧物体高于第一个。我怎么能解决它们?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我的完整代码

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);