opencv 学习 1

opencv 学习的第一天
#coding:utf-8
import cv2 as cv
#首先读图片
src = cv.imread(“img/1.jpg”)
#设置图片的名字
cv.namedWindow(“1”, cv.WINDOW_AUTOSIZE)
#显示图片第一个参数设置图片名,第二个参数图片的地址
cv.imshow(“1”,src)
cv.waitKey(0)
#将图片写入固定位置
cv.imwrite(“img/2.jpg”,src)
#清除所有图片的窗口
cv.destroyAllWindows()

以下代码设计numpy操作,cv.VideoCapture(0)无法调用摄像头问题无法解决
getTickCount()可以获得当前时间
waitKey()设置图片的存在时间,设置为0或者不写代表无限。注意()内单位是毫秒

#coding:utf-8
import cv2 as cv
import numpy as np

'''
def video_demo():
    capture =cv.VideoCapture("C:/Users/asus/Desktop/python/opencv3/video/shiping.mp4")
    while(True):
        ret,fram=capture.read()
        fram=cv.flip(fram,1)
        cv.imread("video",fram)
        c=cv.waitKey(1000)
        if c==27:
            break'''
def get_img(img):
    height=img.shape[0]
    width=img.shape[1]
    c=img.shape[2]
    print('height:%s width:%s c:%s'%(height,width,c))
    for cow in range(height):
        for vc in range(width):
            for cc in range(c):
                pv=img[cow,vc,cc]
                img[cow,vc,cc]=255-pv
    cv.imshow("tets",img)            
def inverse(img):
    dst=cv.bitwise_not(img)
    cv.imshow("test",dst) 
def creat():
    '''  img=np.zeros([400,400,3],np.uint8)
    img[:,:,0]=np.ones([400,400])*255
    cv.imshow("creat",img)    
    img=np.zeros([400,400,1],np.uint8)
    img[:,:,0]=np.ones([400,400])*127
    cv.imshow("creat",img)
       img=np.ones([400,400,1],np.uint8)
    img=img*127
    cv.imshow("test",img)
    cv.imwrite("img/2.jpg",img)
    '''
    m1=np.ones([3,3],np.float32)
    m1.fill(23.123)
    print(m1)
    m2=m1.reshape(1,9)
    print(m2)
 

src=cv.imread("img/1.jpg")
cv.imshow("1",src)
t1=cv.getTickCount()
inverse(src)
t2=cv.getTickCount()
time=(t2-t1)/cv.getTickFrequency()
print("time:%s ms"%(time*1000))
creat()
cv.waitKey(0)
cv.destroyAllWindows()

你可能感兴趣的:(opencv,计算机视觉,python)