Python opencv(四) 像素取反

2021/5/7 22:25:40

本文主要是介绍Python opencv(四) 像素取反,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

前提条件

使用工具python3.x
使用库numpy;opencv,

api简介

1.cv.floodFill
对所选某一区域进行填充颜色,参数解释具体看代码
2.cv.getTickCount()
计算函数运行时间
2.cv.imwrite
保存图片

代码示例

import cv2 as cvimport numpy as npdef accessPixels(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):
				pic_pv = image[row, col, c]
				image[row, col, c] = 255 - pic_pv
	cv.imshow('pixelsDemo',image)def createImage(): #图片基本的颜色
	# img = np.zeros([400,400,3],np.uint8) #建立一个三通道的图片
	# # img[:,:,0] => 宽、高、通道(通道值是0,1,2)
	# img[:,:,0]=np.ones([400,400])*255 #全蓝色
	# img[:,:,1]=np.ones([400,400])*255 #全绿色
	# img[:,:,2]=np.ones([400,400])*255 #全红色
	# cv.imshow("new image",img)

	img = np.zeros([39,296,1],np.uint8) #建立一个单通道的图片
	img[:,:,0]=np.ones([39,296])*255 #灰度图像
	cv.imshow("new image",img)
	cv.imwrite('test.png',img)

	# img = np.ones([400, 400, 1], np.uint8)
	# img = img * 127 #全灰的
	# img = img * 255 #全白的
	# img = img * 0 #全黑
	# cv.imshow("new image",img)
	# cv.imwrite("路径名",img) # 可以将图片写到(保存)自定义的路径下面

	#np数组基本操作
	# m1 = np.ones([3, 3], np.float32) #np.float32 数组数据类型
	# m1.fill(122.388) #将数据填充到数组里面
	# print(m1)
	# m2 = m1.reshape([1,9]) #将3*3的数组转换成1*9(1行9列)
	# print(m2)def Inverse(image): #accessPixels 的升级版,简单方便
	dst = cv.bitwise_not(image) #逻辑非操作
	cv.imshow('Inverse Demo', dst)src = cv.imread("D:/openCV_image/image/1.png")# print(src)# cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)cv.imshow("input image",src)# #计算函数运行时间# t1 = cv.getTickCount()# accessPixels(src)# t2 = cv.getTickCount()# time = (t2-t1)/cv.getTickFrequency()# print("time : %s"%(time*1000)) # 花费的时间,毫秒createImage()# Inverse(src)cv.waitKey(0)cv.destroyAllWindows()

               



这篇关于Python opencv(四) 像素取反的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程