【问题标题】:How to read a .tif floating point gray scale image in openCV如何在openCV中读取.tif浮点灰度图像
【发布时间】:2019-11-13 12:30:15
【问题描述】:

我正在尝试读取OpenCV 中的.tif.tiff 浮点灰度图像。

我可以读取和写入诸如pngjpg 等常规文件格式,但我无法从我的桌面读取我以前从未使用过的格式,即.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


【解决方案1】:

您的图像由单个 64 位浮点通道组成,范围从 -219.774-22.907。我可以看出使用libtiff 附带的tiffutil

tiffutil -verboseinfo  image.tif

TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 42113 (0xa481) encountered.
Directory at 0x256b3a2
  Image Width: 2277 Image Length: 2153
  Bits/Sample: 64
  Sample Format: IEEE floating point
  Compression Scheme: none
  Photometric Interpretation: "min-is-black"
  Samples/Pixel: 1
  Rows/Strip: 1
  Number of Strips: 2153
  Strips (Offset, ByteCount):
     17466, 18216
     35682, 18216
     53898, 18216
     ...
     ...

我不确定您打算做什么,但作为第一次尝试,您可以将 220 添加到每个像素并转换为 unsigned char,您的范围将是 0 到 197,这是完全可显示的:

我实际上是使用 Python 完成的,因为我使用它的速度更快,但 C++ 将遵循完全相同的格式:

import cv2

# Load image
img = cv2.imread('image.tif',cv2.IMREAD_UNCHANGED)

# Add 220 to all values, round to unsigned 8-bit and display
Image.fromarray((img+220).astype(np.uint8)).show()

【讨论】:

  • 谢谢。这正是图像。你能发布更新的代码吗?
  • 我打算用ROS将该图像(这是一个水下调查)传递到grid。事实上,我可以使用一个函数GridMapCvConverter 来利用该调查并变成一个网格。
  • 你能把你用的python脚本贴出来吗?非常感谢您花时间帮助解决这个问题。非常感谢! :)
  • 哎呀,我明白了,谢谢! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2012-06-13
  • 2014-04-05
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
相关资源
最近更新 更多