繁体   English   中英

有人可以解释图像裁剪的工作原理吗?

[英]Can someone explain how image cropping works?

我是图像处理的新手。 我从 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

这是 output:

输出图像

cv2.CannyCanny 边缘检测器 如果我理解正确,它的 output 被视为二进制图像(由表示“边缘”和“非边缘”的单元格组成),然后它确实找到了包含所有“边缘”单元格的最小边界框(矩形)。 此框是从图像中提取的。

裁剪部分通过以下方式完成:

    new_img = img[y1:y2, x1:x2]   

在这里,您将图像数组从 y1 切片到 y2,将 x1 切片到 x2,因此您只保留图像的那个区域,即由点 (x1,y1)、(x1,y2)、(x2, y1), (x2,y2)。 在这种特殊情况下,该区域由 cv2 的 Canny Edge Detector 选择,如上面 Daweo 所述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM