一、对图片各个像素点的像素值进行操作
image.shape[0]
:image图像的height
image.shape[1]
:image图像的width
image.shape[2]
:image图像的channels
import cv2
import numpy as npdef 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 - pvcv2.imshow("pixels ",image)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)t1 = cv2.getTickCount()
access_pixels(src)#为了得到该代码的运行时间,通常再这行代码上下放入一个Tick进行计时
t2 = cv2.getTickCount()time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
运行结果为:
运行的时间为619ms时间有点长,这里也就是实现图像像素值的反转操作而已,这里可以调用bitwise_notAPI(这个函数是C++写的),来缩短运行时间
cv2.bitwise_not(image)
:255 - image所对应的像素值
import cv2
import numpy as npdef inverse(image):dst = cv2.bitwise_not(image)cv2.imshow("inverse_image",dst)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
inverse(src)
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
运行效果如下:
二、创建图像
创建一个400×400的三颜色通道的图像,图像的B通道赋值为1,其他颜色通道赋值为0,即显示蓝色
np.zeros([400,400,3],np.uint8)
:创建一个400*400的3颜色通道的图像,全部赋值为0,且每个像素值单位为uint8
import cv2
import numpy as npdef create_image():img = np.zeros([400,400,3],np.uint8)img[:,:,0] = np.ones([400,400])*255cv2.imshow("new image",img)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
create_image()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下:
三、对单通道图像进行操作
方法一:先对400×400大小的单通道图像全部初始化为0,然后再对400×400区域的像素1乘以127,最终转换为127像素的灰色图像。
np.zeros([400,400,3],np.uint8)
:创建一个400400的3颜色通道的图像,全部赋值为0,且每个像素值单位为uint8
np.ones([400,400])*127
:对400400的图像的元素再次基础上分别乘以127
import cv2
import numpy as npdef create_image():img = np.zeros([400,400,1],np.uint8)img[:,:,0] = np.ones([400,400])*127cv2.imshow("new image",img)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
create_image()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
运行效果如下:
方法二:直接对400×400像素大小的单通道图像全部初始化为1,再对图像乘以255,得到白色图像。
img = np.ones([400,400,1],np.uint8)
:创建一个400*400的单通道的图像,全部赋值为1,且每个像素值单位为uint8
img = img * 255
:img图像所有的像素值均对应乘以255
import cv2
import numpy as npdef create_image():img = np.ones([400,400,1],np.uint8)img = img * 255cv2.imshow("new image",img)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
create_image()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下:
四、对矩阵操作
np.ones([3,3],np.uint8)
:创建一个3 * 3的矩阵,全部赋值为1,且每个像素值单位为uint8
m1.fill(52)
:m1图像的所有像素点的像素值全部填充为52
m1.reshape([1,9])
:将m1原本3 * 3的矩阵转换为1* 9的矩阵
import cv2
import numpy as npdef create_image():m1 = np.ones([3,3],np.uint8)m1.fill(52)print(m1)m2 = m1.reshape([1,9])print(m2)src = cv2.imread("G:/Juptyer_workspace/study/opencv/opencv3/cat.png")#BGR
cv2.namedWindow("input image",cv2.WINDOW_AUTOSIZE)
cv2.imshow("input image",src)
t1 = cv2.getTickCount()
create_image()
t2 = cv2.getTickCount()
time = (t2-t1)/cv2.getTickFrequency()#显示当前运行时间
print("time:%s ms"%(time*1000))#输出当前运行时间单位为ms
cv2.waitKey(0)cv2.destroyAllWindows()
效果图如下: