【发布时间】:2019-11-13 12:30:15
【问题描述】:
我正在尝试读取OpenCV 中的.tif 或.tiff 浮点灰度图像。
我可以读取和写入诸如png、jpg 等常规文件格式,但我无法从我的桌面读取我以前从未使用过的格式,即.tif 或.tiff 格式。
图片:我要读取的图片有以下参数: 尺寸:
还有宽高:
经过一些文档和各种来源后,我了解到可以使用convertTo 函数在可用数据类型之间进行转换,可以在here 找到来源。然而这并没有很好地工作,我实际上有一个编译错误说:
OpenCV(3.4.1) 错误:imshow 中的断言失败 (size.width>0 && size.height>0),文件 /home/to/opencv/modules/highgui/src/window.cpp,第 356 行 在抛出 cv::Exception 的实例后调用终止 what(): OpenCV(3.4.1) /home/to/opencv/modules/highgui/src/window.cpp:356: 错误: (-215) size.width>0 && size.height>0 in function imshow
我使用的代码如下:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat img = imread("/home/to/Desktop/example.tif");
cv::imshow("source",img);
Mat dst; // destination image
// check if we have RGB or grayscale image
if (img.channels() == 3) {
// convert 3-channel (RGB) 8-bit uchar image to 32 bit float
img.convertTo(dst, CV_32FC3);
}
else if (img.channels() == 1) {
// convert 1-chanel (grayscale) 8-bit uchar image to 32 bit float
img.convertTo(dst, CV_32FC1);
}
// display output, note that to display dst image correctly
// we have to divide each element of dst by 255 to keep
// the pixel values in the range [0,1].
cv::imshow("output",dst/255);
waitKey();
}
我试图使其工作的其他示例直接来自OpenCV 文档,该文档可以在here 中找到,不过稍作修改。我从official documentation 读到,选项IMREAD_ANYCOLOR | IMREAD_ANYDEPTH 也应该被激活,事实上我在下面的第二个附加试验中就是这样做的:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
String imageName( "/home/to/Desktop/example.tif" ); // by default
if( argc > 1)
{
imageName = argv[1];
}
Mat image;
Mat outImage;
image = imread( imageName, IMREAD_ANYCOLOR | IMREAD_ANYDEPTH ); // Read the file
if( image.empty() ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
resize(image, outImage, cv::Size(500,500));
imshow("orig", image);
imshow("resized", outImage);
// Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
这次编译器运行没有任何错误,但没有显示图像,这可以从下面的打印屏幕中看到:
更新
这是cv::resize之后的结果
更新 2
这是申请imshow("Display window", image*10);后的结果
我是否在官方文档中遗漏了什么或者我忘记做的其他事情? 感谢您阐明这个问题。
【问题讨论】:
-
您可能需要缩放图像以进行显示。顺便说一句:这不是编译错误,而是运行时错误。
-
@CrisLuengo,感谢您花时间阅读我的问题。我应该在代码中添加什么来显示图像?
-
因为你有一个全黑的图像显示,将图像乘以某个值应该使它足够亮,可以看到。尝试
imshow( "Display window", image*10),然后增加该值直到你看到一些东西。但是,如果imread将浮点值转换为 uint8,则信息可能会丢失。 -
[“缩放图像”是指缩放强度,而不是几何形状。]
-
我调整了图像的大小,但仍然看到黑色的结果
标签: c++ opencv c++11 image-processing opencv3.0