Python opencv(四) 像素取反

前提条件

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

api简介

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

代码示例

import cv2 as cv
import numpy as np

def 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()


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值