我是图像处理的新手。我在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_imgdef 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边缘检测器。如果我理解正确的话,它的输出被视为二值图像(由表示“边缘”和“非边缘”的单元格组成),然后它会找到包含所有“边缘”单元格的最小边界框(矩形)。这个框从图像中被提取出来。