i am writing the code to compare the reference image color with actual image color in a specific region based on reference image color tuple red, green,blue component of a color that is used as reference for comparison eg(128,128,128), tolerance how far from the reference color each RGB component may be and still be considered a match(8,8,8) and flatness within the region eg 90% wrt to actual image
1.My first task is to calculate average r,g,b color component of the test region
(1251, 532, 589, 82)
for that i have written below pseudo code which give me ouput as
(217, 15, 28)
import cv2
from PIL import Image
import math
import numpy as np
class CompareColor(object):
''' loop through each pixel and average rgb '''
def __init__(self, imageName):
self.pic = Image.open(imageName)
# load image data
self.imgData = self.pic.load()
pixel_values = list(self.pic.getdata())
print(pixel_values)
def color(self):
r, g, b = 0, 0, 0
count = 0
for x in range(self.pic.size[0]):
for y in range(self.pic.size[1]):
tempr,tempg,tempb = self.imgData[x,y]
r += tempr
g += tempg
b += tempb
count += 1
# calculate averages
return math.ceil(r/count), math.ceil(g/count), math.ceil(b/count)
class ColorTest:
def __init__(self):
self.actualImgPath = "/home/color/youtube.png"
self.includedAreas = (1251, 532, 589, 82)
def findActualRGBComponent(self):
actualImg = cv2.imread(self.actualImgPath)
Y1 = self.includedAreas[1]
Y2 = Y1 + self.includedAreas[3]
X1 = self.includedAreas[0]
X2 = X1 + self.includedAreas[2]
crop_image = actualImg[Y1:Y2, X1:X2].copy()
status = cv2.imwrite('//home//color//crop.png', crop_image)
img_file = '//home//color//crop.png'
pc = CompareColor(img_file)
print('color', pc.color())
if __name__ == "__main__":
colTest = ColorTest()
colTest.findActualRGBComponent()
- My second task is to find the flatness of color which means how flat the color is - whether there is much variation across the area of interest in the range 0-100 where 100 is perfect single color but i am not able to figure out how to implement this logic. Can any one please help me