📜  Python OpenCV – 获取和设置像素

📅  最后修改于: 2022-05-13 01:54:46.295000             🧑  作者: Mango

Python OpenCV – 获取和设置像素

在本文中,我们将讨论通过Python中的 OpenCV 获取和设置像素。

图像由像素组成。一个像素将被表示为一个数组。 3个整数依次代表红、绿、蓝的强度。例如。 RGB 模式下的 [0,0,0] 表示黑色。还有其他模式——

  • 单纯疱疹病毒
  • 灰度
  • CMY

可以使用返回像素矩阵的 imread()函数读取图像(默认为 RGB 模式)。

使用的图像:

句法:

For Image shape: image.shape
For getting a pixel: image[row][col]
For setting a pixel: image[row][col] = [r,g,b]

示例 1 :显示图像详细信息的Python代码

Python3
# import cv2 module
import cv2
 
# resd the image
img = cv2.imread('image.png')
 
# shape prints the tuple (height,weight,channels)
print(img.shape)
 
# img will be a numpy array of the above shape
print(img)


Python3
import cv2
 
# read the image
img = cv2.imread('image.png')
 
# this is pixel of 0th row and 0th column
print(img[0][0])


Python3
# import the cv2 package
import cv2
 
# read the image
img = cv2.imread('image.png')
for i, row in enumerate(img):
 
  # get the pixel values by iterating
    for j, pixel in enumerate(img):
        if(i == j or i+j == img.shape[0]):
 
                # update the pixel value to black
            img[i][j] = [0, 0, 0]
 
# display image
cv2.imshow("output", img)
cv2.imwrite("output.png", img)


Python3
import cv2
img = cv2.imread('image.png', 0)
 
 
# shape prints the tuple (height,weight,channels)
print("image shape = ", img.shape)
 
# img will be a numpy array of the above shape
print("image array = ", img)
 
print("pixel at index (5,5): ", img[5][5])


输出:

(225, 225, 3)
[[[ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]

[[ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]
 
...

[[ 72 133   9]
 [ 72 133   9]
 [ 72 133   9]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]

[[ 72 133   9]
 [ 72 133   9]
 [ 72 133   9]
 ...
 [ 87 157  14]
 [ 87 157  14]
 [ 87 157  14]]]

这里有 225*225 像素,每个像素都是 3 个整数(红、绿、蓝)的数组。

示例 2 :在此示例中,可以使用索引提取单个像素。

Python3

import cv2
 
# read the image
img = cv2.imread('image.png')
 
# this is pixel of 0th row and 0th column
print(img[0][0])

输出:

[ 87 157  14]

示例 3 :在图像上制作黑色十字的Python代码。

为此,我们将提取所有 (i,j),使得i==j 或 i+j == 图像宽度,并且对于索引为 (i,j) 的所有像素,像素的值将设置为 [0,0, 0]。

Python3

# import the cv2 package
import cv2
 
# read the image
img = cv2.imread('image.png')
for i, row in enumerate(img):
 
  # get the pixel values by iterating
    for j, pixel in enumerate(img):
        if(i == j or i+j == img.shape[0]):
 
                # update the pixel value to black
            img[i][j] = [0, 0, 0]
 
# display image
cv2.imshow("output", img)
cv2.imwrite("output.png", img)

输出:

示例 4:获取灰度然后像素将只是表示白色强度的数字。

Python3

import cv2
img = cv2.imread('image.png', 0)
 
 
# shape prints the tuple (height,weight,channels)
print("image shape = ", img.shape)
 
# img will be a numpy array of the above shape
print("image array = ", img)
 
print("pixel at index (5,5): ", img[5][5])

灰度图像:

输出:

image shape =  (225, 225)
image array =  
[[106 106 106 ... 106 106 106]
 [106 106 106 ... 106 106 106]
 [106 106 106 ... 106 106 106]
 ...
 [ 88  88  88 ... 106 106 106]
 [ 88  88  88 ... 106 106 106]
 [ 88  88  88 ... 106 106 106]]
pixel at index (5,5):  106