Как рассчитать и отсортировать данные RGB на OpenCV? - PullRequest
0 голосов
/ 28 января 2019

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.как ниже код.Однако я не знаю, как написать программу после шага 2. Лучше сохранить данные как csv или numpy. У кого-нибудь есть идея?Пожалуйста, помогите мне.было бы очень полезно, если бы вы показали мне код.

import cv2
import numpy


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

1 Ответ

0 голосов
/ 28 января 2019

Лучше хранить данные в памяти как массив numpy.Кроме того, считайте изображение, используя cv2.imread, а не Image.open, если в конечном итоге его необходимо преобразовать в np.array.

Для построения графика можно использовать 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()

Протестировано с Python 3.5.2, OpenCV 4.0.1,numpy 1.15.4 и matplotlib 3.0.2 в Ubuntu 16.04.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...