使用 .save in python 以迭代编号保存多张图像

Saving multiples images with iterative numbering using .save in python

我正在尝试检测热视频中的闭眼运动以研究睡眠状态。我能够检测到脸部闭合的眼睛区域,然后我能够保存每个眼框,稍后它被转换为只给我睫毛区域,但我无法像我为眼睛区域所做的那样用迭代编号保存睫毛图像。

import numpy as np
import cv2
import scipy
import glob
import os
from pylab import *
from matplotlib import pyplot as plt
from PIL import Image
from collections import Counter
import time

blur_radius = 1.0
threshold = 50


video = cv2.VideoCapture('12.avi')
while True:
        ret, frame = video.read()
        frame = frame[:,1:600]
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (15,15), 0)
        (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
        image = frame
        (x,y) = maxLoc
        cv2.circle(image, maxLoc, 15, (255, 0, 0), 2)
        cv2.rectangle(image,(maxLoc),(390,190),(0,255,0),2)
        roi = frame [y:190,x:390]
        roi = cv2.resize(roi, None, fx=4, fy=4, interpolation=cv2.INTER_AREA)
        count = 0
        while True:
                cv2.imwrite("eye%d.tif" % count,roi)
                count += 1
        for eye in glob.glob("*.tif"):
                im=Image.open(eye)
                gray = im.convert('L')
                bw = gray.point(lambda x: 0 if x<128 else 255, '1')
                count1 = 0
                while True:
                        cv2.imwrite("lashes%d.tif" % count,bw)
                        count += 1
                        count2 = 0
                        for eye in glob.glob("*.tif"):
                                os.remove("eye%d.tif" %count2)
                                count2 += 1                         
        try:
                roi = cv2.resize(roi, None, fx=4, fy=4, interpolation=cv2.INTER_AREA)
                cv2.imshow("Eye",roi)
                cv2.imshow("Eyecorner", image)
        except:
                print''

        if cv2.waitKey(1) & 0xFF == ord('q'):
                break

video.release()
cv2.destroyAllWindows()

这是我在 运行 此代码

时收到的错误
Traceback (most recent call last):
  File "D:\Acads.1 Sem\BTP\Data\Thermal videos\after_getting_eye.py", line 39, in <module>
    cv2.imwrite("lashes%d.tif" % count,bw)
TypeError: img is not a numpy array, neither a scalar

一旦我得到这些图像,我正在考虑使用这个

diff = ImageChops.difference(im2, im1).getbbox()

找出以前的睫毛图像和现在的睫毛图像之间的差异。如果 diff 有除 none 之外的某个值,则表明存在睫毛运动,在 return 中证明闭合的眼睑后面有眼球运动。还有谁能告诉我一个不保存这些图像并直接处理它们的快速方法,因为 运行 时间越来越长,这是一种在盖子关闭时检测眼球运动的可行方法吗?非常感谢任何帮助

编辑:

函数 cv2.imwrite 接收一个变量 bw,在您的代码中它是一个 PIL 图像。此格式与 OpenCV imwrite.

不兼容

this

声明您想要 store/save 按顺序处理所有图像的文件夹。

要保存没有 over-writing 的迭代图像 ,您必须在 for 循环中执行所有操作。

算法如下:

import glob         

需要图书馆。有关更多信息,请参阅 HERE

jpg_files = glob.glob("C:/Users/Desktop/Input_images/*.jpg")
#----Input images are present in this location

a = 'C:/Users/Desktop/Output_folder'
#----Processed output images are saved in this location

#----initializing the for loop
for i in range(len(jpg_files)):
    x=jpg_files[i]
    #---- 'x' contains the input image for every iteration

    #-----
    #-- Here is where you write the code to process your images--
    #-----

    xy = str(jpg_files[(i)])
    #-----'xy' is the new extension you add for every iterated image

    cv2.imwrite('a' + xy, final_image )
    #----- Your procesed image 'final_image ' is stored in location 'a' along with the extension 'xy'.

希望对您有所帮助 :)

我找到了一种使用 PIL 迭代保存图像的方法。这对我来说很好

for eye in glob.glob("*.tif"):
                file, ext = os.path.splitext(eye)
                im=Image.open(eye)
                gray = im.convert('L')
                bw = gray.point(lambda x: 0 if x<128 else 255, '1')
                bw.save(file + ".tif","JPEG")