0
votes

Hi I have done edge detection on an image and now I want to compute the average color of the pixels in the image. For that first I need to convert the image into a 10x10 grid where each grid element represent individual block. For each block I need to compute the average color. Is there a way to do that? Any help appreciated. Currently I can draw a grid on the image but I cannot do computations from that.

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('images/0.jpg',0)
edges = cv2.Canny(img,100,200)

plt.subplot(122),plt.imshow(edges,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()
1
You could simply loop over the pixels in each grid cell and compute and average RGB value. But that isn't very efficient and can be quiet slow for larger images.Ahmad Moussa

1 Answers

1
votes

One way is to resize the image using block averaging. To do that one has to compute the new size such that each pixel in the new image represent a 10x10 block of pixels in the original. Then just print out the list of values in the resized image. Those will be the average colors for each 10x10 block.

Input:

enter image description here

import cv2

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

# get shape
h, w, c = img.shape
print (h,w,c)


# compute scale size so that each pixel in the resize image corresponds to 10x10 original pixels
hs = round(h/10)
ws = round(w/10)
print(hs,ws)

# resize image using block averaging
resized = cv2.resize(img, (ws,hs), interpolation = cv2.INTER_AREA)

cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

print(list(resized))


We start with a 250x250 sized image. The new size will be 25x25. The first few values that result are:

[array([[112, 132, 225],
       [109, 132, 225],
       [111, 138, 231],
       [ 85,  69, 173],
       [ 83,  73, 178],
       [ 87,  83, 188],
       [ 93,  96, 204],
       [ 95,  99, 206],
       [ 97, 101, 210],
       [ 97, 101, 209],
       [ 99, 101, 206],
       [ 95,  99, 206],
       [ 97, 101, 208],
       [ 96,  98, 204],
       [ 96,  97, 203],
       [ 94,  89, 190],
       [101, 103, 201],
       [111, 132, 223],
       [107, 131, 224],
       [106, 129, 221],
       [133, 176, 237],
       [106, 117, 197],
       [ 94,  91, 189],
       [ 94,  93, 193],
       [ 93,  92, 193]], dtype=uint8), array([[110, 133, 228],
       [112, 140, 230],
       [105, 130, 227],
       [ 78,  67, 173],
       [ 80,  71, 178],
       [ 84,  80, 189],
       [ 91,  93, 203],
       [ 94,  96, 206],
       [ 95,  96, 209],
       [ 96,  97, 209],
       [ 90,  92, 206],
       [ 92,  93, 203],
       [ 98,  98, 205],
       [ 95,  96, 205],
       [ 92,  93, 205],
       [ 94,  90, 197],
       [ 97,  89, 191],
       [117, 132, 223],
       [110, 133, 225],
       [109, 129, 223],
       [110, 131, 220],
       [140, 185, 236],
       [ 92,  89, 187],
       [ 94,  91, 190],
       [ 72,  40, 118]], dtype=uint8), array([[111, 138, 231],
...