opencv 机器视觉 学习笔记

混江龙づ霸主 提交于 2021-02-03 08:01:43
import numpy as np
import cv2,os

# img = numpy.zeros((3,3),dtype=numpy.uint8)
# print(img)
# print(img.shape)
# img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
# print("=============",img)



#修改图片格式
# img = cv2.imread("1123.jpg")
# cv2.imwrite("1123.png",img)
#
# grayimg = cv2.imread('1123.png',cv2.IMREAD_GRAYSCALE)
# cv2.imwrite("11231.png",grayimg)
#
# print(grayimg[0,0])
# print(grayimg.item((0,0)))
# #grayimg.setitem((0,0),108)
# #grayimg[0,0] =128
#
# byteimage = bytearray(grayimg)
# print(byteimage)



#将含有随机字节的bytearray转换为灰度图像和BGR图像
# randombytearray = bytearray(os.urandom(120000))
# flatNumpyarray = numpy.array(randombytearray)
#
# grayimage = flatNumpyarray.reshape(300,400)
# cv2.imwrite('grayimg.png',grayimage)
#
#
# bgrimage = flatNumpyarray.reshape(100,400,3)
# cv2.imwrite('igrimage.png',bgrimage)


#将bgr在(0,0)处的像素转化为白像素
# img = cv2.imread('igrimage.png')
# img[0,0] = [255,255,255]



#将坐标(90,120)的当前蓝色值(127)变成255
# img = cv2.imread('igrimage.png')
# print(img.item(90,120,0))
# img.itemset((90,120,0),255)
# print(img.item(90,120,0))

#将所有G(绿色)值设置为0
# img = cv2.imread('igrimage.png')
# img[:, :, 1] =0


#将图像的一部分拷贝至另一个区域
# img = cv2.imread('1.jpg')
# img_roi = img[0:30,0:30]
# img[50:80,50:80] = img_roi
# print(img.shape)
# print(img.size)
# print(img.dtype)




#读取视频avi文件的帧,并采用YUV颜色编码将其写入另一个帧中
# videocapture = cv2.VideoCapture('test13.avi')
# fps = videocapture.get(cv2.CAP_PROP_FPS)
# size = (int(videocapture.get(cv2.CAP_PROP_FRAME_WIDTH))
# ,int(videocapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# videoWrite = cv2.VideoWriter('test13out.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
#
# success,frame = videocapture.read()
# while success:
# videoWrite.write(frame)
# success,frame = videocapture.read()



#读取摄像头,并写入一个avi文件中
# url = "rtsp:admin:123456@192.168.2.134/stream1"
# camera = cv2.VideoCapture(url)
# fps = 30
# size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# videowriter = cv2.VideoWriter('outvideo.avi',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
# success,frame = camera.read()
# numframeremaining = 10 * fps -1
# while success and numframeremaining >0:
# videowriter.write(frame)
# success,frame = camera.read()
# numframeremaining -=1
# camera.release()

#当需要同步一组摄像头或一个多头摄像头,用途grab()和retrive()来替代read()函数。





#显示一幅图像
# img = cv2.imread('1.jpg')
# cv2.imshow('my image',img)
# cv2.waitKey()
# cv2.destroyAllWindows()



#显示实时摄像肉帧
# url = "rtsp:admin:123456@192.168.2.134/stream1"
# clicked = False
# def onMouse(event,x,y,flags,param):
# global clicked
# if event ==cv2.EVENT_LBUTTONUP:
# clicked =True
# cameracapture = cv2.VideoCapture(url)
# cv2.namedWindow('MyWindow')
# cv2.setMouseCallback('MyWindow',onMouse)
#
# success,frame = cameracapture.read()
# while success and cv2.waitKey(1) == -1 and not clicked:
# cv2.imshow('MyWindow',frame)
# success,frame = cameracapture.read()
# cv2.destroyWindow('MyWindow')
# cameracapture.release()



#waitKey()的参数为等待键盘触发的时间,单位为毫秒,返回值是-1(表示没有键被按下)或ascii码。
#在一些系统中,waitKey()的返回值可能比ascii码的值更大,linux中,如果
#opencv使用GTK作为后端的GUI库,就会出现bug。
#在所有系统中,可以通过读取返回值的最后一个字节来保证只提取ascii码
# keycode = cv2.waitKey(1)
# if keycode !=-1:
# keycode &=0xFF
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!