03.opencv中Numpy数组操作改变图片

#coding:utf-8
#file: tutorial_2.py
#@author: young
#@contact: [email protected]
#@time: 2019/12/14 8:03
import  cv2 as cv
import  numpy as np
def access_picture(image):
    print(image.shape)
    height = image.shape[0]
    width = image.shape[1]
    channle = image.shape[2]
    print("width:%s,height:%s.channle:%s"%(width,height,channle))
    #遍历修改每一个像素点
    for row in range(width):
        for col in range(height):
            for c in range(channle):
                pv = image[row,col,c]
                image[row,col,c] = 255 - pv
    cv.imshow("pixel_demo",image)

print("___________hello python__________")
src = cv.imread("lena.jpg")#blue,green,red
cv.imshow("input mages",src)
access_picture(src)
cv.waitKey(0)
cv.destroyAllWindows()

左图是生成的,右图是原图

在这里插入图片描述计算花费的时间:

cv.imshow("input mages",src)
t1 = cv.getTickCount()
access_picture(src)
t2 = cv.getTickCount()
time = (t2-t1)/cv.getTickFrequency()
print("time: %s ms"%(time*1000))
cv.waitKey(0)
cv.destroyAllWindows()

在这里插入图片描述自己创建一张图,只不过是黑色的:

def creat_image():
     img = np.zeros([400,400,3],np.uint8)
     cv.imshow("img images",img)

在这里插入图片描述

多通道操作:

多通道一般是rgb图像

def creat_image():
     img = np.zeros([400,400,3],np.uint8)
     img[:,:,1] = np.ones([400,400])*255
     #img[:,:,1],可以修改为0,1,2
     ##blue,green,red
     cv.imshow("img images",img)

在这里插入图片描述

单通道·操作:

单通道一般是灰色图像

def creat_image():
     img = np.zeros([400,400,1],np.uint8)
     img[:,:,0] = np.ones([400,400])*127
     cv.imshow("new image",img)

灰色图:
在这里插入图片描述

def creat_image():
	 #使用ones更加的灵活
     img = np.ones([400,400,1],np.uint8)
     img = img*255
     #可以修改,0就是黑色,127灰色
     cv.imshow("new image",img)

在这里插入图片描述

初始化二维数组

def creat_image():
     m1 = np.ones([3,3],np.float32)#可以改变类型
     m1.fill(122.388)
     print(m1)

在这里插入图片描述变换:

def creat_image():
     m1 = np.ones([3,3],np.uint8)
     m1.fill(12222.388)
     print(m1)
     m2 = m1.reshape([1,9])
     #3*3行变为1行9列
     print(m2)

在这里插入图片描述

发布了60 篇原创文章 · 获赞 8 · 访问量 3327

猜你喜欢

转载自blog.csdn.net/qq_43476433/article/details/103535871
今日推荐