To be more specific, here is the exact requirement. I'm not sure how to word the question. I have an image, of size say (500,500)
. I extract only r and g channels
r = image[:, :, 0]
g = image[:, :, 1]
Then, I compute the 2D histogram of r and g
hist2d = np.histogram2d(r, g, bins=256, range=[(255,255),(255,255)])
Now, hist2d[0].shape
is (256, 256)
since It corresponds to every pair of 256x256 colors. Fine
The main requirement is, in an separate image, called result
with same shape as original image ie (500, 500)
, I want to populate each element of result
with the value of 2d histogram of r
and g
channels
For example, if r[200,200]
is 23 and g[200, 200]
is 26, I want to place result[200, 200] = hist2d[0][23, 26]
The naive method for doing this is, simple python loop.
for i in range(r.shape[0]):
for j in range(r.shape[1]):
result[i, j] = hist2d[0][r[i, j], g[i, j]]
But for a large image, this takes a significant time to compute. Is there a numpy way of doing this?
Thanks
just use hist2d[0][r, g]
:
import numpy as np
r, g, b = np.random.randint(0, 256, size=(3, 500, 500)).astype(np.uint8)
hist2d = np.histogram2d(r.ravel(), g.ravel(), bins=256, range=[[0, 256], [0, 256]])
hist2d[0][r, g]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.