pnus
pnus

Reputation: 197

OpenCV resize quality

I am trying to resize a set of images using OpenCV. The resolution is correct, but the quality of the image degrades. However, if I do a manual resize through Apple Preview, the image quality is not affected. I am making all of these images smaller, rather than blowing them up. Here are two images comparing the quality. Please note the jagged spokes and jagged top tube of the bike:

Bad Resize:

Bad OpenCV resize

Good resize:

Good Apple Preview resize

Here is my code:

cv::Mat srcimg = cv::imread(src_filepath);
cv::Mat destimg = cv::imread(lowres_path);
cv::Size img_size = srcimg.size();
int img_width = img_size.width;
int img_height = img_size.height;
double percentage = 1000.0/(img_width);
double new_height = img_height * percentage;
cv::Size new_size = cv::Size(1000.0, new_height);
cv::resize(srcimg, destimg, new_size,0,0,CV_INTER_LANCZOS4);
cv::imwrite(lowres_path, destimg);

I've messed with the the interpolation but that seems to have no effect on the image.

Upvotes: 3

Views: 3430

Answers (1)

pnus
pnus

Reputation: 197

All right, figured it out. I switched it back to CV_INTER_AREA and explicitly set the compression settings:

cv::resize(srcimg, destimg, new_size,0,0,CV_INTER_AREA);
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
cv::imwrite(lowres_path, destimg);

Upvotes: 3

Related Questions