简体   繁体   English

如何使用openCV-python识别白色背景上的多张照片?

[英]How to recognize multiple photos on a white background using openCV-python?

Basically, I have scanned in many old photos using our printer. 基本上,我使用我们的打印机扫描了许多旧照片。 About four photos fit on each page of the scan (around the four corners of the page), and the white space isn't always completely white. 扫描的每一页上都有大约四张照片(围绕页面的四个角),而白色空间并不总是完全白色。 I want to use openCV to automatically crop them into individual photos. 我想使用openCV自动裁剪成单张照片。 Need suggestions for a way to tackle this problem, thanks! 需要一些方法来解决这个问题,谢谢!

I thought about detecting the shape of the four rectangles on the page to get the coordinate or using opencv grabCut... I don't know 我想在页面上检测四个矩形的形状以获得坐标或使用opencv grabCut ...我不知道

I've tried to use PIL, but couldn't crop the photos correctly since some photos also had the same color as the background which would cause the crop to end prematurely. 我试图使用PIL,但无法正确裁剪照片,因为有些照片的颜色与背景颜色相同会导致裁剪过早结束。

This is a rough sketch of what it looks like 这是它的样子的粗略草图

这是它的样子的粗略草图 (except they would be actual photos of people) (除了他们将是人的实际照片)

Here's an approach based on the assumption that the photos will not be intersecting each other 这是一种基于假设照片不会相互交叉的方法

  • Convert to grayscale and Gaussian blur 转换为灰度和高斯模糊
  • Threshold image 阈值图像
  • Find contours and obtain bounding box contours 查找轮廓并获取边界框轮廓
  • Extract ROI 提取投资回报率

Threshold image 阈值图像

在此输入图像描述

Next we obtain the contours using cv2.findContours() and grab the bounding boxes using cv2.boundingRect() . 接下来,我们使用获得的轮廓cv2.findContours()并获取使用边界框cv2.boundingRect() We can then extract the ROIs with 然后我们可以提取ROI

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

Here's the results 这是结果

Photo #1 照片#1

在此输入图像描述

Photo #2 照片#2

在此输入图像描述

Photo #3 照片#3

在此输入图像描述

Photo #4 照片#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)

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

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