Austin Gayler
Austin Gayler

Reputation: 4366

Android app crashes when calling OpenCV accumulateWeighted function

I have the following code (attempted port of this)

    VideoCapture sequence = new VideoCapture(fp + "%02d" + ".jpg");
    if (!sequence.isOpened())
        dbg("Failed to open images!");
    }

    Mat outImg = null;
    Mat curImg = null;
    while (sequence.read(curImg)) {
        Imgproc.accumulateWeighted(curImg, outImg, 0.01);

    }

    imwrite(fp + "median" + "-" + curTime + ".jpg", outImg);

When it gets to this point all the images are in fp/01.jpg, 02.jpg, etc. It crashes when it gets to the sequence.read(curImg) line, so I am not sure what's wrong since it already checks to make sure the sequence has been opened properly.

Edit: More specifically, the error I'm getting from the Android debugger is

CvException [org.opencv.core.CvException: cv::Exception: /builds/master_pack-android/opencv/modules/imgproc/src/accum.cpp:1108: error: (-215) _src.sameSize(_dst) && dcn == scn in function void cv::accumulateWeighted(cv::InputArray, cv::InputOutputArray, double, cv::InputArray)]

on the accumulateWeighted method call.

Upvotes: 2

Views: 778

Answers (2)

Logic1
Logic1

Reputation: 1847

Stayed up pretty late and finally found a working solution. First off my input image types are CV_8UC1 and CV_8UC4. So I needed to color-convert my images according to these rules:

if mat == grayscale == CV_8UC1 then convert to CV_32F
if mat == color == CV_8UC4 then convert to CV_32FC4

Example implementation:

private Mat accuImg;
public Mat accumulateImageBG(Mat img, int type){
    if (accuImg==null){//code snipit from berak's answer
        accuImg = Mat.zeros(img.size(), type);
    }
    img.convertTo(img, type);
    Imgproc.accumulateWeighted(img, accuImg, 0.1);
    Core.convertScaleAbs(accuImg, img);
    return img;
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {//new frame from camera
    Mat col = inputFrame.rgba();//in:CV_8UC4 out:CV_32FC4
    Mat gray = inputFrame.gray();//in:CV_8UC1 out:CV_32F

    //be sure to specify conversion type with matching # of channels as input image!
    Mat img = accumulateImageBG(gray,CvType.CV_32F);
    return img;
}

Upvotes: 2

berak
berak

Reputation: 39806

Mat outImg = null;
Mat curImg = new Mat();
while (sequence.read(curImg)) {
    // you can't pass an empty img to accumulateWeighted()
    if (outImg==null)
        outImg = Mat.zeros(curImg.size(), curImg.type());
    Imgproc.accumulateWeighted(curImg, outImg, 0.01);

}

Upvotes: 1

Related Questions