[英]How to calculate and sort RGB data on OpenCV?
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
我可以处理step1。 如下面的代码。 但是,我不知道如何在步骤2之后编写程序。将数据另存为csv或numpy更好吗?有人知道吗? 请帮我。 如果您向我展示代码,那将非常有帮助。
import cv2
import numpy
im_f = np.array(Image.open('data/image.jpg'), 'f')
print(im[:, :])
最好将数据作为numpy
数组保存在内存中。 另外,如果最终必须将其转换为np.array
则使用cv2.imread
而不是Image.open
读取图像。
对于绘图,可以使用matplotlib
。
这是使用OpenCV
, numpy
和matplotlib
可以实现上述过程的方法。
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进行了测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.