简体   繁体   English

Python OpenCV / PIL COLOR_BGR2RGB 错误

[英]Python OpenCV / PIL COLOR_BGR2RGB Error

I've written some Python code that takes frames from a video and puts several of them into rows/columns in jpegs.我编写了一些 Python 代码,它们从视频中获取帧并将其中的几个放入 jpeg 的行/列中。 For the most part, it works, except for the end.大多数情况下,它是有效的,除了结尾。 So I assume this error has something to do with indexing out-of-range in an array.所以我认为这个错误与在数组中索引超出范围有关。

import cv2, Image, os


name = raw_input('Video File (With Extension): ')
x_res = int(raw_input('Image Width (Pixels): '))
y_res = int(raw_input('Image Height (Pixels): '))
rows = int(raw_input('Number of Rows: '))
columns = int(raw_input('Number of Columns: '))


vidcap = cv2.VideoCapture(name)
_,im = vidcap.read()
frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
new_im = Image.new('RGB', (x_res, y_res))
x_cntr = 0
y_cntr = 0


if not os.path.exists('./' + name + ' Output/'):
    os.makedirs('./' + name + ' Output/')


for x in xrange(0,frames,1):
    if x%(rows*columns) == 0:
        new_im.save('./' + name + ' Output/' + str(x) + '.jpg')
        new_im = Image.new('RGB', (x_res,y_res))
        y_cntr = 0
        x_cntr = 0
        print str(round(100*(float(x)/frames), 1)) + "% Complete"
    elif x%rows == 0:
        x_cntr = 0
        y_cntr = y_cntr + y_res/columns
    elif x%1 == 0:
        x_cntr = x_cntr + x_res/rows
    _,cv2_im = vidcap.read()
    cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
    im = Image.fromarray(cv2_im)
    im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
    new_im.paste(im, (x_cntr, y_cntr))

It returns this when it is going to make the last image:当它要制作最后一个图像时,它会返回这个:

Traceback (most recent call last):
  File "C:/Users/Cameron/Downloads/JPG/IMG2.py", line 36, in <module>
    cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
error: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\color.cpp:7349: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor

This error suggests that your image cv2_im is not BGR format so it cannot convert to RGB.此错误表明您的图像 cv2_im 不是 BGR 格式,因此无法转换为 RGB。 It must already be RGB format or might be GRAY format.它必须已经是 RGB 格式或可能是 GRAY 格式。 The error represents format mismatch.该错误表示格式不匹配。

Use this code使用此代码

cv2_im = cv2.cvtColor(cv2_im, cv2.COLOR_GRAY2RGB)

So basically, it was trying to save a non-existent frame to the end.所以基本上,它试图将一个不存在的帧保存到最后。 So if I chose 5 rows and 5 columns, when there were 132 frames, it wouldn't divide equally (132 % 25 ≠ 0) and it would try to index something out of range of the video.因此,如果我选择 5 行和 5 列,当有 132 帧时,它不会平均划分(132 % 25 ≠ 0)并且它会尝试索引超出视频范围的内容。 So I basically added an "if success == True" then process normally, else "if success == False" then save what it has and stop.所以我基本上添加了一个“if success == True”然后正常处理,否则“if success == False”然后保存它所拥有的并停止。 So here is the working code.所以这是工作代码。

import cv2, Image, os


name = raw_input('Video File (With Extension): ')
x_res = int(raw_input('Image Width (Pixels): '))
y_res = int(raw_input('Image Height (Pixels): '))
rows = int(raw_input('Number of Rows: '))
columns = int(raw_input('Number of Columns: '))


vidcap = cv2.VideoCapture(name)
success,im = vidcap.read()
frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print frames
new_im = Image.new('RGB', (x_res, y_res))
x_cntr = 0
y_cntr = 0


if not os.path.exists('./' + name + ' Output/'):
    os.makedirs('./' + name + ' Output/')


for x in xrange(0,frames,1):
    if x%(rows*columns) == 0:
        new_im.save('./' + name + ' Output/' + str(x) + '.jpg')
        new_im = Image.new('RGB', (x_res,y_res))
        y_cntr = 0
        x_cntr = 0
        print str(round(100*(float(x)/frames), 1)) + "% Complete"
    elif x%rows == 0:
        x_cntr = 0
        y_cntr = y_cntr + y_res/columns
    elif x%1 == 0:
        x_cntr = x_cntr + x_res/rows
    success,cv2_im = vidcap.read()
    if success == True:
        cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
        im = Image.fromarray(cv2_im)
        im = im.resize((x_res/rows + x_res%rows, y_res/columns + y_res%columns), Image.ANTIALIAS)
        new_im.paste(im, (x_cntr, y_cntr))
    elif success == False:
        new_im.save('./' + name + ' Output/' + str(x) + '.jpg')

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

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