Hi Team,
How to compute most used color in a image using pytorch .
Hi Team,
How to compute most used color in a image using pytorch .
Your question asks for average RGB value, but in the description you asked for the most used color. The following is for most used color
import matplotlib.pyplot as plt
a = torch.LongTensor(plt.imread(r"temp.jpg"))
b = a[:,:, 0]* 1000000 + a[:,:,1]*1000 + a[:,:,2]
results = torch.unique(b.view(-1), return_counts=True)
results = results[0][torch.argmax(results[1])]
r = int(results/1000000)
g = int((results/1000.) - int(r*1000.) )
b = results - r*1000000 - g*1000
torch.FloatTensor([r,g,b])
The above is a very dirty implementation of a groupby. Torch does not have a string data type so you have to convert it to a scalar through some other trick
Hello anand,
Thanks for Answering ,
wanted to check why you have multiplied by —
“b = a[:,:, 0]* 1000000 + a[:,:,1]*1000 + a[:,:,2]”
Also after uploading a image in png format
it is giving result as -
tensor([0., 0., 0.]).