Opencv第三弹

图像与原始字节的转换

一个OpenCV图像是.array类型的二维或三维数组。

8位的灰度图像是一个含有字节值的二维数组;一个24位的BGR图像是一个含有字节值的三维数组。image[0, 0],或image[0,0,0]第一个值代表像素的y坐标或行,0表示顶部;第二个值是像素的x坐标或列,0表示最左边;第三个值表示颜色通道。

-对单像素操作:

image[0,0]----------------image.item((0,0))

image[0,0]=128---------image.setitem((0,0), 128)

例:由104->255

cat = cv2.imread('Cat.jpg')
print(cat.item(150, 120, 0))
cat.itemset((150, 120, 0), 255)
print(cat.item(150, 120, 0))

"""
104
255
"""

-若一幅图像的每个通道为8位,则可以显式转换为标准的一维bytearray格式:

后端Flask向前端传输图片的时候,也尝试转换成字节形式,但最后采用的Base64格式。

import cv2

cat = cv2.imread('Cat.jpg')
print(cat.shape)
img = bytearray(cat)
print(img)


"""
(1080, 1080, 3)



\x07\x04\x06\x05\x04\x06\x04\x04\x04\x0b\x06\x07\x0b\x07\x06\x0b\x07\x06\x0b\x07\x06\n\x06\x05\n\x06\x05\x08\x06\x05\x08\x06\x05\x07\x05\x04\x06\x04\x03\x04\x05\x03\x04\x05\x03\x04\x05\x03\x04\x05\x03\x03\x04\x02\x03\x04\x02\x07\x05\x05\x07\x05\x05\x07\x05\x05\x07\x05\x05\x07\x05\x05\x08\x06\x06\t\x07\x07\t\x07\x07\t\x07\x07\t\x07\x07\n\x08\x08\x0b\t\t\x0c\n\n\x0e\x0c\x0c\x10\x0e\x0e\x11\x0f\x0f\x14\x0f\x10\x12\r\x0e\x12\r\x0e\x15\x10\x11\x19\x14\x15\x1d\x18\x19\x1f\x1d\x1d"  %##\x16\x14\x14\x0f\x0f\x0f\x18\x18\x18\x19\x19\x19\x0e\x0e\x0e\x0c\x0c\x0c\x15\x15\x15\x12\x11\x13\x10\x0f\x11\x0e\x10\x11\x0e\x10\x11\t\x0c\x10\x0b\x10\x13\x1d!&.7;NV]Ycjs~\x86\x8f\x9a\xa2\x96\xa2\xac\x92\x9e\xa8\x93\xa2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8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc8\xd3\xf3\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc9\xd4\xf4\xc7\xd4\xf4\xc6\xd4\xf1\xc5\xd5\xf2\xc5\xd5\xf2\xc4\xd4\xf1\xbe\xcc\xe8\xad\xbb\xd7\x9a\xa7\xc1\x8c\x99\xb3t\x7f\x9a\\g\x82BLd/9Q (?\x13\x1b2\x10\x18/\x15\x1d47@TLVh;EW\x11\x1c*\n\x13 \x17"*\x13\x1b"\x00\x07\n\x01\x08\x0b\x01\t\x08\x04\n\t\x04\x0b\x08\x06\x0b\t\x06\x0b\t\x08\x0b\t\t\x0c\n\x0b\x0f\n\x0b\x0f\n\n\x0e\t\n\x0e\t\x0f\x13\x0e\x14\x18\x13\x14\x15\x11\x0f\x10\x0c\x10\x11\r\x0e\x0f\x0b\x0e\r\t\x0b\n\x06\x0b\n\x06\x0c\x0b\x07\x0e\r\t\x0f\x0e\n\x0c\r\x0b\x0c\r\x0b\x0c\r\x0b\x0e\x0f\r\x0f\x10\x0e\x0e\x0f\r\x07\x07\x07\x00\x00\x00')
"""

随机生成12万个bytearray格式的字节,每个像素对应一个字节。

转换为numpy数组

reshape成300*400的灰度图像

reshape成100*400*3的BGR图像

randomByteArray = bytearray(os.urandom(120000))
# Return a bytes object containing random bytes suitable for cryptographic use.
flatNumpyArray = numpy.array(randomByteArray)
# An array object satisfying the specified requirements.

grayImage = flatNumpyArray.reshape(300, 400)
cv2.imwrite('RandomGray.png', grayImage)

bgrImage = flatNumpyArray.reshape(100, 400, 3)
cv2.imwrite('RandomColor.png', bgrImage)

视频文件的读写:

VideoCapture类的read()函数和VideoWriter类的write()函数支持读/写。

cap = cv2.VideoCapture('test.mp4')

fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_FRAME_WIDTH)), int(cap.get(cv2.CAP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('test1.mp4', cv2.VideoWriter_fourcc(), fps, size)
success, frame = cap.read()
while success:
    videoWriter.write(frame)
    success, frame = cap.read()

写时必须指定文件名和视频编解码器: 

 

捕获摄像头的帧

VideoCapture类可以获得摄像头的帧流。(摄像头设备索引)

VideoCapture.isOpened判断是否读取帧成功,返回值为布尔类型进而使用read方法。

当需要同步一组摄像头或一个多头摄像头时,可用grab()和retrive方法。

在窗口显示摄像头帧

创建:namedWindow()

显示:imshow()

销毁:destroyWindow()

获取键盘输入:waitKey(),见第一弹

获取鼠标输入:setMouseCallback()

回调事件参数值可以取如下的值,分别对应不同的鼠标事件。

 只有在读取画面成功且在1毫秒内没有按键输入并且未松开鼠标左键,才能一直显示画面。松开左键,clicked为True,循环条件为假。

clicked = False


def onMouse(event, x, y, flags, param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:
        clicked = True


cameraCapture = cv2.VideoCapture(0)
cv2.namedWindow('my_window')
cv2.setMouseCallback('my_window', onMouse)
print('Click window or press any key to stop')
success, frame = cameraCapture.read()
while success and cv2.waitKey(1) == -1 and not clicked:
    cv2.imshow('my_window', frame)
    success, frame = cameraCapture.read()
cv2.destroyAllWindows()
cameraCapture.release()

 

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值