c++ - OpenCV image conversion goes wrong -
i have algorithm stuff. among them, there conversion works fine if i'm working on cv_8uc3
image goes wrong if file type c_16uc3
. code:
//new image created mat3w img(100,100,vec3w(1000,0,0)); //image conversion - error! cv::mat inputsource; //saving image here work img.convertto(inputsource, cv_64fc3); //saving image here not work -> black image
the problem cv_16uc3
image's processing result image of right dimensions black. problem in conversion because saving image right before give legit 1 while saving right after give white one.
edit:
i made changes: cut off useless code , added inputsource declaration. now, while trying stuff, arrived @ conclusion either haven't understood cv types, or strange happening. thought number in type indicating number of bits per channel. so, in head, cv_16uc3
3 channel 16bits per channel. idea strengthened fact image save during tests (before img.convertto) had matching bits per channel number. strange thing, saved inputsource (type cv_64fc3
) 8bpc image.
what's missing?
you confused way imwrite
, imread
work in opencv. opencv documentation
imwrite
the function imwrite
saves image specified file. image format chosen based on filename extension (see imread()
list of extensions). 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. if format, depth or channel order different, use mat::convertto()
, , cvtcolor()
convert before saving. or, use universal filestorage
i/o functions save image xml or yaml format.
imread
the function imread loads image specified file , returns it. possible flags are:
- imread_unchanged : if set, return loaded image (with alpha channel, otherwise gets cropped).
- imread_grayscale : if set, convert image single channel grayscale image.
- imread_color : if set, convert image 3 channel bgr color image.
- imread_anydepth : if set, return 16-bit/32-bit image when input has corresponding depth, otherwise convert 8-bit.
- imread_anycolor : if set, image read in possible color format.
so case, cv_16u
saved without conversion, while cv_64f
converted , saved cv_8u
. if want store double
data, should use filestorage
. should take care use imread
image appropriate flag.
this example should clarify:
#include <opencv2\opencv.hpp> using namespace cv; int main() { // create 16-bit 3 channel image mat3w img16uc3(100, 200, vec3w(1000, 0, 0)); img16uc3(rect(0, 0, 20, 50)) = vec3w(0, 2000, 0); // convert 64-bit (double) 3 channel image mat3d img64fc3; img16uc3.convertto(img64fc3, cv_64fc3); // save disk imwrite("16uc3.png", img16uc3); // no conversion imwrite("64fc3.png", img64fc3); // converted cv_8uc3 filestorage fout("64fc3.yml", filestorage::write); fout << "img" << img64fc3; // no conversion fout.release(); mat img_maybe16uc3_a = imread("16uc3.png" /*, imread_color*/); // cv_8uc3 mat img_maybe16uc3_b = imread("16uc3.png", imread_anydepth); // cv_16uc1 mat img_maybe16uc3_c = imread("16uc3.png", imread_unchanged); // cv_16uc3 mat img_maybe64fc3_a = imread("64fc3.png" /*, imread_color*/); // cv_8uc3 mat img_maybe64fc3_b = imread("64fc3.png", imread_anydepth); // cv_8uc1 mat img_maybe64fc3_c = imread("64fc3.png", imread_unchanged); // cv_8uc3 mat img_mustbe64fc3; filestorage fin("64fc3.yml", filestorage::read); fin["img"] >> img_mustbe64fc3; // cv_64fc3 fin.release(); return 0; }
Comments
Post a Comment