Color Invariants usage with Python OpenCV

Viewed 77

I'm trying to find the color invariants of a photo. I searched it and found that I need to find the value of c1 as its simplest feature. We can find c1 by this formula c1=(r/max(g,b)).

When I try to find img cols and rows with img.shape and split this image b,g,r.

import cv2
import math
import matplotlib.pyplot as plt

img = cv2.imread('CU.png')

b,g,r = cv2.split(img)

cv2.imshow('Image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(img.shape)
rows,cols,channels = img.shape
for i in range(rows):
    for j in range(cols):
        b = img[i,j,0]
        g = img[i,j,1]
        r = img[i,j,2]
        x = (r/max(g,b))
        c1= math.atan(x)
        plt.plot(i,j,c1)

plt.show()

I thought I should split the picture into rows and columns and then break it down into b, g, r. I had to find c1 using the values b, g, r in each row and column, but I'll do this c1 later.

I wonder if anyone knows about it? How to find color constants or what should I do or is my method of finding this c1 correct?

1 Answers

Due to Th. Gevers, J. van de Weijer, H. Stokman, Color Feature Detection article, the color invariants are detecting with these formulas. And the C1,c2,c3 values can be detected like in the attached image. But if we implement the solution like your formula, we can use:

import PIL
import math

def invariant_r(img):
  c1 = np.zeros(32,32)
  for i in range(0, 32):
    for j in range(0, 32):
      r, g, b = img.getpixel((i, j))
      x = (r/max(g,b))
      c1[i][j]= math.atan(x)
  return c1

def invariant_g(img):
  c1 = np.zeros(32,32)
  for i in range(0, 32):
    for j in range(0, 32):
      r, g, b = img.getpixel((i, j))
      x = (g/max(r,b))
      c1[i][j]= math.atan(x)
  return c1

def invariant_b(img):
  c1 = np.zeros(32,32)
  for i in range(0, 32):
    for j in range(0, 32):
      r, g, b = img.getpixel((i, j))
      b = (b/max(r,g))
      c1[i][j]= math.atan(x)
  return c1
Related