Python image processing丨OpenCV+Numpy library to read and modify pixels

Abstract: This article mainly explains the basics of OpenCV+Numpy image processing, including reading pixels and modifying pixels.

This article is shared from Huawei Cloud Community " [Python Image Processing] 2. OpenCV+Numpy Library Reading and Modifying Pixels ", author: eastmount.

1. Traditional reading pixel method

1. Grayscale image, returns the grayscale value.

Return value = image (position parameter), for example: p = img[88,142] print§

# -*- coding:utf-8 -*-
import cv2

#读取图片
img = cv2.imread("picture.bmp", cv2.IMREAD_UNCHANGED)

#灰度图像
p = img[88, 142]
print(p)

#显示图像
cv2.imshow("Demo", img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

#写入图像
cv2.imwrite("testyxz.jpg", img)

The output result is shown in the following figure: [131 131 131], since the figure is a 24-bit BMP, B=G=R outputs three identical results, and some images only have one pixel and output one value.

2. BGR image, the return value is the value of B, G, R.

example:

b = img[78, 125, 0] print(b)
g = img[78, 125, 1] print(g)
r = img[78,125, 2] print®

# -*- coding:utf-8 -*-
import cv2

#读取图片
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)

#BGR图像
b = img[78, 125, 0]
print(b)
g = img[78, 125, 1]
print(g)
r = img[78, 125, 2]
print(r)

#方法二
bgr = img[78, 125]
print(bgr)

#显示图像
cv2.imshow("Demo", img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

#写入图像
cv2.imwrite("testyxz.jpg", img)

The output pixels and images look like this:

155
104
61
[155 104 61]

2. The traditional method of modifying pixels

1. Modify a single pixel value

The BGR image can directly access the pixel value and modify it through the position parameter, and the output result is as follows:

# -*- coding:utf-8 -*-
import cv2

#读取图片
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)

#BGR图像
print(img[78, 125, 0])
print(img[78, 125, 1])
print(img[78, 125, 2])

#修改像素
img[78, 125, 0] = 255
img[78, 125, 1] = 255
img[78, 125, 2] =255

print(img[78, 125])
img[78, 125] = [10, 10, 10]
print(img[78, 125, 0])
print(img[78, 125, 1])
print(img[78, 125, 2])
#方法二
print(img[78, 125])
img[78, 125] = [10, 10, 10]
print(img[78, 125])

The output results are shown below, and the B, G, R pixel values ​​are modified to 255 and 0 by two methods, respectively.

155
104
61
255
255
255
[255 255 255]
[10 10 10]

2. Modify the area pixels

The area pixel modification is realized by accessing the position area of ​​the image array. For example, [100:150, 400:500] is to access the area from rows 100 to 150 and columns 400 to 500, and then modify the pixels in this area. The code looks like this:

# -*- coding:utf-8 -*-
import cv2

#读取图片
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)

#BGR图像
img[100:150, 400:500] = [255, 255, 0]

#显示图像
cv2.imshow("Demo", img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

#写入图像
cv2.imwrite("testyxz.jpg", img)

The output result is shown in the figure below, [255, 255, 0] is light blue.

Three.Numpy read pixel method

Using Numpy for pixel reading, the calling method is as follows:

return value = image.item(position parameter)

# -*- coding:utf-8 -*-
import cv2
import numpy

#读取图片
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)

#Numpy读取像素
blue = img.item(78, 100, 0)
green = img.item(78, 100, 1)
red = img.item(78, 100, 2)
print(blue)
print(green)
print(red)

#显示图像
cv2.imshow("Demo", img)

#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()

The output is as follows. Note that the image channel read by OpenCV is BGR, and it can also be converted to RGB for processing.

155
104
61

Four.Numpy modify the pixel method

Use Numpy's itemset function to modify pixels, which is called as follows:

image.itemset(position, new value)

For example: img.itemset((88,99), 255)

# -*- coding:utf-8 -*-
import cv2
import numpy

#读取图片
img = cv2.imread("test.jpg", cv2.IMREAD_UNCHANGED)

#Numpy读取像素
print(img.item(78, 100, 0))
print(img.item(78, 100, 1))
print(img.item(78, 100, 2))
img.itemset((78, 100, 0), 100)
img.itemset((78, 100, 1), 100)
img.itemset((78, 100, 2), 100)
print(img.item(78, 100, 0))
print(img.item(78, 100, 1))
print(img.item(78, 100, 2))

The output is as follows:

155
104
61
100
100
100

You can also output three values ​​of B, G, and R at the same time. The core code is as follows:

print(img[78, 78])
img.itemset((78, 78, 0), 0)
img.itemset((78, 78, 1), 0)
img.itemset((78, 78, 2), 0)
print(img[78, 78])
#[155 104  61]
#[0 0 0]

This article is excerpted from the e-book "From Zero to One • Python Image Processing and Recognition" jointly produced by eastmount X HUAWEI CLOUD developer community.

Click to download the free e-book "From Zero to One • Python Image Processing and Recognition"

 

Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4526289/blog/5516976