This time, we will introduce the process of outputting the average pixel value RGB of the object to a csv file.
I want to find the average pixel value of an image of an object such as an apple! You can use it at that time. You can read about how this process was built in an article I posted to Qiita earlier. ↓ [[Python] Classify apples and pears from pixel values using a support vector machine (SVM)] [0] [0]:https://qiita.com/ZESSU/items/3083ce23e0405565765d This process is used to perform the pre-processing of the method described in this article, "Acquisition of the average value of the RGB pixel values of each apple and pear image".
The only information required for this process is an image showing the object. However, if the background is included, it cannot be calculated correctly, so use an app such as Paint to trim only the object neatly as shown in the figure below.
Since openCV is used for image processing and pandas is used as a data frame, please install these libraries in advance.
pip install opencv-python
pip install pandas
The code is here. As a caveat, since openCV is used, the output will be in the order of BGR, not RGB.
bgr_csv.py
import cv2
import numpy as np
import pandas as pd
idir = 'input/'
odir = 'output/'
fname='apple'
num_photo=10
bgr = np.zeros((num_photo,3))
for k in range(num_photo):
img = cv2.imread(idir + fname + '_' + str(k+1) + '.jpg') #Start from number 1
print(idir + fname + '_' + str(k+1) + '.jpg')
h, w, c = img.shape #height, width, channnel
#Initialize
l=0
b_ave=0; g_ave=0; r_ave=0
for i in range(h):
for j in range(w):
#Pixel value[0,0,0](Black)を除外してピクセルの和とbgrのPixel valueの合計を計算する
if(img[i,j,0] != 0 or img[i,j,1] != 0 or img[i,j,2] != 0 ):
l+=1 #Calculate the number of pixels of interest
#Calculate the sum of the pixel values of the target pixel
b_ave=b_ave+img[i,j,0]
g_ave=g_ave+img[i,j,1]
r_ave=r_ave+img[i,j,2]
#Obtain the average RGB pixel value by dividing the total pixel value by the number of pixels.
b_ave=b_ave/l
g_ave=g_ave/l
r_ave=r_ave/l
bgr[k]=np.array([b_ave, g_ave, r_ave])
df = pd.DataFrame(bgr, columns=['blue', 'green', 'red']) #Match with opencv order quasi-BGR
df.to_csv(odir + fname + '.csv')
For example ...
fname='apple'
num_photo=1
In the case of, the average value of each pixel value of only one file of "input / apple_1.jpg " is output as "output / apple.csv".
After processing the images of 10 apples, the apple.csv file with the average value of 10 BGR images will be output as shown below.
apple.csv
,blue,green,red
0,39.88469583593901,28.743374377331637,137.23369201906283
1,83.72563703792319,79.59471228615863,164.77884914463453
2,66.8231805177587,74.52501570023027,141.8854929872305
3,55.2837418388098,45.28968211495237,148.4160869099861
4,37.59397951454073,49.82323881039423,137.30237460066527
5,53.68868757437335,50.963264366051206,142.6121454070861
6,51.277953772145956,64.07145371348116,152.98116860260473
7,50.47702848900108,48.37151099891814,124.46714749368914
8,40.35442093843233,52.0682126390019,137.8299091402224
9,48.18758094199441,55.87655919841865,145.6361529548088
As described in the overview, using the apple and pear csv file output by this code ↓ [[Python] Classify apples and pears from pixel values using a support vector machine (SVM)] [1] [1]:https://qiita.com/ZESSU/items/3083ce23e0405565765d I am doing. Please take a look at this article as well!
Recommended Posts