Python3+OpenCV3+Pycharm编程:图片像素Numpy基本操作

  • 遍历像素

  1. 属性读取,输出高,宽,通道,像素值信息
  2. 循环读取像素,对像素取反
  3. OpenCV提供像素取反API:cv.bitwise_not(image)
  4. 循环与API速度对比:2000ms:60ms(i7)
  • 程序计时

  1. 利用OpenCV提供API计时,也可通过Python提供time模块
time1 = cv.getCPUTickCount()  
# getCPUTickCount用于返回从操作系统启动到当前所经的计时周期数

stetements

time2 = cv.getCPUTickCount()

print("Time:%s ms" % ((time2 - time1) / cv.getTickFrequency() * 1000))  
# getTickFrequency用于返回CPU的频率
  • 创建图片

  1. 创建三通道
# 三通道
creat_img3 = np.zeros([400, 400, 3], np.uint8)  # 高400*宽400*通道3 单通道8位
creat_img3[:, :, 0] = np.ones([400, 400]) * 255  # 0(Blue)通道全部赋值255
creat_img3[:, :, 2] = np.ones([400, 400]) * 255  # 2(Red)通道全部赋值255
cv.imshow("create image3", creat_img3)

     2. 创建单通道

 # 单通道
creat_img1 = np.zeros([400, 400, 1], np.uint8)
creat_img1[:, :, 0] = np.ones([400, 400]) * 127  # 单通道灰度图像
cv.imshow("create image1", creat_img1)

     3. 创建小尺寸

# 生成小尺寸图片
mini_img = np.ones([3, 3], np.float)
mini_img.fill(127.1)  # .fill方法填充数据
mini_img2 = mini_img.reshape([1, 9])  # reshape只改变形状,不改变数据
print(mini_img, mini_img2)
  • 代码

# -*- coding: utf-8 -*-
# By:iloveluoluo
# 2019.3.21
import cv2 as cv
import numpy as np


def access_pixels(image):
    """遍历像素"""

    # 属性读取
    # print(image.shape)  # (高,宽,通道数)
    height = image.shape[0]
    width = image.shape[1]
    channels = image.shape[2]  # Blue, Green, Red
    print("height:%s, width:%s, channels:%s, pixels:%s" % (height, width, channels, image.size))

    # 像素读取
    for row in range(height):  # 行
        for col in range(width):  # 列
            for chl in range(channels):  # 维度
                pv = image[row, col, chl]
                image[row, col, chl] = 255 - pv

    cv.imshow("pixels_demo", image)  # 显示遍历修改完的图片


def inverse(image):
    """利用OpenCV API像素取反"""
    dst = cv.bitwise_not(image)
    cv.imshow("inverse image", dst)


def create_image():
    """创建图像"""

    """
    # 三通道
    creat_img3 = np.zeros([400, 400, 3], np.uint8)  # 高400*宽400*通道3 单通道8位
    creat_img3[:, :, 0] = np.ones([400, 400]) * 255  # 0(Blue)通道全部赋值255
    creat_img3[:, :, 2] = np.ones([400, 400]) * 255  # 2(Red)通道全部赋值255
    cv.imshow("create image3", creat_img3)
    """

    """
    # 单通道
    creat_img1 = np.zeros([400, 400, 1], np.uint8)
    creat_img1[:, :, 0] = np.ones([400, 400]) * 127  # 单通道灰度图像
    cv.imshow("create image1", creat_img1)
    """

    """
    # 单通道,快速初始化
    create_img1 = np.ones([400, 400, 1], np.uint8)
    create_img1 *= 127
    cv.imshow("fast creat image1", create_img1)
    """

    # 生成小尺寸图片
    mini_img = np.ones([3, 3], np.float)
    mini_img.fill(127.1)  # .fill方法填充数据
    mini_img2 = mini_img.reshape([1, 9])  # reshape只改变形状,不改变数据
    print(mini_img, mini_img2)

    # x = np.array([1, 2], [3, 4], np.int32)  # 按要求生成数组


img = cv.imread('E:/MyFile/Picture/date/zly.jpg')  # 读取图片
cv.imshow("img", img)

# 遍历像素并修改
time1 = cv.getCPUTickCount()  # getCPUTickCount用于返回从操作系统启动到当前所经的计时周期数
access_pixels(img)
time2 = cv.getCPUTickCount()
print("Time:%s ms" % ((time2 - time1) / cv.getTickFrequency() * 1000))  # getTickFrequency用于返回CPU的频率
# 总次数/一秒内重复的次数*1000 = 时间(ms)

# API速度对比
time3 = cv.getCPUTickCount()
inverse(img)
time4 = cv.getCPUTickCount()
print("Time:%s ms" % ((time4 - time3) / cv.getTickFrequency() * 1000))

# create_image()

key = cv.waitKey(0)  # 无限等待
cv.destroyAllWindows()

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值