C++ minMaxLoc 数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14364563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:16:10  来源:igfitidea点击:

minMaxLoc datatype

c++image-processingopencvcomputer-vision

提问by Lakshmi Narayanan

I have been stumped by this for quite some time. could you please help me to know, what datatype does the minMaxLoc() in Opencv consider, while finding the max, min vlaues of a Mat variable? I got a certain value from the function, but I have no idea wat that value actually represents, and in what data type?

我已经被这个难住了很长一段时间。你能帮我知道,Opencv 中的 minMaxLoc() 考虑什么数据类型,同时找到一个 Mat 变量的最大、最小 vlaues 吗?我从函数中获得了某个值,但我不知道该值实际代表什么,以及使用什么数据类型?

    Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
    minMaxLoc(dst,&min,&estimate,&minLoc,&maxLoc,noArray());

The value of 'estimate' is somewhere around 1000's, while if I try to access the value of 'dst' Mat variable, using

'estimate' 的值在 1000 左右,而如果我尝试访问 'dst' Mat 变量的值,则使用

    dst.at<datatype>(k,l)

Am getting vague values, starting from 124, 125 for uchar, to 2,xxx,xxx,xxx if I use long int. What is the value actually given by the minMaxLoc function? Please help me.

如果我使用长整数,我会得到模糊的值,从 uchar 的 124、125 到 2,xxx,xxx,xxx。minMaxLoc 函数实际给出的值是多少?请帮我。

回答by Hammer

min and estimate should be of type double and I would imagine they are correct. The issue is probably with you accessing

min 和estimate 应该是double 类型,我想它们是正确的。问题可能与您访问有关

dst.at<datatype>(k,l)

As Abhishek Thakur mentioned, the output depends on your input. If you are ever confused about the type of a matrix you can look at dst.type() which returns an integer corresponding to the list defined in types_c.hstarting at line 557. The definitions for single channel types or "depths" are

正如 Abhishek Thakur 所提到的,输出取决于您的输入。如果您对矩阵的类型感到困惑,您可以查看 dst.type(),它返回一个整数,该整数对应于types_c.h中定义的列表,从第 557 行开始。单通道类型或“深度”的定义是

#define CV_8U   0
#define CV_8S   1 
#define CV_16U  2
#define CV_16S  3
#define CV_32S  4
#define CV_32F  5
#define CV_64F  6

You can see the formula used to calculate the other type identifiers on line 573

您可以在第 573 行看到用于计算其他类型标识符的公式

#define CV_CN_SHIFT   3
#define CV_DEPTH_MAX  (1 << CV_CN_SHIFT)

#define CV_MAT_DEPTH_MASK       (CV_DEPTH_MAX - 1)
#define CV_MAT_DEPTH(flags)     ((flags) & CV_MAT_DEPTH_MASK)

#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))

for example

例如

#define CV_8UC4 CV_MAKETYPE(CV_8U,4)

has type

有类型

0+((4-1) << 3) == 24

so for a 4 channel uchar image, type() will return 24. From the above you can see that the depth of a type is represented by the last 3 bits of the type integer. If all you want is the depth (you don't care how many channels it has) you can get that directly with dst.depth()

所以对于一个4通道的uchar图像,type()会返回24。从上面可以看出一个类型的深度是由类型整数的最后3位表示的。如果你想要的只是深度(你不关心它有多少通道)你可以直接用 dst.depth()

回答by nkint

Hammeranswer is very clear and it shows you a nice tips: always check the datatypes and be aware of types_c.h, check it out every time you want!

Hammer 的回答非常清楚,它向您展示了一个很好的提示:始终检查数据类型并注意types_c.h,每次需要时都检查一下!

Anyway, minMaxLoc returns a double but it already do the cast for you. If you give a uchar Mat objects as input array it will set the 2 double variables as double in the range 0-255 (the uchar range!).

无论如何, minMaxLoc 返回一个 double 但它已经为你做了演员。如果您将 uchar Mat 对象作为输入数组,它将在 0-255(uchar 范围!)范围内将 2 个双精度变量设置为双精度。

See this example:

看这个例子:

#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int /*argc*/, char** /*argv*/) {

    double m, M;
    Point p_min, p_max;
    Mat img;

    // let's do with uchar Mat
    img = (Mat_<uchar>(3,3) << 0,1,2,3,4,5,6,7,255); 
    cout << img << endl;
    minMaxLoc(img, &m, &M, &p_min, &p_max);
    cout << "min: " << m << " at " << p_min << endl;
    cout << "max: " << M << " at " << p_max << endl;
    cout << (int)img.at<uchar>(p_max.y, p_max.x) << endl; // cast to int otherwise cout will print an ASCII (uchar)

    // now with float Mat
    img = (Mat_<float>(3,3) << 0.1f,1.2f,2,3000.2f,4,5,6,7,255); 
    cout << img << endl;
    minMaxLoc(img, &m, &M, &p_min, &p_max);
    cout << "min: " << m << " at " << p_min << endl;
    cout << "max: " << M << " at " << p_max << endl;
    cout << img.at<float>(p_max.y, p_max.x) << endl;

    return 0;
}

now, in the code you posted you define the destination type in the ddepth. You can specify ddepthas a opencv constant datatypes, like:

现在,在您发布的代码中,您在ddepth. 您可以指定ddepth为 opencv 常量数据类型,例如:

ddepth = CV_8UC1

in this case the cv::Laplacian will return a uchar Mat.

在这种情况下, cv::Laplacian 将返回一个 uchar Mat。

So, be always aware of what kind of data are you handling. If you are working with colour image you'd probabilly better to use Mat::at<Scalar>().

因此,请始终注意您正在处理的数据类型。如果您正在处理彩色图像,则最好使用Mat::at<Scalar>().

回答by Abhishek Thakur

It depends on what value u want. For a gray scale image if you input a Mat with uchar data type you will get a value between 0 to 255.

这取决于你想要什么价值。对于灰度图像,如果您输入具有 uchar 数据类型的 Mat,您将获得 0 到 255 之间的值。