仅适用于块的 HoG 功能
Posted
技术标签:
【中文标题】仅适用于块的 HoG 功能【英文标题】:HoG features for blocks only 【发布时间】:2015-07-01 10:28:11 【问题描述】:我正在尝试仅计算块的 HOG 特征。我探索了opencv/module/gpu/src/
下列出的hog.cpp
。以下是我将代码更改为仅具有块功能的计算机。
void cv::gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format)
CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);
computeBlockHistograms(img);
// give block back
/*
const size_t block_hist_size = getBlockHistogramSize();
Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
Size wins_per_img = numPartsWithin(img.size(), win_size, win_stride);
descriptors.create(wins_per_img.area(), static_cast<int>(blocks_per_win.area() * block_hist_size), CV_32F); */
switch (descr_format)
case DESCR_FORMAT_ROW_BY_ROW:
hog::extract_descrs_by_rows(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
break;
case DESCR_FORMAT_COL_BY_COL:
hog::extract_descrs_by_cols(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
break;
default:
CV_Error(CV_StsBadArg, "Unknown descriptor format");
这里也是computeBlockHistograms
代码。
void cv::gpu::HOGDescriptor::computeBlockHistograms(const GpuMat& img)
computeGradient(img, grad, qangle);
size_t block_hist_size = getBlockHistogramSize();
Size blocks_per_img = numPartsWithin(img.size(), block_size, block_stride);
// block_hists.create(1, block_hist_size * blocks_per_img.area(), CV_32F);
block_hists = getBuffer(1, static_cast<int>(block_hist_size * blocks_per_img.area()), CV_32F, block_hists_buf);
hog::compute_hists(nbins, block_stride.width, block_stride.height, img.rows, img.cols,
grad, qangle, (float)getWinSigma(), block_hists.ptr<float>());
hog::normalize_hists(nbins, block_stride.width, block_stride.height, img.rows, img.cols,
block_hists.ptr<float>(), (float)threshold_L2hys);
编辑:我还包括来自hog.cpp
的getDescriptor函数
void cv::gpu::HOGDescriptor::getDescriptors(const GpuMat& img, Size win_stride, GpuMat& descriptors, int descr_format)
CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);
computeBlockHistograms(img);
const size_t block_hist_size = getBlockHistogramSize();
Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
Size wins_per_img = numPartsWithin(img.size(), win_size, win_stride);
descriptors.create(wins_per_img.area(), static_cast<int>(blocks_per_win.area() * block_hist_size), CV_32F);
switch (descr_format)
case DESCR_FORMAT_ROW_BY_ROW:
hog::extract_descrs_by_rows(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
break;
case DESCR_FORMAT_COL_BY_COL:
hog::extract_descrs_by_cols(win_size.height, win_size.width, block_stride.height, block_stride.width,
win_stride.height, win_stride.width, img.rows, img.cols, block_hists.ptr<float>(), descriptors);
break;
default:
CV_Error(CV_StsBadArg, "Unknown descriptor format");
谁能帮我获得仅限于块的 HOG 功能。 已编辑:我只对计算不同窗口大小的 HOG 特征感兴趣,保持单元格和块的特征相同。
【问题讨论】:
你能举个例子吗?我对 HoG 不太熟悉,但是 afaik 它不计算 FEATURES 而是 DESCRIPTORS。因此,例如,可以从 64x128 像素窗口计算 HoG 描述符。在该窗口内,块由算法评估。您想将 64x128 大小的窗口更改为不同的窗口吗?您想使用相同的块大小吗? (这不会改变描述符元素的大小吗?) 感谢您的回复。实际上,我只想计算块的 HoG 描述符,因为我有三个不同的窗口,所以我只想计算块的 HoG 描述符一次。如果我计算三个不同窗口的 HoG 描述符,我将计算块的 HoG 描述符三次。我希望你现在明白了 啊好的。你必须使用gpu版本还是cpu也可以? 我想使用gpu版本。谢谢 没有,因为我没有使用它,但可能它更容易理解和调试,因此您可以使用它进行测试并找出块保存在哪里以重复使用它们。并且可能 gpu 版本使用类似的机制,因此您可以将您的 cpu 解决方案调整为 gpu,但这只是一个猜测。抱歉,我没有更多时间为您解决这个问题。 【参考方案1】:我修改了以下函数以仅计算块的 HOG 描述符。
void cv::gpu::HOGDescriptor::getDescriptorsBlock(const GpuMat& img, Size win_stride, GpuMat& descriptors, FileStorage fs3, string fileName, double scale, int width, int height, size_t lev)
CV_Assert(win_stride.width % block_stride.width == 0 && win_stride.height % block_stride.height == 0);
size_t block_hist_size = getBlockHistogramSize();
computeBlockHistograms(img);
Size blocks_per_img = numPartsWithin(img.size(), block_size, block_stride);
// Size blocks_per_win = numPartsWithin(win_size, block_size, block_stride);
// Size wins_per_img = numPartsWithin(img.size(), win_size, win_stride);
// copy block_hists from GPU to CPU/
float dest_ptr[block_hist_size * blocks_per_img.area()];
cudaMemcpy( &dest_ptr[0], block_hists.ptr<float>(), block_hist_size *blocks_per_img.area()*sizeof(CV_32F), cudaMemcpyDeviceToHost);
std::cout<<"( "<<width<< " ," << height<< ")"<< std::endl;
std::cout <<lev<< std::endl;
// write to yml file
int level = lev;
fs3<<"Scale"<<scale;
fs3 <<"Level"<<level;
fs3<<"Width"<<width<<"Height"<<height;
fs3 << "features" << "[";
for (unsigned int i = 0; i < (block_hist_size * blocks_per_img.area()) ; i++ )
fs3 << dest_ptr[i];
fs3 << "]";
以下是计算多尺度图像的 HOG 描述符。
void cv::gpu::HOGDescriptor::getDescriptorsMultiScale(const GpuMat& img,
Size win_stride, double scale0, unsigned int count)
CV_Assert(img.type() == CV_8UC1 || img.type() == CV_8UC4);
vector<double> level_scale;
double scale = 1.;
int levels = 0;
for (levels = 0; levels < nlevels; levels++)
level_scale.push_back(scale);
if (cvRound(img.cols/scale) < win_size.width ||
cvRound(img.rows/scale) < win_size.height || scale0 <= 1)
break;
scale *= scale0;
levels = std::max(levels, 1);
level_scale.resize(levels);
image_scales.resize(levels);
// open yml file with image ID
FileStorage fs3;
char fileName[20];
GpuMat descriptors;
sprintf (fileName, "%04d", count);
fs3.open(fileName, FileStorage::WRITE);
for (size_t i = 0; i < level_scale.size(); i++)
scale = level_scale[i];
Size sz(cvRound(img.cols / scale), cvRound(img.rows / scale));
GpuMat smaller_img;
if (sz == img.size())
smaller_img = img;
else
image_scales[i].create(sz, img.type());
switch (img.type())
case CV_8UC1: hog::resize_8UC1(img, image_scales[i]); break;
case CV_8UC4: hog::resize_8UC4(img, image_scales[i]); break;
smaller_img = image_scales[i];
std::cout<<"scale "<<level_scale[i]<<std::endl;
// calculate descriptors for blocks
getDescriptorsBlock( smaller_img, win_stride, descriptors, fs3, fileName, scale, smaller_img.cols, smaller_img.rows, i);
// detect(smaller_img, locations, hit_threshold, win_stride, padding);
// close yml file
fs3.release();
不要忘记在中添加这两个函数的定义 opencv/modules/gpu/include/opencv2/gpu/gpu.hpp
【讨论】:
以上是关于仅适用于块的 HoG 功能的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Python 中使用 OpenCV 3.0 中的 HOG 功能训练 SVM 分类器?