Ask Your Question
0

Assertion Failed pyrUp(); pyrDown();

asked 2019-03-07 15:26:26 -0500

Darth Revan gravatar image

updated 2019-03-08 02:26:31 -0500

Hello there. I was trying to upsample and downsample an image with size of 256 x 256. My code is below. While it works perfect for 256 x 256 to 512 x 512, beside this code gives error below.

#include "iostream"  
#include "opencv2/imgproc.hpp"  
#include "opencv2/imgcodecs.hpp"  
#include "opencv2/highgui.hpp"  

int main()  
{  
 cv::Mat image = cv::imread("C:\\Users\\XXX\\Desktop\\OpenCV_Workspace\\cv_Project_I\\block.png");  

cv::Mat imageup2 = image.clone();  
cv::Mat imageup4 = image.clone();  
cv::Mat imageup5_6 = image.clone();  
cv::Mat imagedown0_5 = image.clone();  
cv::Mat imagedown0_25 = image.clone();  
cv::Mat imagedown0_12 = image.clone();  

//Upsample by 2, 4, 5.6  
cv::pyrUp(image, imageup2, cv::Size(image.cols * 2, image.rows * 2 ));  
cv::pyrUp(image, imageup4, cv::Size(image.cols * 4, image.rows * 4));  
cv::pyrUp(image, imageup5_6, cv::Size(image.cols * 5.6, image.rows * 5.6));  

//Downsample by 0.5, 0.25, 0.12  
cv::pyrDown(image, imagedown0_5, cv::Size(image.cols * 0.5, image.rows * 0.5));  
cv::pyrDown(image, imagedown0_25, cv::Size(image.cols * 0.25, image.rows * 0.25));  
cv::pyrDown(image, imagedown0_12, cv::Size(image.cols * 0.12, image.rows * 0.12));  

//Displaying Results  
cv::imshow("Original Image", image);  
cv::imshow("Image Upsampled by 2", imageup4);  
cv::imshow("Image Upsampled by 4", imageup4);  
cv::imshow("Image Upsampled by 5.6", imageup5_6);  
cv::imshow("Image Downsampled by 0.5", imagedown0_5);  
cv::imshow("Image Downsampled by 0.25", imagedown0_25);  
cv::imshow("Image Downsampled by 0.12", imagedown0_12);  
cv::waitKey();  

return 0;  
}

And this is the error code:

OpenCV(4.0.1) Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in cv::pyrUp_, file c:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\pyramids.cpp, line 537   OpenCV: terminate handler is called! The last OpenCV error is: 
OpenCV(4.0.1) Error: Assertion failed (std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2) in cv::pyrUp_, file c:\build\master_winpack-build-win64-vc15\opencv\modules\imgproc\src\pyramids.cpp, line 537

What am I doing wrong? Thanks for help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-03-08 01:38:18 -0500

berak gravatar image

pyrUp() and pyrDown() only work with a factor of 2, you can't do arbitrary up / downscaling like that

(use cv::resize() then !)

you're supposed to use it recursively, like:

Mat a = ...
Mat b; pyrUp(a,b); // * 2
Mat c; pyrUp(b,c); // * 4
Mat d; pyrUp(c,d); // * 8

(there is a Size param in those functions, but you're only allowed to variate it +- 1 pixel, to compensate for odd image sizes when downscaling)

edit flag offensive delete link more

Comments

1

Before I read your answer I already used cv::resize(); and got the results I expected. Now I also tried it with cv::pyrUp(); and cv::PyrDown as you explained. All done. Many thanks.

Darth Revan gravatar imageDarth Revan ( 2019-03-08 02:16:38 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-07 15:26:26 -0500

Seen: 215 times

Last updated: Mar 08