【问题标题】:How to calculate and sort RGB data on OpenCV?如何在 OpenCV 上计算和排序 RGB 数据?
【发布时间】:2019-01-28 04:46:00
【问题描述】:

RGB 数据。如何在 Python、OpenCV 上计算和排序

我想在 Python、OpenCV 上工作,这些步骤如下

1. Get the RGB data from pictures
2. Calculate the R*G*B on each pixel of the pictures
3. Sort the data by descending order and plot them on graph or csv
4. Get the max and min and medium of R*G*B

我可以处理第 1 步。如下代码。 但是,我不知道如何在step2之后编写程序 最好将数据保存为 csv 或 numpy 有人有想法吗?请帮我。如果你给我看代码会很有帮助。

import cv2
import numpy


im_f = np.array(Image.open('data/image.jpg'), 'f')
print(im[:, :]) 

【问题讨论】:

  • 对你来说粗略但最简单的方法,因为你正在学习如下:im = cv2.imread(...);b,g,r = cv2.split(im);c = sorted(b*g*r,reverse=True); mx,min=c[-1],c[0]等等等等
  • 请问你为什么要这样做/绘制这个?

标签: python-3.x numpy opencv


【解决方案1】:

最好将内存中的数据保存为numpy 数组。此外,如果最终必须将图像转换为np.array,请使用cv2.imread 而不是Image.open 读取图像。

对于绘图,可以使用matplotlib

以下是使用OpenCVnumpymatplotlib 实现上述过程的方法。

import numpy as np
import cv2, sys
import matplotlib.pyplot as plt

#Read image
im_f = cv2.imread('data/image.jpg')

#Validate image
if im_f is None:
    print('Image Not Found')
    sys.exit();

#Cast to float type to hold the results
im_f = im_f.astype(np.float32)


#Compute the product of channels and flatten the result to get 1D array
product = (im_f[:,:,0] * im_f[:,:,1] * im_f[:,:,2]).flatten()

#Sort the flattened array and flip it to get elements in descending order
product = np.sort(product)[::-1]

#Compute the min, max and median of product
pmin, pmax , pmed = np.amin(product), np.amax(product), np.median(product)

print('Min = ' + str(pmin))
print('Max = ' + str(pmax))
print('Med = ' + str(pmed))

#Show the sorted array
plt.plot(product)
plt.show()

在 Ubuntu 16.04 上使用 Python 3.5.2、OpenCV 4.0.1、numpy 1.15.4 和 matplotlib 3.0.2 进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2014-09-20
    • 2014-08-30
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多