Exemple #1
0
def get_hog(im):
    # 如果图像维数是3维,则转换成灰度图
    if im.ndim == 3:
        image = np.dot(im[..., :3], [0.299, 0.587, 0.144])
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # 图片尺寸
    orientations = 9  # 梯度直方图的数量
    cx, cy = (8, 8)  # 一个单元的像素个数

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx ** 2 + gy ** 2)  # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1),
                            grad_ori, 0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i,
                            temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :, i] = uniform_filter(temp_mag, size=(cx, cy))[int(cx / 2)::cx, int(cy / 2)::cy].T

    return orientation_histogram.ravel()
Exemple #2
0
def hog_feature(img):
    from scipy.ndimage import uniform_filter
    img = rgb2gray(img, 'rgb') if img.ndim == 3 else np.at_least_2d(img)
    R, C = img.shape
    orientations = 9
    cx, cy = (8, 8)
    gx = np.zeros(img.shape)
    gy = np.zeros(img.shape)
    gx[:, :-1] = np.diff(img, n=1, axis=1)
    gy[:-1, :] = np.diff(img, n=1, axis=0)
    gmag = np.sqrt(gx**2 + gy**2)
    gorientation = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90
    nx = R // cx
    ny = C // cy
    orientation_hist = np.zeros((nx, ny, orientations))
    for i in range(orientations):
        temp = np.where(gorientation < 180 / orientations * (i + 1),
                        gorientation, 0)
        temp = np.where(gorientation >= 180 / orientations + i, temp, 0)
        cond2 = temp > 0
        mag = np.where(cond2, gmag, 0)
        orientation_hist[:, :,
                         i] = uniform_filter(mag, size=(cx, cy))[cx // 2::cx,
                                                                 cy // 2::cy].T
    return orientation_hist.ravel()
Exemple #3
0
def hog_feature(im):
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape
    orientations = 9
    cx, cy = (8, 8)

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)
    gy[:-1, :] = np.diff(image, n=1, axis=0)
    grad_mag = np.sqrt(gx**2 + gy**2)
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90

    n_cellsx = int(np.floor(sx / cx))
    n_cellsy = int(np.floor(sy / cy))

    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)

        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx,
                                                        cy))[int(cx / 2)::cx,
                                                             int(cy / 2)::cy].T

    return orientation_histogram.ravel()
def hog_feature(img):
    '''
    Compute Histogram of Gradient (HOG) feature for an image
    Parameters: an input grayscale or rgb image
    Returns: Histogram of Gradient (HOG) feature
    Modified from skimage.feature.hog http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
    Reference : Histograms of Oriented Gradients for Human Detection Navneet Dalal and Bill Triggs, CVPR 2005
    '''
    # convert rgb to grayscale if needed
    img = rgb2gray(img) if img.ndim == 3 else np.at_least_2d(img)

    img_y, img_x = img.shape[0],  img.shape[1]
    orientations = 9 # number of gradient bins
    cx, cy = (8, 8) # pixels per cell

    gx, gy = np.zeros(img.shape), np.zeros(img.shape)
    gx[:, :-1] = np.diff(img, n = 1, axis = 1) # compute gradient on x-direction
    gy[:-1, :] = np.diff(img, n = 1, axis = 0) # compute gradient on y-direction
    grad_mag = np.sqrt(gx ** 2 + gy ** 2) # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90 # gradient orientation

    n_cellsx, n_cellsy = int(np.floor(img_x / cx)), int(np.floor(img_y / cy))
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))

    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori, 0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        temp_mag = np.where( temp_ori > 0, grad_mag, 0)
        orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(cx, cy))[cx//2::cx, cy//2::cy].T  # [4, 12, 20, 28] * [4, 12, 20, 28]
    return orientation_histogram.ravel()
def wavelet_feature(im, widths=[1, 4, 16]):
    """Feature extraction using wavelet transform
		 
		 Reference:
			 Scipy documentation for wavelet transformation:
			 https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.signal.cwt.html
		 
		Parameters:
			im : an input grayscale or rgb image
			widths : widths of the wavelet transformation
			
		Returns:
			cwt_feature: Wavelet Transformed data
		
	"""

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    # apply the wavelet transformation vertically and horizontally
    cwt_hor = [0] * im.shape[0] * len(widths)
    for i in range(im.shape[0]):
        cwt_hor += signal.cwt(image[i, :], signal.ricker, widths).ravel()

    cwt_ver = [0] * im.shape[1] * len(widths)
    for i in range(im.shape[1]):
        cwt_ver += signal.cwt(image[:, i], signal.ricker, widths).ravel()

    cwt_feature = np.concatenate((cwt_hor, cwt_ver))

    return cwt_feature
Exemple #6
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)

        #ME TRYING TO FIX

        #orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(int(cx), int(cy)))[int(cx)/2::int(cx), int(cy)/2::int(cy)].T
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx, cy))[cx // 2::cx,
                                                                 cy // 2::cy].T

    return orientation_histogram.ravel()
Exemple #7
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)
    sx, sy = image.shape  # 图形大小  32x32像素
    orientations = 9  # 梯度直方图bin的数量
    cx, cy = (8, 8)  # 每个cell包含的像素

    gx = np.zeros(image.shape)  # 32x32
    gy = np.zeros(image.shape)  # 32x32
    gx[:, :-1] = np.diff(
        image, n=1, axis=1)  # 计算x轴方向梯度  gx[:,:-1]表示不包括gx最后一列,即gx[:,-1]不变,仍然全是0
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # 计算y轴方向梯度

    grad_mag = np.sqrt(gx**2 + gy**2)  # 梯度大小  32x32
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90  # 梯度方向  32x32

    n_cellsx = int(np.floor(sx / cx))  # x轴方向cell的数量 4
    n_cellsy = int(np.floor(sy / cy))  # y轴方向cell的数量 4

    # 计算完整图形的方向
    orientation_histogram = np.zeros(
        (n_cellsx, n_cellsy, orientations))  # 4x4x9
    for i in range(orientations):
        # np.where相当于C语言中的三目运算符  grad_ori <(180/orientations*(i + 1)) ? grad_ori : 0
        #当i=0时,意味着grad_ori中<0 或>20的元素 对应于temp_ori的是0。意味着只保留 0<=grad_ori<20的元素。
        #直观上,就是0<=grad_ori<20的像素点有投票权。
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)  # 32x32
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)

        # 选择那些方向的大小
        cond2 = temp_ori > 0
        # temp_ori > 0 ? grad_mag : 0
        # 用梯度的大小作为投票的权重
        temp_mag = np.where(cond2, grad_mag, 0)
        #均匀滤波器
        local_mean = uniform_filter(temp_mag, size=(cx, cy))
        orientation_histogram[:, :, i] = local_mean[cx / 2::cx, cy / 2::cy].T
    return orientation_histogram.ravel()
Exemple #8
0
def local_binary_pattern(im, numPoints=24, radius=8):
  from skimage import feature
  # convert rgb to grayscale if needed
  if im.ndim == 3:
    image = rgb2gray(im)
  else:
    image = np.at_least_2d(im)

  desc = LocalBinaryPatterns(numPoints, radius)
  hist = desc.describe(image)
  return hist
def hog_feature(im):
    """计算一幅图像的 梯度直方图(Histogram of Gradient (HOG))
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     参考:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    参数:
      im : 一幅灰度或者RGB图像
      
    返回:
      feat: Histogram of Gradient (HOG) 特征
    
  """

    # 遇到 RGB 彩图转换为灰度图像
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # 图像大小
    orientations = 9  # 梯度的分组数
    cx, cy = (8, 8)  # 每个胞的像素数

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # 计算 x 方向上的梯度
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # 计算 y 方向的梯度
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90  # 梯度方向

    n_cellsx = int(np.floor(sx / cx))  # x 中胞的个数
    n_cellsy = int(np.floor(sy / cy))  # y 中胞的个数
    # 计算方向上积分图像
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # 在该方向上创建新的积分图像 create new integral image for this orientation
        # 将方向隔离在这些范围内 isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # 为这些方向选择大小 select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx, cy))[cx / 2::cx,
                                                                 cy / 2::cy].T

    return orientation_histogram.ravel()
Exemple #10
0
def pca_my(im):
    # convert rgb to grayscale if needed
    #print("in the pca function")
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    #print("the size of the image is :",sx)
    #print("the size of the image is :",sy)
    topNfeat = 90
    meanVals = np.mean(image, axis=0)
    # print 'meanVals', meanVals

    # 每个向量同时都减去 均值
    meanRemoved = image - meanVals
    # print 'meanRemoved=', meanRemoved

    covMat = np.cov(meanRemoved, rowvar=0)

    # eigVals为特征值, eigVects为特征向量
    eigVals, eigVects = np.linalg.eig(np.mat(covMat))
    eigValInd = np.argsort(eigVals)
    # print 'eigValInd1=', eigValInd

    k = eigValPct(eigVals, 0.9)  #要达到方差的百分比percentage,需要前k个向量
    eigValInd = np.argsort(eigVals)  #对特征值eigVals从小到大排序
    eigValInd = eigValInd[:-(k + 1):-1]  #从排好序的特征值,从后往前取k个,这样就实现了特征值的从大到小排列
    redEigVects = eigVects[:, eigValInd]  #返回排序后特征值对应的特征向量redEigVects(主成分)
    lowDDataMat = meanRemoved * redEigVects  #将原始数据投影到主成分上得到新的低维数据lowDDataMat
    reconMat = (lowDDataMat * redEigVects.T) + meanVals  #得到重构数据reconMat

    #   # -1表示倒序,返回topN的特征值[-1 到 -(topNfeat+1) 但是不包括-(topNfeat+1)本身的倒叙]
    # eigValInd = eigValInd[:-(topNfeat+1):-1]
    #   # print 'eigValInd2=', eigValInd
    #   # 重组 eigVects 最大到最小
    # redEigVects = eigVects[:, eigValInd]
    #   # print 'redEigVects=', redEigVects.T
    #   # 将数据转换到新空间
    #   # print "---", shape(meanRemoved), shape(redEigVects)
    # lowDDataMat = meanRemoved * redEigVects
    # reconMat = (lowDDataMat * redEigVects.T) + meanVals
    #reconMat_one=reconMat.reshape(reconMat.shape[0]*reconMat.shape[1])
    reconMat_one = reconMat.reshape(1024, 1)
    #np.resize(a, (2, 3))
    reconMat_test = np.resize(reconMat_one, (1024))
    #print("shape of the reconMat_test is :",reconMat_test.shape)

    #print("shape of the reconMat_one is :",reconMat_one.shape)

    # print("shape of the changed ")

    return reconMat_test
Exemple #11
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image

         Modified from skimage.feature.hog
         http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog

       Reference:
         Histograms of Oriented Gradients for Human Detection
         Navneet Dalal and Bill Triggs, CVPR 2005

      Parameters:
        im : an input grayscale or rgb image

      Returns:
        feat: Histogram of Gradient (HOG) feature

    """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx ** 2 + gy ** 2)  # gradient magnitude
    grad_ori = np.arctan2(gy, (gx + 1e-15)) * \
        (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1),
                            grad_ori, 0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i,
                            temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :, i] = uniform_filter(temp_mag, size=(cx, cy))[
            cx / 2::cx, cy / 2::cy].T

    return orientation_histogram.ravel()
Exemple #12
0
def extract_features(images):
    feature_vec = []
    for im in images:
        # color histogram
        hsv = matplotlib.colors.rgb_to_hsv(im/255)*255
        hist, bin_edges = np.histogram(hsv[:,:,0], bins=np.linspace(0, 255, 10+1), density=True) # normalized
        hist = hist * np.diff(bin_edges)

        # hog descriptors
        im = im[:,:,0] if im.ndim == 3 else np.at_least_2d(im) # grayscale
        h  = feature.hog(im, pixels_per_cell=(8,8), cells_per_block=(4,4))
        feature_vec.append(np.concatenate((h,hist)))
    return np.array(feature_vec)
Exemple #13
0
def lbp_feature(im):
    eps = 1e-7
    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)
    lbp = feature.local_binary_pattern(image, 24, 8, method="uniform")
    (hist, _) = np.histogram(lbp.ravel(),
                             bins=np.arange(0, 24 + 3),
                             range=(0, 24 + 2))
    # normalize the histogram
    hist = hist.astype("float")
    hist /= (hist.sum() + eps)
    return hist
Exemple #14
0
def fft_feature(im):
  """Return the 2D fft of the image
    Parameters:
      rgb : RGB or greyscale image
    Returns:
      feats : feauter vector associate to the  rfft
  
  """
    # convert rgb to grayscale if needed
  if im.ndim == 3:
    image = rgb2gray(im)
  else:
    image = np.at_least_2d(im)

  feats = np.abs(np.fft.rfft2(image))**2
  return feats.ravel()
Exemple #15
0
def fft_feature(im):
  """Return the 2D fft of the image

    Parameters:
      rgb : RGB or greyscale image

    Returns:
      feats : feauter vector associate to the  rfft
  
  """
    # convert rgb to grayscale if needed
  if im.ndim == 3:
    image = rgb2gray(im)
  else:
    image = np.at_least_2d(im)

  feats = np.abs(np.fft.rfft2(image))**2
  return feats.ravel()
Exemple #16
0
 def describe(self, im):
     # convert rgb to grayscale if needed
     # if im.ndim == 3:
     #     image = self.rgb2gray(im)
     # else:
     #     image = np.at_least_2d(im)
     # sx, sy = image.shape # image size
     #
     # gx = np.zeros(image.shape)
     # gy = np.zeros(image.shape)
     # gx[:, :-1] = np.diff(image, n=1, axis=1) # compute gradient on x-direction
     # gy[:-1, :] = np.diff(image, n=1, axis=0) # compute gradient on y-direction
     # grad_mag = np.sqrt(gx ** 2 + gy ** 2) # gradient magnitude
     # grad_ori = np.arctan2(gy, (gx + 1e-15)) * (180 / np.pi) + 90 # gradient orientation
     #
     # n_cellsx = int(np.floor(sx / self.cx))  # number of cells in x
     # n_cellsy = int(np.floor(sy / self.cy))  # number of cells in y
     # # compute orientations integral images
     # orientation_histogram = np.zeros((n_cellsx, n_cellsy, self.orientations))
     # for i in range(self.orientations):
     #     # create new integral image for this orientation
     #     # isolate orientations in this range
     #     temp_ori = np.where(grad_ori < 180 / self.orientations * (i + 1),
     #                 grad_ori, 0)
     #     temp_ori = np.where(grad_ori >= 180 / self.orientations * i,
     #                 temp_ori, 0)
     #     # select magnitudes for those orientations
     #     cond2 = temp_ori > 0
     #     temp_mag = np.where(cond2, grad_mag, 0)
     #     orientation_histogram[:,:,i] = uniform_filter(temp_mag, size=(self.cx, self.cy))[self.cx/2::self.cx, self.cy/2::self.cy]
     #
     # return orientation_histogram.ravel()
     if im.ndim == 3:
         image = self.rgb2gray(im)
     else:
         image = np.at_least_2d(im)
     fd, hog_image = hog(image, orientations = self.orientations, pixels_per_cell=(128, 128),
                 cells_per_block=(self.cx,self.cy), visualise=True)
     return fd
def hog_feature(im):
    #extract hog feature
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx,
                                                        cy))[int(cx / 2)::cx,
                                                             int(cy / 2)::cy].T

    return orientation_histogram.ravel()
Exemple #18
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    ####################################################################################
    # gradient 계산을 위한 변수 초기화
    ####################################################################################
    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)

    ####################################################################################
    # x, y 방향 gradient 계산
    ####################################################################################
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    ####################################################################################
    # cell 갯수 계산
    ####################################################################################
    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y

    # compute orientations integral images
    ####################################################################################
    # cell 단위로 histogram을 생성함
    ####################################################################################
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        ####################################################################################
        # orientation을 20도 단위로 구분함 (0, 20, 40, 60, 80, 100, 120, 140, 160, 180)
        # ex> i==0일 때는 0~20도 구간의 orientation을 구해냄. 0~20 구간을 제외하고 0으로..
        ####################################################################################
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        ####################################################################################
        # 해당 구간의 magnitude를 구함
        ####################################################################################
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        ####################################################################################
        # [cx/2::cx, cy/2::cy]
        #  - cx/2에서 시작하여 cx 단위로 인덱싱
        # ex> l = range(10)
        #     l[::4] => [0, 4, 8]
        #     l[1::4] => [1, 5, 9]
        #
        # uniform_filter
        #  - 평균 값을 취하는 필터 (?)
        ####################################################################################
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx, cy))[cx / 2::cx,
                                                                 cy / 2::cy].T

    return orientation_histogram.ravel()
Exemple #19
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    sx, sy = image.shape  # image size
    #  print 'sx,sy',sx,sy #sx,sy 32 32
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    #diff的含义:out[n] = a[n+1] - a[n]
    #-1第一个是少一列,第二个是少一行,因为做了diff操作
    #计算的是x和y方向梯度,步进是1,比较粗糙
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    #计算梯度大小和方向
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation
    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        #寻找步长在 180/9 之间的角度和梯度
        #相当于 180 / orientations *  =< x < 180 / orientations * (i + 1)
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)
        #大概是统一尺度统计的意思吧
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx, cy))[cx / 2::cx,
                                                                 cy / 2::cy].T

    return orientation_histogram.ravel()
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)
    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    gx[:, :-1] = np.diff(image, n=1, axis=1)  # compute gradient on x-direction
    gy[:-1, :] = np.diff(image, n=1, axis=0)  # compute gradient on y-direction
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    # 32/8 = 4, there are 4 cells in the x and y
    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    # orientation_histogram is used to calculate each cell orientations,
    # so its shape is (4, 4, 9)
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))

    # go through the nine orientations to determine the cell is belong to whinch orientations
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(
            cond2, grad_mag,
            0)  # if con2 is bigger than 0, temp_mag is the grad_mag
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx,
                                                        cy))[int(cx / 2)::cx,
                                                             int(cy / 2)::cy].T

    # ravel() return a contiguous flattened array
    return orientation_histogram.ravel()
Exemple #21
0
def hog_feature(im):
    """Compute Histogram of Gradient (HOG) feature for an image
     提取图像纹理特征
  
       Modified from skimage.feature.hog
       http://pydoc.net/Python/scikits-image/0.4.2/skimage.feature.hog
     
     Reference:
       Histograms of Oriented Gradients for Human Detection
       Navneet Dalal and Bill Triggs, CVPR 2005
     
    Parameters:
      im : an input grayscale or rgb image
      
    Returns:
      feat: Histogram of Gradient (HOG) feature
    
  """

    # convert rgb to grayscale if needed
    # 检验维度,3维图片,转换为灰度图片
    if im.ndim == 3:
        image = rgb2gray(im)
    else:
        image = np.at_least_2d(im)

    # image.shape = 32x32
    sx, sy = image.shape  # image size
    orientations = 9  # number of gradient bins
    cx, cy = (8, 8)  # pixels per cell

    gx = np.zeros(image.shape)
    gy = np.zeros(image.shape)
    # diff,沿轴计算i+1号元素和i元素的差值,n计算次数,默认最后一个轴
    gx[:, :-1] = np.diff(image, n=1,
                         axis=1)  # compute gradient on x-direction(行)
    gy[:-1, :] = np.diff(image, n=1,
                         axis=0)  # compute gradient on y-direction(列)
    grad_mag = np.sqrt(gx**2 + gy**2)  # gradient magnitude(幅度)
    # 对y/x求反正切函数返回一个弧度制的数组,取值在[-pi,pi]之间,32x32
    # y轴在第1位
    grad_ori = np.arctan2(
        gy, (gx + 1e-15)) * (180 / np.pi) + 90  # gradient orientation

    n_cellsx = int(np.floor(sx / cx))  # number of cells in x
    n_cellsy = int(np.floor(sy / cy))  # number of cells in y
    # compute orientations integral images
    orientation_histogram = np.zeros((n_cellsx, n_cellsy, orientations))
    for i in range(orientations):
        # create new integral image for this orientation
        # isolate orientations in this range,过滤噪声
        temp_ori = np.where(grad_ori < 180 / orientations * (i + 1), grad_ori,
                            0)
        temp_ori = np.where(grad_ori >= 180 / orientations * i, temp_ori, 0)
        # select magnitudes for those orientations
        cond2 = temp_ori > 0
        temp_mag = np.where(cond2, grad_mag, 0)  # 32x32
        # [::-1]表示以-1为间隔,4::8表示从第4号索引开始,以8为间隔来索引数组
        # 32x32图片,行轴和列轴以8个像素为间隔均匀滤波
        # 对某个i,[:,:,i].shape=4
        orientation_histogram[:, :,
                              i] = uniform_filter(temp_mag,
                                                  size=(cx,
                                                        cy))[int(cx / 2)::cx,
                                                             int(cy / 2)::cy].T

    return orientation_histogram.ravel()  #扁平化数组,这里扁平化为4x4x9=144个元素