gpt4 book ai didi

python - 从图像中准确裁剪文档纸

转载 作者:行者123 更新时间:2023-12-02 16:07:11 24 4
gpt4 key购买 nike

我目前正在使用以下代码裁剪图像

def crop_image(image):
image = cv2.imread(image)
original_img = image.copy()
hsv_img = convert_hsv(image)

lower_blue = np.array([0, 0, 120])
upper_blue = np.array([180, 38, 255])

masked_image = mask_img(hsv_img, lower_blue, upper_blue)
result = cv2.bitwise_and(image, image, mask=masked_image)
contours = cv2.findContours(masked_image.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
cv2.drawContours(masked_image, contours, -1, (0, 255, 0), 3)
max_area_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(max_area_contour)
cv2.rectangle(result, (x, y), (x+w, y+h), (0, 255, 0), 3)
cont_filename = generate_contours_filename()
cv2.imwrite(cont_filename, np.hstack([image, result]))
logger.info('Successfuly saved file : %s' % cont_filename)
img = image[y:y+h, x:x+w]
filename = generate_filename()
cv2.imwrite(filename, img)
logger.info('Successfully saved cropped file : %s' % filename)
return img, filename
以下是之前和之后的图像:
This is original image
This is resulting image
我需要只裁 Papercut 张部分的图像
提前致谢

最佳答案

这是在 Python/Opencv 中执行此操作的一种方法。

  • 阅读输入
  • 转灰度
  • 阈值
  • 应用形态学清洁小区域
  • 获取轮廓并过滤以保留最大的
  • 获取边界框
  • 绘制填充在黑色背景上的最大轮廓作为蒙版
  • 使用 mask 使纸张背景变黑
  • 使用边界框裁剪蒙版输入
  • 保存结果

  • 输入:

    enter image description here
    import cv2
    import numpy as np

    # read image as grayscale
    img = cv2.imread('paper.jpg')

    # convert to grayscale
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    # threshold
    thresh = cv2.threshold(gray, 190, 255, cv2.THRESH_BINARY)[1]\

    # apply morphology
    kernel = np.ones((7,7), np.uint8)
    morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    kernel = np.ones((9,9), np.uint8)
    morph = cv2.morphologyEx(morph, cv2.MORPH_ERODE, kernel)

    # get largest contour
    contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    contours = contours[0] if len(contours) == 2 else contours[1]
    area_thresh = 0
    for c in contours:
    area = cv2.contourArea(c)
    if area > area_thresh:
    area_thresh = area
    big_contour = c


    # get bounding box
    x,y,w,h = cv2.boundingRect(big_contour)

    # draw filled contour on black background
    mask = np.zeros_like(gray)
    mask = cv2.merge([mask,mask,mask])
    cv2.drawContours(mask, [big_contour], -1, (255,255,255), cv2.FILLED)

    # apply mask to input
    result1 = img.copy()
    result1 = cv2.bitwise_and(result1, mask)

    # crop result
    result2 = result1[y:y+h, x:x+w]

    # view result
    cv2.imshow("threshold", thresh)
    cv2.imshow("morph", morph)
    cv2.imshow("mask", mask)
    cv2.imshow("result1", result1)
    cv2.imshow("result2", result2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # save result
    cv2.imwrite("paper_thresh.jpg", thresh)
    cv2.imwrite("paper_morph.jpg", morph)
    cv2.imwrite("paper_mask.jpg", mask)
    cv2.imwrite("paper_result1.jpg", result1)
    cv2.imwrite("paper_result2.jpg", result2)

    阈值图像:

    enter image description here

    形态清洁图像:

    enter image description here

    从最大轮廓蒙版图像:

    enter image description here

    屏蔽输入的结果:

    enter image description here

    裁剪上一张图像的结果:

    enter image description here

    关于python - 从图像中准确裁剪文档纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60665392/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com