重用对齐的图像进行边缘检测和阈值化

时间:2019-10-23 00:37:28

标签: python numpy opencv image-processing python-imaging-library

嗨,我正在尝试在python中实现焦点堆叠。我看过Photoshop以及它如何进行图像堆叠,效果非常好。但是我想使用Python来应用类似的技术。

  1. 我目前使用ECC算法将图像对齐在一起
  2. 对它们进行边缘检测,使图像膨胀和模糊
  3. 应用了阈值以生成关注区域(不确定此步骤)。 我在这里继续进行下去。在Photoshop中,当我们加载一叠图像时,我们使用自动对齐来对齐图像,然后将它们堆叠在一起时,会创建图像蒙版,并使用该蒙版创建清晰的最终图像。 如果有人能够回顾我到目前为止所做的正确方向的工作,并且不能指导我这样做,那将真的很有帮助。
def stackImagesECC(file_list):
    M = np.eye(3, 3, dtype=np.float32)

    first_image = None
    stacked_image = None

    for file in file_list:
        image = cv2.imread(file,1).astype(np.float32) / 255
        print(file)
        if first_image is None:
            # convert to gray scale floating point image
            first_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
            stacked_image = image
        else:
            # Estimate perspective transform
            s, M = cv2.findTransformECC(cv2.cvtColor(image,cv2.COLOR_BGR2GRAY), first_image, M, cv2.MOTION_HOMOGRAPHY)
            w, h, _ = image.shape
            # Align image to first image
            image = cv2.warpPerspective(image, M, (h, w))
            stacked_image += image

    stacked_image /= len(file_list)
    stacked_image = (stacked_image*255).astype(np.uint8)
    return stacked_image

此功能可以对齐图像

images = glob.glob("edges_canny/*.jpg")
kernel = np.ones((3, 3), np.uint8)
for image in images:
    with open(image, 'rb') as file:
        img = Image.open(file)
#         img.show()
        indexed = np.array(img)
        img_dilate = cv2.dilate(indexed, kernel, iterations=1)
        very_blurred = ndimage.gaussian_filter(img_dilate, sigma=5)
        ret, thresh5 = cv2.threshold(very_blurred, 100, 255, cv2.THRESH_TOZERO_INV) 
        cv2.imshow("a",thresh5)
        cv2.waitKey(0)

在这里,我再次读取边缘检测到的图像,并对其进行膨胀,高斯模糊和阈值处理以获得感兴趣的区域(我不确定)。我相信有一些方法可以直接从对齐功能读取对齐的图像。

0 个答案:

没有答案