在 python openCV 中查找图像类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37139014/
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-19 18:55:57  来源:igfitidea点击:

Find image type in python openCV

pythonopencv

提问by Alexandre Rozier

I recently had some issues finding the type of a template image i'm using for pattern matching purposes. I use the python version of OpenCV, and the image returned by imreaddoes not seem to have a "type" property like in the C++ OpenCV implementation.

我最近在查找用于模式匹配目的的模板图像的类型时遇到了一些问题。我使用 OpenCV 的 python 版本,并且返回的图像imread似乎没有像 C++ OpenCV 实现中那样的“类型”属性。

I would like to access the type of the template to create a "dst" Mathaving the same size and type of the template.

我想访问模板的类型以创建一个与模板Mat大小和类型相同的“dst” 。

Here is the code :

这是代码:

template = cv2.imread('path-to-file', 1)
height, width = template.shape[:-1]
dst = cv2.cv.CreateMat(width,height, template.type) 
--->'Error :numpy.ndarray' object has no attribute 'type'

Do you have any thoughts about this issue ?

你对这个问题有什么想法吗?

Thanks a lot for your answers.

非常感谢你的回答。

回答by bakkal

While it's true that the numpy array type can be accessed using template.dtype, that's not the type you want to pass to cv2.cv.CreateMat(), e.g.

虽然确实可以使用 访问 numpy 数组类型template.dtype,但这不是您想要传递给的类型cv2.cv.CreateMat(),例如

In [41]: cv2.imread('abalone.jpg', cv2.IMREAD_COLOR).dtype         
Out[41]: dtype('uint8')

In [42]: cv2.imread('abalone.jpg', cv2.IMREAD_GRAYSCALE).dtype
Out[42]: dtype('uint8')

If you pass the numpy array's dtypeto cv2.cv.CreateMat()you will get this error, e.g.

如果您将 numpy 数组传递dtypecv2.cv.CreateMat()您将收到此错误,例如

cv2.cv.CreateMat(500, 500, template.dtype)

TypeError: an integer is required

类型错误:需要一个整数

And as you can see, the dtype doesn't change for grayscale/color, however

正如您所看到的,dtype 不会因灰度/颜色而改变,但是

In [43]: cv2.imread('abalone.jpg', cv2.IMREAD_GRAYSCALE).shape
Out[43]: (250, 250)

In [44]: cv2.imread('abalone.jpg', cv2.IMREAD_COLOR).shape
Out[44]: (250, 250, 3)

Here you can see that it's the img.shapethat is more useful to you.

在这里您可以看到它对img.shape您更有用。

Create a numpy matrix from the template

从模板创建一个 numpy 矩阵

So you want to create a object from your template you can do :

所以你想从你的模板中创建一个对象,你可以这样做:

import numpy as np
dst = np.zeros(template.shape, dtype=template.dtype)

That should be useable as far as Python API goes.

就 Python API 而言,这应该是可用的。

cv2.cv.CreateMat

cv2.cv.CreateMat

If you want this using the C++ way of creating matrices, you should remember the type with which you opened your template:

如果您希望使用 C++ 方式创建矩阵,您应该记住打开模板时使用的类型:

template = cv2.imread('path-to-file', 1) # 1 means cv2.IMREAD_COLOR 
height, width = template.shape[:-1]    
dst = cv2.cv.CreateMat(height, width, cv2.IMREAD_COLOR)

If you insist on guessing the type:While it's not perfect, you can guess by reading the length of the dimensions of the matrix, an image of type IMREAD_COLORhas 3 dimensions, while IMREAD_GRAYSCALEhas 2

如果你坚持猜测类型:虽然它并不完美,但你可以通过读取矩阵维度的长度来猜测,类型的图像IMREAD_COLOR有 3 个维度,而IMREAD_GRAYSCALE有 2个维度

In [58]: len(cv2.imread('abalone.jpg', cv2.IMREAD_COLOR).shape)
Out[58]: 3

In [59]: len(cv2.imread('abalone.jpg', cv2.IMREAD_GRAYSCALE).shape)
Out[59]: 2