更改cv :: Mat图像的类型(解释) [英] Changing the type (interpretation) of cv::Mat image

查看:484
本文介绍了更改cv :: Mat图像的类型(解释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论出于何种原因,我有一个 .tiff 图像,由OpenCV cv :: imread CV_16U 键入 CV_16S 。我知道这是不正确的,因为我的数据被解释给我(图像应该包含-9999和一个正的最大值的虚拟值),我得到正确的值,当输入 Matlab

For whatever reason, I have a .tiff image which is incorrectly input by OpenCV cv::imread as CV_16U type instead CV_16S. I know it is incorrect, because my data was explained to me (the image should contain dummy values of -9999 and a positive max value), and I get the correct values when inputing in Matlab.

我仍然可以通过eg来处理 .at< type> 函数自己,因为我知道真正的类型,所以我可以使用 .at< short> 。但是, cv :: Mat :: type()是错误的,这是不可取的,如果我使用其他函数进一步处理,可能依赖这个参数(其中处理可能依赖于 cv :: Mat :: type())。

I can still handle it through e.g. the .at<type> function myself as I know the real type, so I can use .at<short>. However, the cv::Mat::type() is wrong which is not desirable if I use other functions for further processing which might rely on this parameter (where the processing might depend on cv::Mat::type()).

如何更改 cv :: Mat :: type()而不转换图像?也就是说,我不希望从 unsigned short 表示重新计算值,但是只需要更改 的读取方式即可。

How can I change the cv::Mat::type() without converting the image? That is, I do not want the values to be re-calculated from the unsigned short to a short representation, but simply the way they are read to be changed.

如何更改与图片相关联的 cv :: Mat :: type() (但不是简单地将图片转换为其他类型)。

How do I change the cv::Mat::type() associated to the image. (but not simply convert the image to a different type).

这里是一些示例代码,它的输出来说明问题:

Here is some example code and it's output to illustrate the problem:

cv::Mat test = cv::imread(argv[1], CV_LOAD_IMAGE_ANYDEPTH);
if (test.type() == CV_16U){ // true
    std::cerr << (short)(*std::min_element(test.begin<short>(),
                                           test.end<short>()))
              << std::endl;
    std::cerr << (short)(*std::max_element(test.begin<short>(),
                                           test.end<short>()))
              << std::endl;
    // output is OK, "-9999" and "1645"

    std::cerr << (unsigned short)
                 (*std::min_element(test.begin<unsigned short>(),
                                    test.end<unsigned short>()))
              << std::endl;
    std::cerr << (unsigned short)
                 (*std::max_element(test.begin<unsigned short>(),
                                    test.end<unsigned short>()))
              << std::endl;
    // output is not OK: "1" and "55537"

    cv::Mat test2;
    test.convertTo(test2, CV_16S);
    // also tried:
    // test.assignTo(test2, CV_16S);

    std::cerr << (short)(*std::min_element(test2.begin<short>(),
                                           test2.end<short>()))
              << std::endl;
    std::cerr << (short)(*std::max_element(test2.begin<short>(),
                                           test2.end<short>()))
              << std::endl;
    // output is not OK: "1" and "32767"

    test.type = CV_16U; // what I would like to do
}


推荐答案

OpenCV默认情况下使数据成员公开(虽然你通常不想自己搞砸它们)。继续尝试;如果它工作伟大!如果不是很好...

OpenCV leaves its data members public by default (though you generally don't want to mess with them yourself). Go ahead and give this a try; if it works Great! if not well...

警告:未经测试和黑客解决方案

Warning: untested and hackish solution

test.flags = (test.flags & ~CV_MAT_TYPE_MASK) | CV_16S;

这篇关于更改cv :: Mat图像的类型(解释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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