gpt4 book ai didi

python - 从过程中排除某些尺寸的图像(OpenCV、Python)

转载 作者:太空宇宙 更新时间:2023-11-03 22:23:12 27 4
gpt4 key购买 nike

使用下一个代码,我可以拍摄图像并将其分成包含感兴趣区域的各种较小的图像。

import cv2
import sys

sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project')
sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project\\FOLDER')
sys.path.insert(0, 'C:\\Users\\Bob\\Desktop\\Project\\READER')

import FOLDER.folders
import READER.extractor

timestr = FOLDER.folders.timestr

################## AREA 1 ##################

# Load the image
img = cv2.imread('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr) + '\\EXTRACTED\\' + 'area1.png')

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

# smooth the image to avoid noises
gray = cv2.medianBlur(gray,5)

# Apply adaptive threshold
thresh = cv2.adaptiveThreshold(gray,255,1,1,11,2)
thresh_color = cv2.cvtColor(thresh,cv2.COLOR_GRAY2BGR)

# apply some dilation and erosion to join the gaps - change iterations value to detect more or less area's
thresh = cv2.dilate(thresh,None,iterations = 15)
thresh = cv2.erode(thresh,None,iterations = 15)

# Find the contours
image,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# For each contour, find the bounding rectangle and draw it
idx =0

for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi = gray[y:y + h, x:x + w]
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr) + '\\EXTRACTED\\ex_area1' + str(idx) + '.png',roi)

这是一个例子:

图片已加载

origin

输出

output1

output2

代码还给出了一些我不想要的次要图像(比方说人工制品)。所有这些图像都低于某些特定尺寸。

selection

我的问题是:我必须在上面的代码中添加什么才能删除这些图像?要删除下一个尺寸以下的图像:250(宽度)X 60(高度)像素?

谢谢

提示:使用此代码检测区域:Improve text area detection (OpenCV, Python)

最佳答案

首先不要写它们。只写60*250以上的图片。

   for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi = gray[y:y + h, x:x + w]
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2)
if (w*h>250*60): #change this for proper settings
cv2.imwrite('C:\\Users\\Bob\\Desktop\\Destination\\' + str(timestr)
+ '\\EXTRACTED\\ex_area1' + str(idx) + '.png',roi)

关于python - 从过程中排除某些尺寸的图像(OpenCV、Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46192870/

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