网站建设与规划实验心得百度seo关键词优化电话
根据我们的注释,您可以创建一个numpy数组列表,其中每个元素都是描述每个对象轮廓内部的强度。具体来说,对于每个轮廓,创建一个填充轮廓内部的二进制掩码,找到填充对象的(x,y)坐标,然后索引到图像中并获取强度。
我不知道您是如何设置代码的,但假设您有一个灰度图像,名为img。您可能需要将图像转换为灰度,因为cv2.findContours可用于灰度图像。这样,通常调用cv2.findContours:import cv2
import numpy as np
#... Put your other code here....
#....
# Call if necessary
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Call cv2.findContours
contours,_ = cv2.findContours(img, cv2.RETR_LIST, cv2.cv.CV_CHAIN_APPROX_NONE)
contours现在是3Dnumpy数组的列表,其中每个数组的大小都是N x 1 x 2,其中N是每个对象的轮廓点总数。
因此,您可以这样创建我们的列表:# Initialize empty list
lst_intensities = []
# For each list of contour points...
for i in range(len(contours)):
# Create a mask image that contains the contour filled in
cimg = np.zeros_like(img)
cv2.drawContours(cimg, contours, i, color=255, thickness=-1)
# Access the image pixels and create a 1D numpy array then add to list
pts = np.where(cimg == 255)
lst_intensities.append(img[pts[0], pts[1]])
对于每个轮廓,我们创建一个空白图像,然后在此空白图像中绘制填充的轮廓。您可以通过将thickness参数指定为-1来填充轮廓占用的区域。我将轮廓的内部设置为255。之后,我们使用^{}查找数组中与特定条件匹配的所有行和列位置。在我们的例子中,我们希望找到等于255的值。之后,我们使用这些点索引到图像中,以获取轮廓内部的像素强度。
lst_intensities包含1Dnumpy数组的列表,其中每个元素提供属于每个对象轮廓内部的强度。要访问每个数组,只需执行lst_intensities[i],其中i是要访问的轮廓。