我正在尝试简单加载图像(TIFF)并显示像素值。
如果我使用ImageJ打开图像,则值为32位浮点数。但是使用opencv打开相同的图像我得到了非常奇怪的浮动值,例如4.2039e-44。
如果我使用“int”读取特定像素的值,则显示的值是正确的。下面是我用来测试的代码。这里有一个图片链接:https://goo.gl/Wmv9xE。
提前致谢。
#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 the file
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the 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 at 0,0: " << image.at<float>(0,1)<< std::endl; // Strange Value
std::cout << "Value at 0,0: " << image.at<int>(0,1)<< std::endl; // Correct Value
return (EXIT_SUCCESS);
}
*更新*
尝试继续使用代码,我决定创建一个函数将数据转换为从文件中读取的“int”。 作为一个临时解决方案,这有效,但我仍然在寻找数据加载错误的原因。
int main(int argc, char **argv) {
std::string imageFile = "/home/slepicka/XSConfig/image.tiff";
cv::Mat image = openImage(imageFile);
if (!image.data) // Check for invalid input
{
std::cout << "Could not open or find the 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 at 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);
for (r=0; r<source.rows;r++) {
for (c=0; c<source.cols;c++) {
converted.at<int>(r, c) = source.at<int>(r, c);
}
}
return converted;
}