0

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)

enter image description here

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()
  1. 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
user1891916
  • 951
  • 1
  • 8
  • 9
  • Sorry, but your method is fundamentally flawed. Finding the average colour is not going to be very useful. What are you actually trying to do? If you can take a step back and explain that, you'll likely get better advice. Why do you want to find similarity? Between what? For what purpose? Do you know any constraints on your images that could help reduce the problem to something simpler... e.g. max number of colours, palette from which they are chosen, or that they are GIFs with only 32 colours? – Mark Setchell Sep 22 '19 at 10:20
  • This article should be pretty applicable to your case... https://www.pyimagesearch.com/2014/07/14/3-ways-compare-histograms-using-opencv-python/ – Mark Setchell Sep 22 '19 at 10:46
  • I would like 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 (8,8,8) and flatness within the region eg 90% wrt to actual image – user1891916 Sep 22 '19 at 13:36
  • Thank you for the reply to my comment but I am none the wiser. I have no idea what you are really trying to achieve. How does your image relate to your question please? – Mark Setchell Sep 22 '19 at 16:13
  • I would like to compare the area in red color around signin option with actual image for comparison – user1891916 Sep 22 '19 at 17:19
  • You want to compare the red area, of which there are actually two (one saying *"SIGN IN"* and one like a *"Play"* button) for comparison? Sorry, I still don't get it. – Mark Setchell Sep 22 '19 at 17:37

1 Answers1

0

You should check this answer about the dominant colors of the image, because finding an average on that particular picture has no practical meaning, the average color does not exist anywhere on the picture: How to find the average colour of an image in Python with OpenCV?

And once you're done with the dominant colors, you'll easily answer the second question about the flatness or whatever -- because the mean color you've found so far does not exist on the picture, so the flatness can be an arbitrary number for a non-existing color.

lenik
  • 23,228
  • 4
  • 34
  • 43