2

I have an image with gridline like this and I want to measure each monochrome area with unit squares made from that gridline. The squares that mix with different colours shall not be counted. In the image there are 3 colors: yellow, orange and blue. Borders and gridlines does not count. The original images don't have gridlines.

Full Image Here

!Image here, I'll fix this when I have 10 reputation closeup

So far I can count the number of black and white pixels in the image but I don't know how to customize the code to suit my needs.

Ooker
  • 1,969
  • 4
  • 28
  • 58
Loc Nguyen
  • 21
  • 2
  • monochrome means "no color" (or one hue, shades of it). what do you mean? – Christoph Rackwitz Jun 29 '23 at 15:39
  • sounds like you want to downsample that source image (ignoring the grid), while maintaining the color palette, or applying a palette/posterization effect? – Christoph Rackwitz Jun 29 '23 at 15:40
  • @ChristophRackwitz so for example, in the image there are 3 colors: yellow, orange and blue. Borders and gridlines does not count. We also have original images that don't have gridlines. Does that answer you? – Ooker Jun 29 '23 at 15:47

2 Answers2

0

You can use np.unique to count the occurance of all colors in the img:

import cv2
import numpy as np
  
# reading the image data from desired directory
img = cv2.imread("1OKN8m.jpg")
cv2.imshow('Image',img)

img_1d = img.reshape(img.shape[0]*img.shape[1], img.shape[2])
unique_colors, counts = np.unique(img_1d, axis=0, return_counts=True)
print(f'Unique colors: {unique_colors}')
print(f'Counts: {counts}')

because it is a jpeg, the "monochrome" areas contain a lot of different colors, so you should select all the unique colors that are close enough according to you and count up their individual counts.

Leon Klute
  • 126
  • 6
  • it seems like this only count via the pixels? Is there a way to count via the squares made from the gridline? – Ooker Jun 29 '23 at 14:35
  • `print(img.shape[0]*img.shape[1], img_1d.size)` returns `4584300 13752900`. `print(unique_colors.size, counts.size)` returns `481008 160336`. Shouldn't the numbers in each pair be the same? – Ooker Jun 29 '23 at 16:30
  • `size` seems to count the total number of element, not the size of the first dimension, the numbers are 3x for this rgb image. – Leon Klute Jul 03 '23 at 07:55
  • To count the squares you could downsize the image, but you have to count the size of the gridline, I estimated 85 byt 115: ` img = cv2.resize(img, (85, 115), interpolation=cv2.INTER_AREA) ` – Leon Klute Jul 03 '23 at 07:56
0

I think the way to go is to split image into multiple grids, then check individual parts if they are monochrome or not.

Ooker
  • 1,969
  • 4
  • 28
  • 58