您的位置:首页 > 编程语言 > Python开发

<<python computer vision with opencv>> chapter 2

2014-05-27 11:17 495 查看
import cv2
import numpy
import os

# Make an array of 120,000 random bytes.
randomByteArray = bytearray(os.urandom(120000))
flatNumpyArray = numpy.array(randomByteArray)

# Convert the array to make a 400x300 grayscale image.
grayImage = flatNumpyArray.reshape(300, 400)
# Assignment
grayImage[0, 0] = 0

# grayImage.setitem([0, 1], 128)
# AttributeError: 'numpy.ndarray' object has no attribute 'setitem'

# Write image
cv2.imwrite('RandomGray.png', grayImage)

# Convert the array to make a 400x100 color image.
bgrImage = flatNumpyArray.reshape(100, 400, 3)

# 24-bit BGR image with a Red pixel in the upper-left corner
bgrImage[0, 0] = [0, 0, 255] #  [0, 0, 255] from left to right --> 0: Blue; 0: Green; 0: Red

cv2.imwrite('RandomColor.png', bgrImage)






CameraCaptureFrames write to avi video

import cv2

cameraCapture = cv2.VideoCapture(0)
fps = 1 # an assumption
size = (int(cameraCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cameraCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('MyOutputVid.avi',
cv2.cv.CV_FOURCC('I','4','2','0'),
fps, size)
success, frame = cameraCapture.read()
numFramesRemaining = 10 * fps - 1
while success and numFramesRemaining > 0:
videoWriter.write(frame)
success, frame = cameraCapture.read()
numFramesRemaining -= 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐