Python 3.x 如何在python中从非重叠面片大小为32 X 32的图像中提取24维颜色直方图特征向量
我想读取大小为32 X 32的非重叠面片的图像,每个面片应使用R、G、B值表示,8+8+8=24维特征向量 我假设每个面片需要三个8格直方图,每个颜色通道一个。如果您的图像数据是Python 3.x 如何在python中从非重叠面片大小为32 X 32的图像中提取24维颜色直方图特征向量,python-3.x,numpy,image-processing,Python 3.x,Numpy,Image Processing,我想读取大小为32 X 32的非重叠面片的图像,每个面片应使用R、G、B值表示,8+8+8=24维特征向量 我假设每个面片需要三个8格直方图,每个颜色通道一个。如果您的图像数据是uint8,那么我们可以通过右移五位来获得每个像素的适当的bin索引。实际的组织编程可以使用np.bincount+一些温和的技巧来完成,以解决其仅为1D的问题 这本质上是一条单行线。下面的大部分代码用于处理非32维图像 import numpy as np def histo24(img): h, w, c
uint8
,那么我们可以通过右移五位来获得每个像素的适当的bin索引。实际的组织编程可以使用np.bincount
+一些温和的技巧来完成,以解决其仅为1D的问题
这本质上是一条单行线。下面的大部分代码用于处理非32维图像
import numpy as np
def histo24(img):
h, w, c = img.shape
assert c == 3
assert img.dtype == np.uint8
# pad
H, W = (h+31)>>5, (w+31)>>5
patches = np.zeros((H, 32, W, 32, 3), np.uint8)
patches.reshape(H<<5, W<<5, 3)[:h, :w] = img>>5
# the next line is the actual histogramming
histo = np.bincount(
(patches + np.arange(0, H*W*24, 8).reshape(H, 1, W, 1, 3)).ravel(),
minlength=H*W*24).reshape(H, W, 24)
# subtract padded zeros from zero bins at the right and bottom edges
if h & 31:
histo[-1, :, ::8] -= (31&-h)<<5
if w & 31:
histo[:, -1, ::8] -= (31&-w)<<5
if h & 31:
histo[-1, -1, ::8] += (31&-h)*(31&-w)
return histo
将numpy导入为np
def历史记录24(img):
h、 w,c=img.形状
断言c==3
断言img.dtype==np.uint8
#垫
H、 W=(H+31)>>5,(W+31)>>5
补丁=np.0((H,32,W,32,3),np.uint8)
补丁。重塑(hq)问题与机器学习无关
-请不要向标签发送垃圾邮件(已删除)。
def visualize(histo):
h, w, c = histo.shape
assert c == 24
vis = np.zeros((h, 32, w, 32, 3), np.uint8)
idx = np.arange(28)[None, :, None]
for c in range(3):
bnds = (histo[..., c<<3:(c+1)<<3].cumsum(axis=-1)*(28/1024)).astype(np.uint8)[..., ::-1]
for j in range(1, 8):
view = vis[:, 2:-2, :, 7*c+6:7*c+12, c]
view[..., 0][(idx >= bnds[:, None, :, j]) &
(idx < bnds[:, None, :, j - 1])] = (j<<5)|16
view[..., 1:] = view[..., :1]
return vis.reshape(h<<5, w<<5, 3)
from scipy.misc import face
import Image
exmpl = face()
histo = histo24(exmpl)
Image.fromarray(exmpl).show()
#Image.fromarray(exmpl>>5<<5).show()
Image.fromarray(visualize(histo)).show()