我想找到是否有一个效用函数或变量输出特定Mat类型可以采用的最大值。 例如,CV_U8的最大可能值为255。
示例案例
Matlab有一些内置函数,可以拍摄任意图像类型的图像,并将其转换(如果需要,可以进行缩放)到另一种图像类型。
例如,Matlab具有im2double函数。 运行help im2double显示:
Class Support ------------- Intensity and truecolor images can be uint8, uint16, double, logical, single, or int16. Indexed images can be uint8, uint16, double or logical. Binary input images must be logical. The output image is double.因此它将在10种不同的图像类型上运行,并输出具有相同数量的颜色通道的双图像,通过除以原始数据类型中的最大允许值来缩放。
因此,如果能够获得输入数据类型的最大值并将其输入到这些函数中,则OpenCV函数convertTo()和normalize()将能够执行相同的操作。
特别是convertTo(dst, type, scale)如果可以使用scale = 1/<max_value_of_input_type>并且normalize(src, dst, alpha, beta, NORM_MINMAX)可以使用alpha = <src_min>/<max_value_of_input_type>并且beta = <src_max>/<max_value_of_input_type> 。
I want to find if there is a utility function or variable that outputs the maximum value a specific Mat type can take. For example, the maximum possible value of a CV_U8 is 255.
Example case
Matlab has a couple of built in functions which can take an image of arbitrary image type and convert it (with scaling if necessary) to another image type.
For example, Matlab has the function im2double. Running help im2double shows:
Class Support ------------- Intensity and truecolor images can be uint8, uint16, double, logical, single, or int16. Indexed images can be uint8, uint16, double or logical. Binary input images must be logical. The output image is double.So it will run on 10 different image types, and outputs a double image with the same number of color channels, scaled by dividing the max allowable value in the original data type.
Thus the OpenCV functions convertTo() and normalize() would be able to do the same thing if one was able to get the max value of the input data type and input it into those functions.
In particular convertTo(dst, type, scale) would work identically if one could use scale = 1/<max_value_of_input_type> and normalize(src, dst, alpha, beta, NORM_MINMAX) would work with alpha = <src_min>/<max_value_of_input_type> and beta = <src_max>/<max_value_of_input_type>.
最满意答案
我找到了解决方案。 希望这对其他人也有用。
效用函数saturate_cast()可以对所需类型的最小值和最大值执行限幅。 为了除以任意类型的最大可能值,使用图像类型可以在OpenCV中获取的最大数字,并使其与目标类型相同,与图像相同。 这适用于未签名的图像。 对于带有有符号值的图像,在正负侧饱和,然后移位和缩放。
请参阅此处的saturate_cast的OpenCV文档: http : //docs.opencv.org/3.1.0/db/de0/group__core__utils.html#gab93126370b85fda2c8bfaf8c811faeaf
编辑:显而易见的解决方案是只为不同的可用Mat类型编写七个if语句:CV_8U,CV_8S,CV_16U,CV_16S,CV_32S,CV_32F,CV_64F,我认为这对于读者来说不会太烦人且更加清晰。
我将尽快使用两种解决方案的代码更新此答案。
I have figured out a solution. Hopefully this can be of use to someone else, too.
The utility function saturate_cast() can perform clipping to the min and max value of a wanted type. In order to divide by the max possible value of an arbitrary type, use the biggest number an image type can take in OpenCV and saturate it with the destination type the same as the image. This will work for unsigned images. For images with signed values, saturate on the positive and negative side, and then shift and scale.
See the OpenCV docs for saturate_cast here: http://docs.opencv.org/3.1.0/db/de0/group__core__utils.html#gab93126370b85fda2c8bfaf8c811faeaf
Edit: The obvious solution is to just write the seven if statements for the different available Mat types: CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F, which I suppose would not be too annoying and much more clear to a reader.
I will update this answer soon with code for both solutions.
更多推荐
发布评论