简体   繁体   English

如何在OpenCV上计算和排序RGB数据?

[英]How to calculate and sort RGB data on OpenCV?

RGB data. RGB数据。 How to calculate and sort them on Python, OpenCV 如何在Python,OpenCV上对其进行计算和排序

I want to work on Python, OpenCV these below steps 我想在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

I could handle that the step1. 我可以处理step1。 as below code. 如下面的代码。 However, I don't know how to write a program after step2 It's better to save the data as csv or numpy Does anybody have an idea? 但是,我不知道如何在步骤2之后编写程序。将数据另存为csv或numpy更好吗?有人知道吗? Please help me. 请帮我。 it would be very helpful if you show me the code. 如果您向我展示代码,那将非常有帮助。

import cv2
import numpy


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

It is better to keep data in-memory as numpy array. 最好将数据作为numpy数组保存在内存中。 Also, read image using cv2.imread rather than Image.open if it has to be converted to np.array eventually. 另外,如果最终必须将其转换为np.array则使用cv2.imread而不是Image.open读取图像。

For plotting, matplotlib can be used. 对于绘图,可以使用matplotlib

Here is how the above mentioned process can be achieved using OpenCV , numpy and 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()

Tested with Python 3.5.2, OpenCV 4.0.1, numpy 1.15.4, and matplotlib 3.0.2 on Ubuntu 16.04. 在Ubuntu 16.04上使用Python 3.5.2,OpenCV 4.0.1,numpy 1.15.4和matplotlib 3.0.2进行了测试。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM