opencv学习(三):Numpy数组操作

参考链接:https://blog.csdn.net/u011321546/article/details/79523115numpy操作数组输出图片

 代码如下:

#导入cv模块
import cv2 as cv
#导入numpy模块
import numpy as np

#访问图片的所有像素
def access_pixels(image):

    #图片属性的读取
    print(image.shape)
    height=image.shape[0]
    width=image.shape[1]
    channels=image.shape[2]
    print("width:%s,height:%s,channels:%s"%(width,height,channels))

    #循环获取每一个像素点,进行改变
    for row in range(height):
        for col in range(width):
            for c in range(channels):
                pv=image[row,col,c]
                image[row,col,c]=255-pv
    cv.imshow("pixels_demo",image)

#创建图像
def create_image():
    '''
    img=np.zeros([400,400,3],np.uint8)
    img[: , :,0]=np.ones([400,400])*255  #改变第一个channel的值
    img[:, :, 1] = np.ones([400, 400]) * 255  # 改变第二个channel的值
    img[:, :, 2] = np.ones([400, 400]) * 255  # 改变第三个channel的值
    cv.imshow("new image",img)

    img=np.ones([400,400,1],np.uint8)
    img=img*0
    cv.imshow("new image", img)
    cv.imwrite("F:/Projects/images/myImage1.png",img)
    '''

    m1=np.ones([3,3],np.uint32)
    m1.fill(122.138)
    print(m1)

    m2=m1.reshape([1,9])
    print(m2)

    m3=np.array([[2,3,4],[4,5,6],[7,8,9]],np.int32)
    #m3.fill(9)
    print(m3)

def inverse(image):  #循环获取每一个像素点,进行改变
    dst=cv.bitwise_not(image)
    cv.imshow("pixels_demo",dst)

print("---------------Hi,Python!-----------------")
# 读取图像,支持 bmp、jpg、png、tiff 等常用格式
src = cv.imread("F:/Projects/images/1.jpg")#blue,green,red
#创建窗口并显示图像
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
t1=cv.getTickCount()
#access_pixels(src)
inverse(src)
create_image()
t2=cv.getTickCount()
time=(t2-t1)/cv.getTickFrequency()
print("spent time:%s ms"%(time*1000))
cv.waitKey(0)
#释放窗口
cv.destroyAllWindows()



               

 

你可能感兴趣的:(opencv学习(三):Numpy数组操作)