我是图像处理新手。我从Kaggle那里找到了下面的种植技术。有人能解释一下它是如何裁剪图像的吗
def edge_and_cut(img):
try:
edges = cv2.Canny(img, img_w, img_h)
if(np.count_nonzero(edges)>edges.size/10000):
pts = np.argwhere(edges>0)
y1,x1 = pts.min(axis=0)
y2,x2 = pts.max(axis=0)
new_img = img[y1:y2, x1:x2]
new_img = cv2.resize(new_img,(img_w, img_h))
else:
new_img = cv2.resize(img,(img_w, img_h))
except Exception as e:
print(e)
new_img = cv2.resize(img,(img_w, img_h))
return new_img
def crop_images(Imgs):
CroppedImages = np.ndarray(shape=(len(Imgs), img_w, img_h, 3), dtype=np.int)
ind = 0
for im in Imgs:
x = edge_and_cut(im)
CroppedImages[ind] = x
ind += 1
return CroppedImages
以下是输出:
cv2.Canny是Canny边缘检测器。如果我理解正确,它的输出被视为二值图像(由表示“边”和“非边”的单元格组成),然后它会找到包含所有“边”单元格的最小边界框(矩形)。此框是从图像中提取的
裁剪部分通过以下方式完成:
在这里,您将从y1到y2,以及x1到x2对图像阵列进行切片,因此您只需保留图像的该区域,即由点(x1,y1)、(x1,y2)、(x2,y1)、(x2,y2)包围的矩形。在这种特殊情况下,该区域由cv2的Canny边缘检测器选择为mentioned by Daweo above
相关问题 更多 >
编程相关推荐