opencv2 中用于阈值 32 uint 图像的轮廓和凸包
Contours and convex hull in opencv2 for a thresholded 32 uint image
我有以下代码:
import cv2
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from skimage import data
from skimage import filter
from skimage.filter import threshold_otsu
matplotlib.rcParams['font.size'] = 9
nomeimg = 'frame1_depth.png'
i = cv2.imread(nomeimg, -1)
#conversione da 16 a 32 uint
img = np.array(i, dtype=np.uint32)
img *= 65536
print img.dtype
#thresholding con il metodo Otsu
thresh = threshold_otsu(img)
binary = img > thresh
print thresh
plt.figure(1)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 2.5))
ax1.imshow(img)
ax1.set_title('Original')
ax1.axis('off')
ax2.hist(img)
ax2.set_title('Histogram')
ax2.axvline(x=thresh, color='r', linestyle='dashed', linewidth=2)
ax3.imshow(binary, cmap=plt.cm.gray)
ax3.set_title('Thresholded')
ax3.axis('off')
plt.figure(2)
f, ax = plt.subplots(figsize=(8, 2.5))
ax.imshow(binary, cmap=plt.cm.gray)
ax.set_title('Thresholded')
ax.axis('off')
plt.show()
我有一组来自 XBOX kinect 的深度图像,因此在数据类型转换之后,我可以使用一些仅适用于 8 位或 32 位图像的 opencv
函数,我已经设定了阈值图像与大津算法并显示结果。
我获得了一个子图,其中有我的原始图像、直方图和经过阈值处理的黑白图像。现在我只想处理这张黑白图像,我想保存它并计算轮廓、凸包和其他几何特征。但是,它只是阈值,但是 otsu
。
我该如何计算?
如果您想使用 findContours
和 OpenCV 中的其他图像处理分析函数处理您的二值图像,您只需将 binary
转换为 uint8
。另外,请确保缩放图像,使非零值变为 255。uint32
仅在某些模式下工作,并且为了避免记住哪些功能允许您执行此操作的模式,只需坚持 uint8
因此,这样做:
binary = 255*(binary.astype('uint8'))
完成此转换后,您可以调用 findContours
:
contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
以上只是一种调用方式。我建议您查看我在上面 link 编辑的文档以获取更多详细信息。
作为另一个例子,如果你想找到你的阈值图像的凸包,它有一堆形状 convexHull
,你需要一组代表你的轮廓的点,这正是由 cv2.findContours
的 contours
输出给出。但是,convexHull
函数假定只有一个 单个 对象表示一个轮廓。如果您有多个对象,因此有多个轮廓,则必须遍历每个轮廓并存储结果。
因此,做这样的事情:
hull = [cv2.convexHull(cnt) for cnt in contours]
hull
中的每个元素将return组成每个轮廓的凸包的坐标。因此,要访问轮廓 i
的凸包坐标,您可以这样做:
points = hull[i]
顺便说一句,这里有一些很棒的 link 可以帮助您开始使用 OpenCV 的形状分析功能。这是一个 link,讨论如何在更一般的上下文中使用 cv2.findContours
:
这里还有一个link讲OpenCV的其他形状分析函数,比如凸包,像矩等:
玩得开心!
我有以下代码:
import cv2
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from skimage import data
from skimage import filter
from skimage.filter import threshold_otsu
matplotlib.rcParams['font.size'] = 9
nomeimg = 'frame1_depth.png'
i = cv2.imread(nomeimg, -1)
#conversione da 16 a 32 uint
img = np.array(i, dtype=np.uint32)
img *= 65536
print img.dtype
#thresholding con il metodo Otsu
thresh = threshold_otsu(img)
binary = img > thresh
print thresh
plt.figure(1)
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 2.5))
ax1.imshow(img)
ax1.set_title('Original')
ax1.axis('off')
ax2.hist(img)
ax2.set_title('Histogram')
ax2.axvline(x=thresh, color='r', linestyle='dashed', linewidth=2)
ax3.imshow(binary, cmap=plt.cm.gray)
ax3.set_title('Thresholded')
ax3.axis('off')
plt.figure(2)
f, ax = plt.subplots(figsize=(8, 2.5))
ax.imshow(binary, cmap=plt.cm.gray)
ax.set_title('Thresholded')
ax.axis('off')
plt.show()
我有一组来自 XBOX kinect 的深度图像,因此在数据类型转换之后,我可以使用一些仅适用于 8 位或 32 位图像的 opencv
函数,我已经设定了阈值图像与大津算法并显示结果。
我获得了一个子图,其中有我的原始图像、直方图和经过阈值处理的黑白图像。现在我只想处理这张黑白图像,我想保存它并计算轮廓、凸包和其他几何特征。但是,它只是阈值,但是 otsu
。
我该如何计算?
如果您想使用 findContours
和 OpenCV 中的其他图像处理分析函数处理您的二值图像,您只需将 binary
转换为 uint8
。另外,请确保缩放图像,使非零值变为 255。uint32
仅在某些模式下工作,并且为了避免记住哪些功能允许您执行此操作的模式,只需坚持 uint8
因此,这样做:
binary = 255*(binary.astype('uint8'))
完成此转换后,您可以调用 findContours
:
contours, hierarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_NONE)
以上只是一种调用方式。我建议您查看我在上面 link 编辑的文档以获取更多详细信息。
作为另一个例子,如果你想找到你的阈值图像的凸包,它有一堆形状 convexHull
,你需要一组代表你的轮廓的点,这正是由 cv2.findContours
的 contours
输出给出。但是,convexHull
函数假定只有一个 单个 对象表示一个轮廓。如果您有多个对象,因此有多个轮廓,则必须遍历每个轮廓并存储结果。
因此,做这样的事情:
hull = [cv2.convexHull(cnt) for cnt in contours]
hull
中的每个元素将return组成每个轮廓的凸包的坐标。因此,要访问轮廓 i
的凸包坐标,您可以这样做:
points = hull[i]
顺便说一句,这里有一些很棒的 link 可以帮助您开始使用 OpenCV 的形状分析功能。这是一个 link,讨论如何在更一般的上下文中使用 cv2.findContours
:
这里还有一个link讲OpenCV的其他形状分析函数,比如凸包,像矩等:
玩得开心!