简体   繁体   中英

iOS & OpenCV Error: Assertion Failed in PhaseCorrelateRes

I am trying to detect the shift between 2 images using OpenCV in iOS. The function that I used is phaseCorrelate, which is supposed to return the Point2d given the 2 cv::Mat images. I followed the sample code here by converting the UIImage to Mat, and then convert the Mat to CV_32F type. But I kept on getting this error:

OpenCV Error: Assertion failed (src1.type() == CV_32FC1 || src1.type() == CV_64FC1) in            phaseCorrelateRes, file /Users/alexandershishkov/dev/opencvIOS/opencv-2.4.7/modules/imgproc/src/phasecorr.cpp, line 498
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/alexandershishkov/dev/opencvIOS/opencv-2.4.7/modules/imgproc/src/phasecorr.cpp:498: error: (-215) src1.type() == CV_32FC1 || src1.type() == CV_64FC1 in function phaseCorrelateRes

I don't understand why I got that error, because I have converted the Mat type to CV_32F . FYI: the reason why I didn't convert to CV_64F is because it cost huge memory and the app in iOS will immediately closed due to heavy memory.

Here's the snippet of my code where error occurred (at phaseCorrelate call):

#ifdef __cplusplus
-(void)alignImages:(NSMutableArray *)camImages
{
int i;
Mat matImages, refMatImage, hann;
Point2d pcPoint;

for (i = 0; i < [camImages count]; i++) {
    if(i == 0){
        UIImageToMat([camImages objectAtIndex:i], refMatImage);
        refMatImage.convertTo(refMatImage, CV_32F);
        createHanningWindow(hann, refMatImage.size(), CV_32F);
    }
    else{
        UIImageToMat([camImages objectAtIndex:i], matImages);
        matImages.convertTo(matImages, CV_32F);

        pcPoint = phaseCorrelate(refMatImage, matImages, hann);
        NSLog(@"phase correlation points: (%f,%f)",pcPoint.x, pcPoint.y);
    }
}
NSLog(@"Done Converting!");
}
#endif

Nevermind, this was actually caused by the fact that the UIImage has 3 channels in the first place. When converted into Mat and to CV_32F type, the resulting Mat was actually of type CV_32FC3 (3 channels); Therefore an error occurred as the parameter type did not match.

My solution is to split the original Mat into array of channels, then pass ONE channel only to the phaseCorrelate function:

vector<Mat> refChannels;
split(refMatImage, refChannels);
phaseCorrelate(refChannels[0],...);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM