OpenCv 多光谱图像 openCV [英] OpenCv Multispectral Image openCV

查看:216
本文介绍了OpenCv 多光谱图像 openCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将具有多个波段的图像 .TIF 导入 o​​penCV (c++).使用命令 imread 它只显示第一个波段.我如何访问其他人?

I am trying to import into openCV (c++) an image .TIF which has several bands. Using command imread it shows just the first band. How can I access to the others?

此外,我尝试使用 ifstream 访问该文件,但看起来我犯了一些错误!

More over I tried to access the file with ifstream but it looks like I made some mistakes!

感谢您的帮助!

最佳

推荐答案

OpenCV 目前不支持多页图像读取.它只会读取第一张图片.

OpenCV currently doesn't support multipage image reading. It will read only the first image.

对于 C++ .TIFF 阅读,libtiff 有一组很好的例子.Imagemagick 也有 C++ 支持.您可以读取图像并将数据缓冲区复制到 OpenCV Mat 中.

For C++ .TIFF reading, libtiff has nice set of examples. Imagemagick also has C++ support. You can read images and copy the data buffer into OpenCV Mat.

以下是使用 imagemagick 的 Magick++ 例程的示例 C++ 代码:

Here is a sample c++ code that uses imagemagick's Magick++ routines:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <string>
#include <Magick++.h>
#include <sstream>
#include <exception>

using namespace Magick;
using namespace std;
using namespace cv;

template < typename T > std::string to_string( const T& n )
{
    std::ostringstream stm ;
    stm << n ;
    return stm.str() ;
}

vector <Mat> read_images( string filename, int num=1, string dpi="300" ) {

    vector <Mat> ret;
    Image image;
    image.density(dpi);

    int cols, rows;
    int i = 0; 
    while( i < num ) {
        cout << filename + "[" + to_string(i) + "]" << endl;
        try { 
            image.read(filename + "[" + to_string(i) + "]");
        }catch ( exception ex ) {
            cout << "read " << i << " pages" << endl;
            break;
        }
        i++;
        cols = image.columns();
        rows = image.rows();
        char* blob = new char[cols*rows*3];

        image.write(0,0, cols, rows, "RGB", MagickCore::CharPixel, blob);

        ret.push_back(Mat(rows, cols, CV_8UC3, blob));
    }

    return ret; 
}


int main ( int argc, char** argv ) {
    vector<Mat> images = read_images(argv[1], 10);

    for( int i = 0; i < images.size(); i++ ) {
        imshow("image", images[i]);
        waitKey();
    } 
}

这篇关于OpenCv 多光谱图像 openCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆