Nunmpy数组包含:

  • 强大的N维数组对象

  • 复杂的(广播)功能

  • 集成C / C ++和Fortran代码的工具

  • 有用的线性代数,傅立叶变换和随机数功能

遍历与修改数组中的所有像素点

 #对所有像素进行循环
def 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 - pv
cv.imshow("pixels_demo",image)

创建新图像

创建新图像:

 np.zeros([400, 400, 3], np.uint8)        #形状、类型

代码:

 #通过Numpy对一个数组指定维数赋值
def create_image():
'''
#(创建一个新三通道的图像)
#多通道常见为RGB图像
# img = np.zeros([400, 400, 3], np.uint8)
# img[: ,: ,0] = np.ones([400, 400])*255 #0表示第一通道Blue;1表示第二通道Green;3表示第三通道Read #单通道常见为灰度图像
# img = np.zeros([400, 400, 1], np.uint8) #创建新的图像
# img[:, :, 0] = np.ones([400, 400]) * 127
img = np.ones([400, 400, 1], np.uint8)
img =img*127
cv.imshow("new image", img) #窗口显示
cv.imwrite("C:/Users/shinelon/Desktop/DL/001.png") #将图像保存
''' #初始化二维,打印像素点
m1 = np.ones([3, 3], np.float32)
m1.fill(222.388)
print(m1) m2 = m1.reshape([1,9]) #改变其在空间的形状
print(m2)

其他知识点

获取当前CPU时钟:

 t1 = cv.getTickCount()                  #获取当前CPU时间

完整代码

 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] #通道数
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 - pv
cv.imshow("pixels_demo",image) def inverse(image):
dest = cv.bitwise_not(image) #像素取反,依靠C的代码
cv.imshow("inverse",dest) #通过Numpy对一个数组指定维数赋值
def create_image():
'''
#(创建一个新三通道的图像)
#多通道常见为RGB图像
# img = np.zeros([400, 400, 3], np.uint8)
# img[: ,: ,0] = np.ones([400, 400])*255 #0表示第一通道Blue;1表示第二通道Green;3表示第三通道Read #单通道常见为灰度图像
# img = np.zeros([400, 400, 1], np.uint8) #创建新的图像
# img[:, :, 0] = np.ones([400, 400]) * 127
img = np.ones([400, 400, 1], np.uint8)
img =img*127
cv.imshow("new image", img) #窗口显示
cv.imwrite("C:/Users/shinelon/Desktop/DL/001.png") #将图像保存
''' #初始化二维,打印像素点
m1 = np.ones([3, 3], np.float32)
m1.fill(222.388)
print(m1) m2 = m1.reshape([1,9]) #改变其在空间的形状
print(m2) m3 = np.array([[2, 3, 4], [4, 5, 6], [7, 8, 9]], np.int32) #卷积神经需要
#m3.fill(9)
print(m3) print("------Python OpenCV Tutorial-----")
src = cv.imread("C:/Users/shinelon/Desktop/DL/12.png") #括号类为图片的绝对路径
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src) #将图片在Windows窗口显示
t1 = cv.getTickCount() #获取当前CPU时间
create_image()
#access_pixels(src) #时间比较长
inverse(src) #优化,时间较短
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency() #运行的时间ms
print("time : %s ms"%(time*1000))
cv.waitKey(0) cv.destroyAllWindows()

OpenCV-----Numpy数组的更多相关文章

  1. Python图像处理丨OpenCV+Numpy库读取与修改像素

    摘要:本篇文章主要讲解 OpenCV+Numpy 图像处理基础知识,包括读取像素和修改像素. 本文分享自华为云社区<[Python图像处理] 二.OpenCV+Numpy库读取与修改像素> ...

  2. numpy数组的操作

    numpy - 介绍.基本数据类型.多维数组ndarray及其内建函数 http://blog.csdn.net/pipisorry/article/details/22107553 http://w ...

  3. numpy数组、向量、矩阵运算

    可以来我的Github看原文,欢迎交流. https://github.com/AsuraDong/Blog/blob/master/Articles/%E6%9C%BA%E5%99%A8%E5%AD ...

  4. Numpy数组对象的操作-索引机制、切片和迭代方法

    前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法.使用索引和切片的方法选择元素,还有如何数组的迭代方法. 一.索引机制 1.一维数组 In [1]: a = np.arange ...

  5. 操作 numpy 数组的常用函数

    操作 numpy 数组的常用函数 where 使用 where 函数能将索引掩码转换成索引位置: indices = where(mask) indices => (array([11, 12, ...

  6. NumPy 超详细教程(1):NumPy 数组

    系列文章地址 NumPy 最详细教程(1):NumPy 数组 NumPy 超详细教程(2):数据类型 NumPy 超详细教程(3):ndarray 的内部机理及高级迭代 文章目录 Numpy 数组:n ...

  7. NumPy数组对象

    1.创建NumPy数组 import numpy as np # 创建3*2*4的三维数组 a = np.arange(24).reshape(3, 2, 4) # 打印三维数组的所有元素 print ...

  8. Numpy 数组属性

    Numpy 数组的维数称为秩(rank),一维数组的秩为 1 , 二维数组的秩为 2 , 以此类推:在Numpy中, 每一个线性的数组称为是一个轴(axis),也就是维度(dimensios).比如说 ...

  9. numpy 数组对象

    numpy 数组对象NumPy中的ndarray是一个多维数组对象,该对象由两部分组成:实际的数据,描述这些数据的元数据# eg_v1 import numpy as np a = np.arange ...

  10. python numpy 数组拼接

    我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2],       [3, 4, 5],       [6, 7, ...

随机推荐

  1. 多模字符串匹配算法-Aho–Corasick

    背景 在做实际工作中,最简单也最常用的一种自然语言处理方法就是关键词匹配,例如我们要对n条文本进行过滤,那本身是一个过滤词表的,通常进行过滤的代码如下 for (String document : d ...

  2. 关于反射和动态代理和AOP

    package Exercise.reflect; /** * 反射把java中所有的东西都当做对象,甚至是类的本身也作为一种对象,并把它作为Class的对象的实例: * 反射是把类.类的属性.方法都 ...

  3. github-搜索功能

    in:name spring boot stars:>3000   //在标题上查找spring boot 并且 stars >3000 in:readme spring boot sta ...

  4. Java的基本使用

    1.如何运行一个Java源码 打开文本编辑器,输入以下代码: public class Hello { public static void main(String[] args) { System. ...

  5. 求大师点化,寻求大文件(最大20G左右)上传方案

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  6. R 大小写转换

    >x = "CAGTTTCTTGAGTCTGATTAATTCAGGTTTCGGGGT"#定义字符串变量x>tolower(x)[1] "cagtttcttga ...

  7. 一、Nginx常见问题

    1.相同server_name多个虚拟主机优先级访问 最先读取哪个配置文件,就访问那个的网页 2.location匹配优先级 相同location,会被后面的覆盖 匹配优先级更高的,找后面的 =    ...

  8. Spring Boot 集成 JPA 的步骤

    Spring Boot 集成 JPA 的步骤 配置依赖 compile group: 'org.springframework.boot', name: 'spring-boot-starter-da ...

  9. How to derive mean and variance of a Gaussian?

    PRML exercise 1.8: To derive mean: change of variable z = x - u, use symmetry To derive variance: di ...

  10. python 定时器,轮询定时器

    首先想要实现的效果是:每隔1段时间,就去调用1个接口确认结果,直到接口返回的结果为true,停止调用 所以这里会用到python的定时器 先来了解最简单的定时器: python 定时器默认定时器只执行 ...