一、遍历图片三个通道像素点,并修改相应的RGB

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           #灰度值是0-255   这里是修改每个像素点每个通道灰度值cv2.imshow("Reverse_phase_image",image)if __name__ =="__main__":src=cv2.imread('555.png')                          #默认彩色读入cv2.imshow('original_image', src)                  #显示图像t1 = cv2.getTickCount()                            #GetTickcount函数返回从操作系统启动到当前所经过的毫秒数access_pixels(src)t2 = cv2.getTickCount()time = (t2-t1)/cv2.getTickFrequency()              #getTickFrequency函数返回CPU的频率,就是每秒的计时周期数print("time : %s ms"%(time*1000) )                 #输出运行时间cv2.waitKey(0)cv2.destroyAllWindows()

测试如何高效遍历像素

我本来想如何加速opencv-python的遍历像素效率,唯一能够稍微加快的方式是:先遍历小的数据范围,然后遍历大的数据范围。这样才能使遍历的速度有所加快。也就是说以下两种方式效果一样。

通过下述代码发现,最好不要通过遍历像素来操作图像,我们可以用numpy的矩阵运算。
若是C++代码,可以使用C语言的指针来遍历,通过指针操作像素的地址来操作对应数字会大大加速运算。

import cv2
import time
import numpy as np# 遍历依次为通道,高,宽。
def inverse(image):height = image.shape[0]width = image.shape[1]channels = image.shape[2]pixel_data = np.array(image, dtype = np.uint8)for c in range(channels):for row in range(height):for col in range(width):level = pixel_data[row, col, c]pixel_data[row, col, c] = 255 - levelreturn pixel_datadef inverse2(image):height,width = image.shape[:2]channels = image.shape[2]for c in range(channels):for row in range(height):for col in range(width):level = image[row, col, c]image[row, col, c] = 255 - levelreturn imageif __name__ == '__main__':imgpath = r'C:\Users\xxxx\Desktop\201920100013253001_30302_01.jpg'image = cv2.imread(imgpath)print(image.shape)print(image.size)print(image.dtype)start1 = time.time()image1 = inverse(image)end1 = time.time()start2 = time.time()image2 = inverse2(image)end2 = time.time()print('cost_time1:',end1 - start1)print('cost_time2:', end2 - start2)cv2.imshow("inverse1 image", image1)cv2.imshow("inverse2 image", image2)cv2.waitKey(0)cv2.destroyAllWindows()
'''
(1732, 2309, 3)
11997564
uint8
cost_time1: 17.330647706985474
cost_time2: 17.001970291137695Process finished with exit code 0
'''

二、非赋值修改像素

def get_bin_table(threshold=140):  #二值化图片(非函数,直接操作像素,简便快速)table = []for i in range(256):if i < threshold:table.append(0)else:table.append(1)return table

去除二值化后孤立点

def sum_9_region(img, x, y):"""9邻域框,以当前点为中心的田字框,黑点个数:param x::param y::return:"""# todo 判断图片的长宽度下限cur_pixel = img.getpixel((x, y))  # 当前像素点的值width = img.widthheight = img.heightif cur_pixel == 1:  # 如果当前点为白色区域,则不统计邻域值return 0if y == 0:      # 第一行if x == 0:  # 左上顶点,4邻域# 中心点旁边3个点sum = cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x + 1, y)) \+ img.getpixel((x + 1, y + 1))return 4 - sumelif x == width - 1:  # 右上顶点sum = cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x - 1, y)) \+ img.getpixel((x - 1, y + 1))return 4 - sumelse:  # 最上非顶点,6邻域sum = img.getpixel((x - 1, y)) \+ img.getpixel((x - 1, y + 1)) \+ cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x + 1, y)) \+ img.getpixel((x + 1, y + 1))return 6 - sumelif y == height - 1:  # 最下面一行if x == 0:  # 左下顶点# 中心点旁边3个点sum = cur_pixel \+ img.getpixel((x + 1, y)) \+ img.getpixel((x + 1, y - 1)) \+ img.getpixel((x, y - 1))return 4 - sumelif x == width - 1:  # 右下顶点sum = cur_pixel \+ img.getpixel((x, y - 1)) \+ img.getpixel((x - 1, y)) \+ img.getpixel((x - 1, y - 1))return 4 - sumelse:  # 最下非顶点,6邻域sum = cur_pixel \+ img.getpixel((x - 1, y)) \+ img.getpixel((x + 1, y)) \+ img.getpixel((x, y - 1)) \+ img.getpixel((x - 1, y - 1)) \+ img.getpixel((x + 1, y - 1))return 6 - sumelse:  # y不在边界if x == 0:  # 左边非顶点sum = img.getpixel((x, y - 1)) \+ cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x + 1, y - 1)) \+ img.getpixel((x + 1, y)) \+ img.getpixel((x + 1, y + 1))return 6 - sumelif x == width - 1:  # 右边非顶点# print('%s,%s' % (x, y))sum = img.getpixel((x, y - 1)) \+ cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x - 1, y - 1)) \+ img.getpixel((x - 1, y)) \+ img.getpixel((x - 1, y + 1))return 6 - sumelse:  # 具备9领域条件的sum = img.getpixel((x - 1, y - 1)) \+ img.getpixel((x - 1, y)) \+ img.getpixel((x - 1, y + 1)) \+ img.getpixel((x, y - 1)) \+ cur_pixel \+ img.getpixel((x, y + 1)) \+ img.getpixel((x + 1, y - 1)) \+ img.getpixel((x + 1, y)) \+ img.getpixel((x + 1, y + 1))return 9 - sum

OpenCV—Python Numpy数组(像素点)操作相关推荐

  1. python 初始化数组 numpy,Python Numpy 数组的初始化和基本操作

    Python 是一种高级的,动态的,多泛型的编程语言.Python代码很多时候看起来就像是伪代码一样,因此你可以使用很少的几行可读性很高的代码来实现一个非常强大的想法. 一.基础: Numpy的主要数 ...

  2. python numpy数组切片_python中numpy数组切片实验解释

    print(a[0::2]).a[::2].[:,2].[1:,-1:].a[::-1].[ : n].[m : ].[-1].[:-1].[1:]等的含义 文章目录 一维数组(冒号:) 1.一个参数 ...

  3. python numpy数组和one-hot编码相互转换

    a=[0,0,1,0,1,0,1]result=[] for i, x in enumerate(a):if x==1:result.append(i)print(result) python num ...

  4. 【Python语言基础】——Python NumPy 数组副本 vs 视图

    Python语言基础--Python NumPy 数组副本 vs 视图 文章目录 Python语言基础--Python NumPy 数组副本 vs 视图 一.Python NumPy 数组副本 vs ...

  5. 【Python语言基础】——Python NumPy 数组索引

    Python语言基础--Python NumPy 数组形状 文章目录 Python语言基础--Python NumPy 数组形状 一.Python NumPy 数组形状 一.Python NumPy ...

  6. 【Python语言基础】——Python NumPy 数组搜索

    Python语言基础--Python NumPy 数组搜索 文章目录 Python语言基础--Python NumPy 数组搜索 一.Python NumPy 数组搜索 一.Python NumPy ...

  7. python numpy数组含逗号的切片

    普通切片操作: list名[首:尾:步长] numpy数组切片操作: 数组名[首:尾,首:尾:步长](逗号用于区分维度,注意:逗号前面不能限定步长) example 1: import numpy a ...

  8. python numpy数组动态写入csv文件_python - 将NumPy数组转储到csv fi中

    python - 将NumPy数组转储到csv fi中 有没有办法将NumPy数组转储到CSV文件中? 我有一个2D NumPy数组,需要以人类可读的格式转储它. 9个解决方案 588 votes n ...

  9. Python Numpy数组的降序排列 由大到小

    Python内置array数组有sort()函数可以对数组进行排序,将参数reverse值修改为True为降序排列 x = [2, 4, 6, 8, 3, 1] x.sort() # [1, 2, 3 ...

最新文章

  1. 图的实现(邻接链表C#)
  2. java 打包成服务_maven javaProject打包发布成服务
  3. ITK:转换文件格式
  4. python iocp_记对协程增加IOCP支持时候踩过的一些坑
  5. python中@staticmethod_Python中的@staticmethod和@classmethod的区别
  6. JavaOne 2015 –又一年,又向前迈进了一步
  7. fortran调用matlab画图,[转载]Matlab与Fortran的交互--基本概念
  8. java中常见的包类接口_Java中一些常用的类,包,接口
  9. 有家创业公司花重金要请我当CTO?!
  10. nodejs进入mysql数据库_nodejs简单访问及操作mysql数据库的方法示例
  11. ChaiNext:大盘调整,主流币种还未稳住阻力位
  12. 关于数组相关乱七八糟
  13. java图片处理工具类
  14. Oracle:grouping和rollup
  15. PASCAL-VOC2012数据集(vocdevkit、Vocbenchmark_release)详细介绍
  16. 计算机机房的消防验收,机房建设(消防)解决方案
  17. 库克开怼谷歌和Facebook:自己搞的烂摊子,要会自己收拾
  18. C语言程序设计-用英文单词模拟数学计算系统
  19. android 查看手机存储空间以及文件状态及大小
  20. 苹果手机php如何解压,在PHP中使用gzcompress;需要能够在iPhone上解压缩

热门文章

  1. elasticsearch的percolator操作
  2. Linux系列:Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password
  3. 毕业论文终于接近尾声了
  4. Excel转CSV格式
  5. 2024苏州大学计算机考研信息汇总
  6. php strrpos,PHP之mb_strrpos使用
  7. 如何给select标签赋初值
  8. 在windows下配置R-CNN detection selective_search_ijcv_with_python目标检测
  9. 进程创建、进程等待、进程休眠、结束子进程;macOS查看头文件路径;
  10. 关于#matlab#的问题:出错 IF_Distributed_Kalman_difSNR_Fig3_4 (第 202 行) labelEst(itn,:) = double(idx==trust)