Ema*_*ele 4 c++ opencv image-processing c++11 opencv3.0
我正在尝试读取.tif或.tiff浮点灰度图像OpenCV。
我可以读取和写入常规文件格式,例如等png,jpg但我无法从桌面读取我以前从未使用过的格式(即.tif或.tiff格式)。
图像:我尝试读取的图像具有以下参数: 大小:

以及宽度和高度:

在一些文档和各种来源之后,我了解到可以使用函数在可用数据类型之间进行转换,可以在此处convertTo找到来源。然而这并没有很好地工作,我实际上有一个编译错误:
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: error: (-215) size.width>0 && 大小。函数 imshow 中高度>0
我正在使用的代码如下:
#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();
}
Run Code Online (Sandbox Code Playgroud)
我尝试使其工作的其他示例直接来自可以在此处OpenCV找到的文档,但进行了一些小修改。我从官方文档中读到,这些选项也应该被激活,事实上我在下面的第二次附加试验中就是这样做的: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;
Run Code Online (Sandbox Code Playgroud)
这次编译器运行时没有任何错误,但没有显示图像,可以从下面的打印屏幕中看到:

更新
这是执行后的结果cv::resize

更新2
这是申请后的结果imshow("Display window", image*10);

官方文档中是否有我遗漏的内容或我忘记做的其他事情?感谢您阐明这个问题。
您的图像由 64 位浮点数的单通道组成,范围从-219.774到-22.907。我可以告诉使用tiffutil的是libtiff:
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
...
...
Run Code Online (Sandbox Code Playgroud)
我不确定你到底打算做什么,但作为第一次尝试,你可以将 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()
Run Code Online (Sandbox Code Playgroud)