I wrote a code that gives me the average RGB value of an image. Now I want besides the RGB value, also a LAB value. I found a code to do the conversion, but when I run the code, it only gives me the last value.
So with this code I receive the average RGB and place it in a dataframe:
import cv2 as cv
import glob
import numpy as np
from skimage import io
import pandas as pd
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
path = "image.jpg"
img_number = 1
for file in glob.glob(path):
print(file)
img = cv.imread(file)
scale_percent = 60
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
imgr = cv.resize(img, dim, interpolation=cv.INTER_AREA)
hsv = cv.cvtColor(imgr, cv.COLOR_BGR2HSV)
blur0 = cv.medianBlur(hsv, 11)
low_yellow = np.array([10, 42, 220])
high_yellow = np.array([30, 255, 255])
mask = cv.inRange(blur0, low_yellow, high_yellow)
res = cv.bitwise_and(imgr, imgr, mask=mask)
cv.imwrite("image"+str(img_number)+".jpg", res)
img_number +=1
path1 = "Image/*.jpg"
img_number = 1
result_df = pd.DataFrame()
for file in glob.glob(path1):
image = io.imread(file)
x = image[np.all(image != 0, axis=2)].mean(axis=0)
result_df = pd.concat((result_df, pd.DataFrame(x)), axis=1)
df_t = result_df.T
df_lab = rgb_to_cielab(df_t)
df_t.columns = ['R', 'G', 'B']
df_t.loc['Mean'] = df_t.mean()
df = df_t.round(decimals=1)
df.to_excel("Excel.xlsx")
When I want to converse my RGB value to LAB I found this code to do the conversion:
def rgb_to_cielab(a):
"""
a is a pixel with RGB coloring
"""
a1,a2,a3 = a/255
color1_rgb = sRGBColor(a1, a2, a3);
color1_lab = convert_color(color1_rgb, LabColor);
return color1_lab
When I run this code it provides me only the last value. I quite sure I am creating a loop, but I don't know how to fix it. Can someone help me with this?
I know my title is a bit different as my real question but maybe someone knows a easier way to get the LAB values?