gpt4 book ai didi

python - 如何遍历文件并保存使用 OpenCV 应用的转换?

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

我正在使用 python 进行我的第一个真正的个人项目,我在其中使用边缘检测来创建“扫描”图像,并最终将扫描图像转换为 pdf。所以我能够让我的扫描图像代码在单个文件上工作,但我尝试在目录中的文件上使用 for 循环,但它似乎没有保存每个扫描图像。它只保存第一个。但是,由于我的控制台日志,很明显 for 循环正在完成。我似乎无法弄清楚文件在哪里没有改变。任何指导将不胜感激。谢谢。

# import packages
from pyimagesearch.transform import four_point_transform
from skimage.filters import threshold_local
import numpy as np
import argparse
import cv2
import imutils
import glob




# Iterate through file and make them into scanned versions
for filepath in glob.iglob('images/*.jpg'):

image = cv2.imread(filepath)
ratio = image.shape[0] / 500.0
orig = image.copy()
image = imutils.resize(image, height = 500)


# convert the image to grayscale, blur it, and find edges
# in the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 75, 200)

# show the original image and the edge detected image
print("Applying Edge Detection")
cv2.imshow("Image", image)
cv2.imshow("Edged", edged)
cv2.destroyAllWindows()

# find the contours in the edged image, keeping only the
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]

# loop over the contours
for c in cnts:
# approximate the contour
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)

# if our approximated contour has four points, then we
# can assume that we have found our screen
if len(approx) == 4:
screenCnt = approx
break

# show the contour (outline) of the piece of paper
print("Finding contours of pages")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
cv2.imshow("Outline", image)
cv2.destroyAllWindows()

# apply the four point transform to obtain a top-down
# view of the original image
warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)

# convert the warped image to grayscale, then threshold it
# to give it that 'black and white' paper effect
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
T = threshold_local(warped, 11, offset = 10, method = "gaussian")
warped = (warped > T).astype("uint8") * 255

# show the original and scanned images
print("Applying perspective transform")
cv2.imshow("Original", imutils.resize(orig, height = 650))
cv2.imshow("Scanned", imutils.resize(warped, height = 650))

# Save scanned image

page_num = 1

print("Saving scanned image")
cv2.imwrite('scanned' + str(page_num) + '.jpg', warped)
page_num += 1

最佳答案

你应该移动

page_num = 1

之前

for filepath in glob.iglob('images/*.jpg'):

否则在每个 cv2.imwrite 上它将是 1:

# Iterate through file and make them into scanned versions
for filepath in glob.iglob('images/*.jpg'):

# <<< SNIPP lots of code >>>

page_num = 1 # this will reset it to 1 _every single time_

print("Saving scanned image")
cv2.imwrite('scanned' + str(page_num) + '.jpg', warped)
page_num += 1

关于python - 如何遍历文件并保存使用 OpenCV 应用的转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55765381/

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