【问题标题】:How to recognize multiple photos on a white background using openCV-python?如何使用openCV-python识别白色背景上的多张照片?
【发布时间】:2019-10-30 02:03:04
【问题描述】:

基本上,我使用我们的打印机扫描了许多旧照片。大约四张照片适合扫描的每一页(围绕页面的四个角),并且空白区域并不总是完全白色。我想使用 openCV 将它们自动裁剪成单独的照片。需要解决此问题的方法的建议,谢谢!

想过检测页面上四个矩形的形状来获取坐标还是用opencv的grabCut……不知道

我尝试使用 PIL,但无法正确裁剪照片,因为有些照片也与背景颜色相同,这会导致裁剪过早结束。

这是它外观的粗略草图

(除非是真实的人物照片)

【问题讨论】:

标签: python image opencv image-processing image-recognition


【解决方案1】:

这是一种基于照片不会相互交叉的假设的方法

  • 转换为灰度和高斯模糊
  • 阈值图像
  • 寻找轮廓并获得边界框轮廓
  • 提取投资回报率

阈值图像

接下来我们使用cv2.findContours() 获取轮廓,并使用cv2.boundingRect() 获取边界框。然后我们可以使用

提取 ROI
x,y,w,h = cv2.boundingRect(c)
ROI = original[y:y+h, x:x+w]

这是结果

照片#1

照片#2

照片#3

照片#4

import cv2

image = cv2.imread('1.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
thresh = cv2.threshold(blurred, 230,255,cv2.THRESH_BINARY_INV)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

# Iterate thorugh contours and filter for ROI
image_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
    ROI = original[y:y+h, x:x+w]
    cv2.imwrite("ROI_{}.png".format(image_number), ROI)
    image_number += 1

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey(0)

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 2017-11-22
    • 2016-03-10
    • 2021-09-10
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2011-05-08
    相关资源
    最近更新 更多