查看原文
其他

Python OpenCV像素操作

gloomyfish OpenCV学堂 2019-03-29

Python OpenCV像素操作

环境声明 : Python3.6 + OpenCV3.3 + PyCharm IDE

首先要引入OpenCV和Numpy支持,添加代码如下:

  1. import cv2 as cv;

  2. import numpy as np;

读写像素

对RGB图像来说,在Python中第一个维度表示高度、第二个维度表示宽度、第三个维度是通道数目,可以通过下面的代码获取图像三个维度的大小

  1. print(image.shape)

  2. print(image.size)

  3. print(image.dtype)

循环读取图像方法一:

直接从图像中读取,缺点是每次都需要访问imread之后的Mat对象,进行native操作,速度是个问题, 代码实现如下:

  1. height = image.shape[0]

  2. width = image.shape[1]

  3. channels = image.shape[2]

  4. print(image.shape)

  5. for row in range(height):

  6.    for col in range(width):

  7.        for c in range(channels):

  8.            level = image[row, col, c]

  9.            pv = level + 30

  10.            image[row, col, c] = (255 if pv>255 else pv)

循环读取图像方法二:

首先通过Numpy把像素数据读到内存中,在内存中进行高效循环访问每个像素,修改之后,在赋值回去即可,代码如下:

  1. # read once

  2. pixel_data = np.array(image, dtype = np.uint8);

  3. # loop pixel by pixel

  4. for row in range(height):

  5.    for col in range(width):

  6.        for c in range(channels):

  7.            level = pixel_data[row, col, c]

  8.            pixel_data[row, col, c] = 255 - level

  9. # write once

  10. image[ : : ] = pixel_data

案例演示 在Python语言中完成图像的属性读取、像素读取与操作、实现了图像的颜色取反、亮度提升、灰度化、梯度化、操作。首先看一下效果:

完整的Python代码实现如下:

  1. import cv2 as cv;

  2. import numpy as np;

  3. def inverse(image):

  4.    print("read and write pixel by pixel")

  5.    print(image.shape)

  6.    print(image.size)

  7.    print(image.dtype)

  8.    height = image.shape[0]

  9.    width = image.shape[1]

  10.    channels = image.shape[2]

  11.    # read once

  12.    pixel_data = np.array(image, dtype = np.uint8);

  13.    # loop pixel by pixel

  14.    for row in range(height):

  15.        for col in range(width):

  16.            for c in range(channels):

  17.                level = pixel_data[row, col, c]

  18.                pixel_data[row, col, c] = 255 - level

  19.    # write once

  20.    image[ : : ] = pixel_data

  21.    cv.imshow("inverse image", image)

  22. def brightness(image):

  23.    print("read and write pixel by pixel")

  24.    height = image.shape[0]

  25.    width = image.shape[1]

  26.    channels = image.shape[2]

  27.    print(image.shape)

  28.    for row in range(height):

  29.        for col in range(width):

  30.            for c in range(channels):

  31.                level = image[row, col, c]

  32.                pv = level + 30

  33.                image[row, col, c] = (255 if pv>255 else pv)

  34.    cv.imshow("inverse image", image);

  35. def to_gray(image):

  36.    print("RGB to Gray Image")

  37.    height = image.shape[0]

  38.    width = image.shape[1]

  39.    channels = image.shape[2]

  40.    print("channels : ", channels);

  41.    print(image.shape)

  42.    for row in range(height):

  43.        for col in range(width):

  44.            blue = image[row, col, 0]

  45.            green = image[row, col, 1];

  46.            red = image[row, col, 2]

  47.            gray = (0.2989*red + 0.5870*green + 0.1140*blue);

  48.            image[row, col, 0] = gray;

  49.            image[row, col, 1] = gray;

  50.            image[row, col, 2] = gray;

  51.    cv.imshow("gray image", image)

  52. def gradient_image(image):

  53.    gx = cv.Sobel(image, cv.CV_32F, 1, 0)

  54.    gy = cv.Sobel(image, cv.CV_32F, 0, 1)

  55.    dst = cv.addWeighted(gx, 0.5, gy, 0.5, 50)

  56.    sobel_abs = np.absolute(dst)

  57.    sobel_8u = np.uint8(sobel_abs)

  58.    cv.imshow("gradient image", sobel_8u)

  59. def clam(pv):

  60.    if pv > 255:

  61.        return 255

  62.    if pv < 0:

  63.        return 0;

  64.    return pv;

  65. print("Image Pixel Operation Demo")

  66. src = cv.imread("D:/vcprojects/images/demo.png")

  67. cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)

  68. cv.imshow("input image", src)

  69. gradient_image(src)

  70. cv.waitKey(0)

  71. cv.destroyAllWindows()



云厚者,雨必猛,

弓劲者,箭必远!


关注【OpenCV学堂】

长按或者扫码下面二维码即可关注

+OpenCV识别群 657875553

进群暗号:识别


    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存