【发布时间】:2019-03-20 20:07:59
【问题描述】:
我正在使用 openCV 创建特定模式的视频。我的一个 35x35 大小的图案看起来像这样
基础矩阵中对应的数字条目如下所示
我的想法是将图案覆盖在 260x346 大小的白色背景上,并创建一个图案水平移动的视频。第一帧看起来像这样
我使用具有以下功能的 openCV 创建视频
def move_square(pattern, background):
'''
The function creates a video of the pattern moving horizontally over a given background
Parameters:
-----------
pattern: <np.array, 35x35>
The pattern supposed to move over the background
background: <np.array, 260x346>
A white background of the given size
'''
fourcc = VideoWriter_fourcc(*'MP42')
video = VideoWriter('./videos/moving_pattern_for_sampling_exp.avi', fourcc, 30, (346, 260))
background[112:147, 0:35] = pattern
frame = background
for _ in range(0, 346-30):
video.write(cv2.cvtColor(frame,cv2.COLOR_GRAY2RGB))
shifted_frame = np.roll(frame, 1, axis=1)
frame = shifted_frame
video.write(frame)
cv2.destroyAllWindows()
video.release()
但是,如果我使用以下 sn-p 读取上述视频的帧
vidcap = cv2.VideoCapture('videos/moving_pattern_for_sampling_exp.avi')
success,image = vidcap.read()
count = 0
while success:
cv2.imwrite("test/frame%d.jpg" % count, image) # save frame as JPEG file
success,image = vidcap.read()
print('Read a new frame: ', success)
count += 1
im = cv2.imread('test/frame1.jpg')
img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
np.savetxt("image.csv", np.asarray(img),fmt='%i', delimiter=",")
现在查看框架,框架中的图案值与原来的不同。
此外,本应为白色且值为 255 的位置现在已自始至终设置为 252。这些差异的原因可能是什么?
【问题讨论】:
-
JPEG 有损。允许更改您的数据以使您的文件更小。如果您想要无损压缩,请使用 PNG。
-
大多数视频编解码器都会帮你,即“调整”你的数据,顺便说一下,就像 JPEG 一样。
-
是的,我意识到视频编解码器不正确。我用其中一种无损编解码器替换了我的旧编解码器,我的问题得到了解决。感谢您的投入。
标签: python opencv image-processing video-processing