c++ - Issue with imread. Strange float values -
i'm trying simple load image (tiff) , display pixel value.
if open image using imagej values 32-bit float. opening same image using opencv strange float values, e.g. 4.2039e-44.
if read value of specific pixel using "int" presented value correct. below code use test. , here link image: https://goo.gl/wmv9xe.
thanks in advance.
#include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> #include <iostream> int main(int argc, char **argv) { std::string imagefile = "image.tiff"; cv::mat image; image = cv::imread(imagefile, cv_load_image_anydepth); // read file if (!image.data) // check invalid input { std::cout << "could not open or find image: " << imagefile << std::endl; return -1; } std::cout << "image:" << image.rows << " x " << image.cols << " channels: " << image.channels() << " depth: " << image.depth() << std::endl; std::cout << "value @ 0,0: " << image.at<float>(0,1)<< std::endl; // strange value std::cout << "value @ 0,0: " << image.at<int>(0,1)<< std::endl; // correct value return (exit_success); } * update *
trying move forward code, decided create function convert data "int" reading file. temporary solution worked, i'm still looking reason why data being loaded wrong.
int main(int argc, char **argv) { std::string imagefile = "/home/slepicka/xsconfig/image.tiff"; cv::mat image = openimage(imagefile); if (!image.data) // check invalid input { std::cout << "could not open or find image: " << imagefile << std::endl; return -1; } std::cout << "image:" << image.rows << " x " << image.cols << " channels: " << image.channels() << " depth: " << image.depth() << " type: " << image.type() << std::endl; std::cout << "value @ 0,0: " << image.at<int>(0,1)<< std::endl; // correct value //std::cout << "data: " << image << std::endl; return (exit_success); } cv::mat openimage(std::string filename){ cv::mat imageload = cv::imread(filename, cv_load_image_anydepth); if(imageload.type() == cv_32f){ return converttoint(imageload); } return imageload; } cv::mat converttoint(cv::mat source){ int r, c; cv::mat converted; converted.create(source.rows, source.cols, cv_32sc1); (r=0; r<source.rows;r++) { (c=0; c<source.cols;c++) { converted.at<int>(r, c) = source.at<int>(r, c); } } return converted; }
Comments
Post a Comment