简体   繁体   English

OpenCv多光谱图像openCV

[英]OpenCv Multispectral Image openCV

I am trying to import into openCV (c++) an image .TIF which has several bands. 我正在尝试将具有多个波段的图像.TIF导入openCV(c ++)。 Using command imread it shows just the first band. 使用命令读取,它仅显示第一个波段。 How can I access to the others? 如何访问其他人?

More over I tried to access the file with ifstream but it looks like I made some mistakes! 此外,我尝试使用ifstream访问文件,但看起来我犯了一些错误!

Thanks for your help,! 谢谢你的帮助,!

Best 最好

OpenCV currently doesn't support multipage image reading. OpenCV当前不支持多页图像读取。 It will read only the first image. 它将仅读取第一张图像。

For C++ .TIFF reading, libtiff has nice set of examples. 对于C ++ .TIFF的阅读, libtiff提供了很多示例。 Imagemagick also has C++ support. Imagemagick还具有C ++支持。 You can read images and copy the data buffer into OpenCV Mat. 您可以读取图像并将数据缓冲区复制到OpenCV Mat中。

Here is a sample c++ code that uses imagemagick's Magick++ routines: 这是一个使用imagemagick的Magick ++例程的示例c ++代码:

#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();
    } 
}

Some code would be helpful here to see what went wrong. 一些代码在这里有助于发现问题所在。

However, it seems that you are accessing only the first band instead of all the bands you want. 但是,您似乎只访问第一个波段,而不访问所有想要的波段。

Try something like this (This example has 3 bands, thus Vec3b): 尝试这样的事情(此示例有3个频段,因此为Vec3b):

Vec3b image = imread(filePath, CV_LOAD_IMAGE_UNCHANGED);

This stores the image in a 3-band vector (There are several types with different amounts of bands, like Vec4b or Vec5b). 这会将图像存储在3波段矢量中(有几种类型的波段数量不同,例如Vec4b或Vec5b)。 You can then access each band just like accessing an element in a vector: 然后,您可以访问每个波段,就像访问向量中的元素一样:

image[0]
image[1]
image[2]

If what I am assuming is correct, you are attempting to access by using something along the lines of 如果我假设是正确的,则您正在尝试通过以下方式使用某些内容:

int bandValue = (int)image;

or something similar. 或类似的东西。 Remember that referencing the name of a vector (or array) is the same as accessing the first element in that vector (or array) 请记住,引用向量(或数组)的名称与访问该向量(或数组)中的第一个元素相同

image = image[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM