OpenCV-Python学习(5)—— OpenCV 图像像素的读写操作

2022-10-24 15:58:51 浏览数 (1)

1. 学习目标

  1. 图像像素的读写操作;
  2. 图像像素的遍历;

2. 像素的理解

  1. 像素实际大小:dpi * inches = 像素总数;
  2. ppi (pixels per inch):图像的采样率 (在图像中,每英寸所包含的像素数目)
  3. dpi (dots per inch): 打印分辨率 (每英寸所能打印的点数,即打印精度)

3. OpenCV 中像素

  1. 灰度图像排序
  1. 彩色图像排序

4. 像素的访问与赋值

4.1 获取图像维度信息;image.shape
4.1.1 灰度图像维度信息
  1. 获取灰度图像维度信息代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg",0)
  print(image.shape)

if __name__ == "__main__":
  get_image_info()
  1. 获取灰度图像维度信息结果
4.1.2 彩色图像维度信息
  1. 获取彩色图像维度信息代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg")
  print(image.shape)

if __name__ == "__main__":
  get_image_info()
  1. 获取彩色图像维度信息结果
4.2 图像访问像素:image[row,col];
4.2.1 灰度图像访问像素
  1. 获取灰度图像访问像素代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg",0)
  print(image[100,100])

if __name__ == "__main__":
  get_image_info()
  1. 获取灰度图像访问像素结果
4.2.2 彩色图像访问像素
  1. 获取彩色图像访问像素代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg")
  print(image[100,100])

if __name__ == "__main__":
  get_image_info()
  1. 获取彩色图像访问像素结果
4.3 图像赋值像素:image[row,col] = xxx;
4.3.1 灰度图像赋值像素
  1. 获取灰度图像赋值像素代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg",0)
  image[0:100,0:100] = 255
  cv.imshow('change 0 - 100:', image)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  get_image_info()
  1. 获取灰度图像赋值像素结果
4.3.2 彩色图像赋值像素
  1. 获取彩色图像赋值像素代码
代码语言:javascript复制
import cv2 as cv

def get_image_info():
  image = cv.imread("./images/butterfly.jpg")
  image[0:100,0:100] = (255,0,0)
  cv.imshow('change 0 - 100:', image)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  get_image_info()
  1. 获取彩色图像赋值像素结果
4.4 注意
  1. 灰度图像是单通道,像素赋值范围[0-255];
  2. 彩色图像是三通道,像素赋值范围([0-255],[0-255],[0-255]);(b,g,r)
  3. 不能将【三通道的彩色图像】像素赋值给【单通道的灰度图像】,会报错:

5. 像素遍历

OpenCV 像素遍历的本质就是numpy数组访问!!!

5.1 彩色图像像素遍历
5.1.1 彩色图像像素遍历代码
代码语言:javascript复制
import cv2 as cv

def visit_pixel_demo():
  image = cv.imread("./images/butterfly.jpg")
  cv.imshow('original:', image)
  h,w,c = image.shape

  for row in range(h):
    for col in range(w):
      b,g,r = image[row,col]
      image[row,col] = (255-b,255-g,255-r)
  cv.imshow('visited:', image)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  visit_pixel_demo()
5.1.2 彩色图像像素遍历结果
5.2 灰度图像像素遍历
5.2.1 灰度图像像素遍历代码
代码语言:javascript复制
import cv2 as cv

def visit_pixel_demo():
  image = cv.imread("./images/butterfly.jpg",0)
  cv.imshow('original', image)
  h,w = image.shape

  for row in range(h):
    for col in range(w):
      p_value = image[row,col]
      image[row,col] = 255 - p_value
  cv.imshow('visited', image)
  cv.waitKey(0)
  cv.destroyAllWindows()

if __name__ == "__main__":
  visit_pixel_demo()
5.2.2 灰度图像像素遍历结果

6. 总结

  1. 图像维度信息:image.shape;
  2. 访问图像像素:image[row,col];
  3. 图像赋值像素:image[row,col] = xxx;
  4. OpenCV 像素遍历的本质就是numpy数组访问!!!

0 人点赞