【发布时间】:2018-06-08 11:29:44
【问题描述】:
您好,我是 python 和 opencv 的新手。我有这张图片:
我正在尝试从图片中裁剪灰度图像。目前,代码找到最大的边界框,即右上角的图像,然后对其进行裁剪。我想要做的是找到所有灰度图像,即使图片中有超过 4 个并裁剪所有图像。我正在考虑使用循环来执行此操作,但我不想设置一个循环,它会找到最大的边界框 4 次,然后停止,因为我正在处理的其他图像将包含超过 4 个图像。任何帮助将不胜感激!
import cv2
import numpy as np
# load image
img = cv2.imread('multi.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to grayscale
# threshold to get just the signature (INVERTED)
retval, thresh_gray = cv2.threshold(gray, thresh=100, maxval=255, \
type=cv2.THRESH_BINARY_INV)
image, contours, hierarchy = cv2.findContours(thresh_gray,cv2.RETR_LIST, \
cv2.CHAIN_APPROX_SIMPLE)
# Find object with the biggest bounding box
mx = (0,0,0,0) # biggest bounding box so far
mx_area = 0
for cont in contours:
x,y,w,h = cv2.boundingRect(cont)
area = w*h
if area > mx_area:
mx = x,y,w,h
mx_area = area
x,y,w,h = mx
# Find object with the biggest bounding box
mx = (0,0,0,0) # biggest bounding box so far
mx_area = 0
for cont in contours:
x,y,w,h = cv2.boundingRect(cont)
area = w*h
if area > mx_area:
mx = x,y,w,h
mx_area = area
x,y,w,h = mx
# Output to files
roi=img[y:y+h,x:x+w]
cv2.imwrite('Image_crop.jpg', roi)
cv2.rectangle(img,(x,y),(x+w,y+h),(200,0,0),2)
cv2.imwrite('Image_cont.jpg', img)
【问题讨论】:
-
您可以使用HERE 中的
cv.contourArea(cnt)找到轮廓区域。为必须找到轮廓的区域设置阈值并裁剪它们 -
问题是灰度图像大小不一样。有大有小,但都比底部显示的文字和范围大。
-
我添加了一个以 10000 作为区域阈值的答案。您可以使用不同的值,因为框大于文本和范围
标签: python numpy opencv image-processing crop