简体   繁体   English

如何使用 OpenCV、Python 将图像的 RGB 值作为 numpy 数组

[英]How can i get the RGB values of an image as numpy array using OpenCV, Python

I need to RGB values of an image using OpenCV, but when I read an image as below, sometimes it does not return exact values.我需要使用 OpenCV 对图像的 RGB 值,但是当我读取如下图像时,有时它不会返回精确值。

    import cv2
    img = cv2.imread('camel.jpeg')

For example例如

For this picture ( camel.jpg ) everything is correct but for this picture ( result.jpg ) it returns 255 for every pixel.对于这张图片( camel.jpg ),一切都是正确的,但对于这张图片( result.jpg ),它为每个像素返回 255。 But if try to read a specific pixel, I can reach the RGB value of the pixel.但如果尝试读取特定像素,我可以达到像素的 RGB 值。 I tried to use a for loop but the result was not correct, it returned again 255 for every pixel.我尝试使用 for 循环,但结果不正确,它再次为每个像素返回 255。 What should i do?我应该怎么办?

I did this on your camel image in Python/OpenCV to get two pixels.我在 Python/OpenCV 中对您的骆驼图像执行此操作以获得两个像素。 Channel values are not just 0 and 255.通道值不仅仅是 0 和 255。

import cv2
import numpy as np

# load image and get dimensions
img = cv2.imread("camel.jpg")
hh, ww = img.shape[:2]

# get pixel at x=0,y=0
px1 = img[0:1, 0:1]

# get pixel at center of image
hh2 = hh//2
ww2 = ww//2
px2 = img[hh2:hh2+1, ww2:ww2+1]

print(px1)
print(px2)

Results:结果:

[[[0 0 0]]]
[[[129   0 193]]]

But you should be able to just print(img) or use Numpy to crop a region and print it.但是您应该能够只print(img)或使用 Numpy 裁剪区域并打印它。

Perhaps there is something wrong with your camel.jpeg image, but it gets fixed when saving and posting it.也许您的 camel.jpeg 图像有问题,但在保存和发布时它会得到修复。 Try uploading the file you posted and see if it has the same issue.尝试上传您发布的文件,看看它是否有同样的问题。

I used Pillow for this and it worked perfectly fine我为此使用了 Pillow,效果非常好

from PIL import Image
import numpy as np

img = Image.open('camel.jpeg')
img_array = np.array(img)

print(img_array.shape)
print(img_array)

Result结果

(1477, 1200, 3)

[[[105 159 206]
  [106 160 207]
  [106 160 207]
  ...
  [215 215 217]
  [215 215 217]
  [216 216 218]]

 [[105 159 206]
  [106 160 207]
  [106 160 207]
   ...

It works fine and as you can say the shape of img_array also matched with resolution of camel.jpeg它工作正常,你可以说img_array的形状也与camel.jpeg的分辨率相匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Opencv 2.4 将 python numpy 数组转换为 RGB 图像? - How to convert a python numpy array to an RGB image with Opencv 2.4? 如何使用 opencv 从图像轮廓内部获取 rgb 颜色值? - How can i get the rgb color values from inside of a contour in image using opencv? 如何使用opencv python在两个变量中获取图像中两条单独行的RGB值 - How to get RGB values of two separate lines in an image in two variables using opencv python 如何用图像的中值有效填充 RGB numpy 数组? - How can I efficently pad an RGB numpy array with the median of the image? 如何使用 numpy.pad 用 RGB 值填充 RGB 图像 - How to pad a RGB image with RGB values using numpy.pad 如何对 python 图像的每个 PIXEL(不是每个 rgb 组件)应用操作(使用 numpy?opencv 或 PIL)? - How to apply an operation to every PIXEL (not every rgb component!) of a python image (either using numpy, opencv or PIL)? 在Python中将RGB十六进制值数组转换为OpenCV图像 - Convert an array of RGB hexadecimal values to OpenCV image in Python 使用 numpy 从图像数组的 RGB 值中获取 (x,y) 坐标值 - Get the (x,y) coordinate values from an image array's RGB value using numpy 使用Numpy数组的Opencv Python裁剪图像 - Opencv Python Crop Image Using Numpy Array Python - 使用 OpenCV 将字节图像转换为 NumPy 数组 - Python - byte image to NumPy array using OpenCV
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM