英文:
skimage hog returns no feature vector with length 0
问题
我正在尝试使用skimage.feature中的实现来提取给定图像(2D)上某些感兴趣区域(ROI)的HOG描述符。我已经附上了我提取的ROI的一些示例。我只是使用了具有所有默认值的HOG描述符。
配置:python:3.9.13; scikit-image: 0.19.2; Windows 10
返回的特征向量为空,即它是一个空数组([])。
注意:我想要使用正样本和负样本的HOG描述符来训练线性SVM模型。
重现问题的代码
from skimage.feature import hog
import cv2 as cv
img = cv.imread(<图像路径>, cv.IMREAD_GRAYSCALE)
desc = hog(img)
print(desc)
#输出:array([], dtype=float64)
特征描述符不应为空。即使我提供一个完全空白的白色图像,也应该有一个描述符。但是,对于正样本,我也没有得到描述符。
是否有人可以帮助解决问题或者这是否是实现的问题?
英文:
I am trying to extract hog descriptors for some ROI on a given image (2D) using the implementation in skimage.feature. I have attached some samples of the ROI that I have extracted. I am just using the hog descriptor with all the default values.
Configuration: python:3.9.13; scikit-image: 0.19.2; Windows 10
The feature vector that is returned has nothing i.e. it is a null array ([]).
NOTE: I want to use the HOG descriptors from positive and negative sample to train and Linear SVM model.
Sample ROI
positive sample
negative sample
Code to reproduce problem
from skimage.feature import hog
import cv2 as cv
img = cv.imread(<path to image>, cv.IMREAD_GRAYSCALE)
desc = hog(img)
print(desc)
#output: array([], dtype=float64)
The feature descriptor should not be null. Even if I am giving a completely blank white image there should be a descriptor. But again I am not getting a descriptor for the positive samples as well.
Can someone please help with what is going wrong or is it an issue with the implementation?
答案1
得分: 1
我参考了源代码
它似乎会生成n_blocks_row
xn_blocks_col
个元素的输出,具体代码如下:
s_row, s_col = image.shape[:2]
c_row, c_col = pixels_per_cell # 默认情况下为 8x8
b_row, b_col = cells_per_block # 默认情况下为 3x3
n_cells_row = int(s_row // c_row) # 沿着行轴的单元格数
n_cells_col = int(s_col // c_col) # 沿着列轴的单元格数
n_blocks_row = (n_cells_row - b_row) + 1
n_blocks_col = (n_cells_col - b_col) + 1
由于图像只有16行,垂直方向上只有两个单元格,这不足以填满一个3x3单元格的块。所以,n_blocks_row
是0,你会得到一个0x22的块数组。
对于这样小的图像,你必须改变pixels_per_cell
和/或cells_per_block
参数,以便pixels_per_cell[0] * cells_per_block[0]
不大于你的图像高度(类似地,对于图像宽度也是如此)。
例如,这对我来说有效:
skimage.feature.hog(img, cells_per_block=(2,2))
尽管我不知道这是否会产生一个有用的输出。减少每个块的单元格可能不会给你一个好的结果。也许你可以在使它们不那么高的同时使它们更宽,以保持它们的大小不变?类似cells_per_block=(2,5)
这样的情况?我在HOG方面没有足够的经验来提供建议。
英文:
I referenced the source code
It looks like it produces output for n_blocks_row
xn_blocks_col
elements, with:
s_row, s_col = image.shape[:2]
c_row, c_col = pixels_per_cell # input parameter, 8x8 by default
b_row, b_col = cells_per_block # input parameter, 3x3 by default
n_cells_row = int(s_row // c_row) # number of cells along row-axis
n_cells_col = int(s_col // c_col) # number of cells along col-axis
n_blocks_row = (n_cells_row - b_row) + 1
n_blocks_col = (n_cells_col - b_col) + 1
With the image having only 16 rows, you get two cells vertically, which is not enough to fill a single block of 3x3 cells. So, n_blocks_row
is 0, and you get as output a 0x22 array of blocks.
For such small images, you must change the pixels_per_cell
and/or the cells_per_block
parameters so that pixels_per_cell[0] * cells_per_block[0]
is not larger than your image height (and similarly for the image width).
For example, this worked for me:
skimage.feature.hog(img, cells_per_block=(2,2))
though I don't know if this produces a useful output or not. Having fewer cells per block might not give you as good a result. Maybe you could make the blocks wider as you make them less tall, so their size remains the same? Something like cells_per_block=(2,5)
? I don't have enough experience with HOG to advice on this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论