c++ - OpenCV-2.4.8.2: imshow differs from imwrite -
i'm using opencv2.4.8.2 on mac os 10.9.5. have following snippet of code:
static void compute_weights(const vector<mat>& images, vector<mat>& weights) { weights.clear(); (int = 0; < images.size(); i++) { mat image = images[i]; mat mask = mat::zeros(image.size(), cv_32f); int x_start = (i == 0) ? 0 : image.cols/2; int y_start = 0; int width = image.cols/2; int height = image.rows; mat roi = mask(rect(x_start,y_start,width,height)); // set roi roi.setto(1); weights.push_back(mask); } } static void blend(const vector<mat>& inputimages, mat& outputimage) { int maxpyrindex = 6; vector<mat> weights; compute_weights(inputimages, weights); // find fused pyramid: vector<mat> fused_pyramid; (int = 0; < inputimages.size(); i++) { mat image = inputimages[i]; // build gaussian pyramid weights vector<mat> weight_gaussian_pyramid; buildpyramid(weights[i], weight_gaussian_pyramid, maxpyrindex); // build laplacian pyramid original image mat float_image; inputimages[i].convertto(float_image, cv_32fc3, 1.0/255.0); vector<mat> orig_guassian_pyramid; vector<mat> orig_laplacian_pyramid; buildpyramid(float_image, orig_guassian_pyramid, maxpyrindex); (int j = 0; j < orig_guassian_pyramid.size() - 1; j++) { mat sized_up; pyrup(orig_guassian_pyramid[j+1], sized_up, size(orig_guassian_pyramid[j].cols, orig_guassian_pyramid[j].rows)); orig_laplacian_pyramid.push_back(orig_guassian_pyramid[j] - sized_up); } // last lapalcian layer same gaussian layer orig_laplacian_pyramid.push_back(orig_guassian_pyramid[orig_guassian_pyramid.size()-1]); // convolve laplacian original guassian weights vector<mat> convolved; (int j = 0; j < maxpyrindex + 1; j++) { // create 3 channels weight gaussian pyramid vector<mat> gaussian_3d_vec; (int k = 0; k < 3; k++) { gaussian_3d_vec.push_back(weight_gaussian_pyramid[j]); } mat gaussian_3d; merge(gaussian_3d_vec, gaussian_3d); //mat convolved_result = weight_gaussian_pyramid[j].clone(); mat convolved_result = gaussian_3d.clone(); multiply(gaussian_3d, orig_laplacian_pyramid[j], convolved_result); convolved.push_back(convolved_result); } if (i == 0) { fused_pyramid = convolved; } else { (int j = 0; j < maxpyrindex + 1; j++) { fused_pyramid[j] += convolved[j]; } } } // blending (int = (int)fused_pyramid.size()-1; > 0; i--) { mat sized_up; pyrup(fused_pyramid[i], sized_up, size(fused_pyramid[i-1].cols, fused_pyramid[i-1].rows)); fused_pyramid[i-1] += sized_up; } mat final_color_bgr; fused_pyramid[0].convertto(final_color_bgr, cv_32f, 255); final_color_bgr.copyto(outputimage); imshow("final", outputimage); waitkey(0); imwrite(outputimagepath, outputimage); }
this code doing basic pyramid blending 2 images. key issues related imshow , imwrite in last line. gave me drastically different results. apologize displaying such long/messy code, afraid difference coming other parts of code can subsequently affect imshow , imwrite.
the first image shows result imwrite , second image shows result imshow, based on code given. i'm quite confused why case.
i noticed when these:
mat float_image; inputimages[i].convertto(float_image, cv_32fc3, 1.0/255.0); imshow("float image", float_image); imshow("orig image", image);
they show same thing, both show same picture in original rgb image (in image).
imwrite functionality
by default, imwrite, converts input image only 8-bit (or 16-bit unsigned (cv_16u) in case of png, jpeg 2000, , tiff) single-channel or 3-channel (with ‘bgr’ channel order) images can saved using function. whatever format feed in imwrite, blindly converts cv_8u range 0(black) - 255(white) in bgr format.
imshow - problem
so when noticed function, fused_pyramid[0].convertto(final_color_bgr, cv_32f, 255);
fused_pyramid under mat type 21 (floating point cv_32f). tried convert floating point scale factor 255. scaling factor 255 caused problem @ imshow. instead visualize, can directly feed in fused_pyramid without conversion scaled floating point between 0.0(black) - 1.0(white).
hope helps.