1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| import cv2
import numpy as np ''' 在提取图像中的像素值时,首先需要将图像转化为数组,对数组进行提取,因此需要用到numpy库。 '''
img = cv2.imread("Typhoon Chanthu image09182021_500m.jpeg") cv2.imshow("Color 2", img)
def get_red(img): redImg = img[:, :, 2] return redImg
def get_green(img): greenImg = img[:, :, 1] return greenImg
def get_blue(img): blueImg = img[:, :, 0] return blueImg
b = get_blue(img) g = get_green(img) r = get_red(img)
cv2.imshow("Blue 2", b) cv2.imshow("Green 2", g) cv2.imshow("Red 2", r)
cv2.imwrite("typtoon2_r.jpg", r) cv2.imwrite("typtoon2_g.jpg", g) cv2.imwrite("typtoon2_b.jpg", b)
cv2.waitKey()
cv2.destroyAllWindows()
print("图像形状大小:") print(img.shape) print("图像像素数目:") print(img.size) print("图像数据类型:") print(img.dtype)
img_array = np.array(img)
print("像素点(0,0)的R通道值为:") print(img_array[0, 0, 2]) print("像素点(0,0)的G通道值为:") print(img_array[0, 0, 1]) print("像素点(0,0)的B通道值为:") print(img_array[0, 0, 0])
print("像素点(838,757)的R通道值为:") print(img_array[838, 757, 2]) print("像素点(838,757)的G通道值为:") print(img_array[838, 757, 1]) print("像素点(838,757)的B通道值为:") print(img_array[838, 757, 0])
print("像素点(726,1287)的R通道值为:") print(img_array[726, 1287, 2]) print("像素点(726,1287)的G通道值为:") print(img_array[726, 1287, 1]) print("像素点(726,1287)的B通道值为:") print(img_array[726, 1287, 0])
print("像素点(2096,1870)的R通道值为:") print(img_array[2096, 1870, 2]) print("像素点(2096,1870)的G通道值为:") print(img_array[2096, 1870, 1]) print("像素点(2096,1870)的B通道值为:") print(img_array[2096, 1870, 0])
print("像素点(2168,2014)的R通道值为:") print(img_array[2168, 2014, 2]) print("像素点(2168,2014)的G通道值为:") print(img_array[2168, 2014, 1]) print("像素点(2168,2014)的B通道值为:") print(img_array[2168, 2014, 0])
|