python用identify函数用于显示动物的种类,如何在python中识别numpy类型?

How can one reliably determine if an object has a numpy type?

I realize that this question goes against the philosophy of duck typing, but idea is to make sure a function (which uses scipy and numpy) never returns a numpy type unless it is called with a numpy type. This comes up in the solution to another question, but I think the general problem of determining if an object has a numpy type is far enough away from that original question that they should be separated.

解决方案

Use the builtin type function to get the type, then you can use the __module__ property to find out where it was defined:

>>> import numpy as np

a = np.array([1, 2, 3])

>>> type(a)

>>> type(a).__module__

'numpy'

>>> type(a).__module__ == np.__name__

True

以下是基于Harris角点检测算法的Python代码,包括一个名为“findcorners”的函数,以及代码的示例图像: ```python import numpy as np import cv2 def findcorners(img, blockSize=2, ksize=3, k=0.04, threshold=0.01): # Convert image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Calculate Harris corner response dst = cv2.cornerHarris(gray, blockSize, ksize, k) # Normalize response values to range 0-255 dst_norm = np.empty_like(dst) cv2.normalize(dst, dst_norm, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) # Threshold response values to identify corners corners = np.argwhere(dst_norm > threshold * dst_norm.max()) corners = corners[:, ::-1] # Draw corners on image img_corners = img.copy() for c in corners: cv2.drawMarker(img_corners, tuple(c), color=(0, 0, 255), markerType=cv2.MARKER_CROSS, thickness=2) return corners, img_corners # Example usage img = cv2.imread('example.jpg') corners, img_corners = findcorners(img) cv2.imshow('Original Image', img) cv2.imshow('Corners', img_corners) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在上面的代码,我们首先将输入图像转换为灰度图像,然后使用`cv2.cornerHarris`函数计算Harris响应值。接下来,我们对响应值进行归一化,并将其阈值化以找到角点。最后,我们在原始图像上绘制角点,并返回角点列表以及绘制了角点的新图像。 我们可以使用以下代码来测试`findcorners`函数: ```python img = cv2.imread('example.jpg') corners, img_corners = findcorners(img) cv2.imshow('Original Image', img) cv2.imshow('Corners', img_corners) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这将加载名为“example.jpg”的图像并在屏幕上显示原始图像以及在其找到的角点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值