首页 > 解决方案 > 请告诉如何获取整个图像的 rgb 值。我已经打印了每个像素的 rgb 值

问题描述

请告诉如何获取整个图像的 rgb 值。我已经打印了每个像素的 rgb 值

import cv2
# LOAD AN IMAGE USING 'IMREAD'
img = cv2.imread("tiger.jpg")
# DISPLAYg
cv2.imshow("Tiger", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img.shape)
[x,y,z]=img.shape


count=0
for i in range(1,x-1):
    for j in range(1,y-1):
        print(img[i,j]) 
        count=count+1
        print(count)

标签: pythonopencvcomputer-visioncv2

解决方案


你已经有像素了。当您使用阅读图像时

img = cv2.imread('tiger.jpg')

img 包含图像的像素,您无需使用循环来获取它们。你可以通过打印它来检查它。

print(img)

#it gives you something like

[[[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]

 [[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]

 [[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]
  ...
  [132 171 210]
  [135 174 213]
  [137 176 215]]]

img 是 numpy 数组(numpy.ndarray)的类型,您可以通过以下方式检查它:

print(type(img))

推荐阅读