Size winStride = Size(), Size padding = Size(),
const std::vector<Point>& locations = std::vector<Point>()) const;
- virtual bool ocl_compute(InputArray _img, Size win_stride, std::vector<float>& descriptors, int descr_format) const;
-
//with found weights output
CV_WRAP virtual void detect(const Mat& img, CV_OUT std::vector<Point>& foundLocations,
CV_OUT std::vector<double>& weights,
double hitThreshold = 0, Size winStride = Size(),
Size padding = Size(),
const std::vector<Point>& searchLocations=std::vector<Point>()) const;
- //ocl
- virtual bool ocl_detect(InputArray img, std::vector<Point> &hits,
- double hitThreshold = 0, Size winStride = Size()) const;
+
//with result weights output
CV_WRAP virtual void detectMultiScale(InputArray img, CV_OUT std::vector<Rect>& foundLocations,
CV_OUT std::vector<double>& foundWeights, double hitThreshold = 0,
double hitThreshold = 0, Size winStride = Size(),
Size padding = Size(), double scale = 1.05,
double finalThreshold = 2.0, bool useMeanshiftGrouping = false) const;
- //ocl
- virtual bool ocl_detectMultiScale(InputArray img, std::vector<Rect> &found_locations, std::vector<double>& level_scale,
- double hit_threshold, Size winStride, double groupThreshold) const;
CV_WRAP virtual void computeGradient(const Mat& img, CV_OUT Mat& grad, CV_OUT Mat& angleOfs,
Size paddingTL = Size(), Size paddingBR = Size()) const;
CV_PROP bool gammaCorrection;
CV_PROP std::vector<float> svmDetector;
UMat oclSvmDetector;
+ float free_coef;
CV_PROP int nlevels;
- // evaluate specified ROI and return confidence value for each location
- virtual void detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
+ // evaluate specified ROI and return confidence value for each location
+ virtual void detectROI(const cv::Mat& img, const std::vector<cv::Point> &locations,
CV_OUT std::vector<cv::Point>& foundLocations, CV_OUT std::vector<double>& confidences,
double hitThreshold = 0, cv::Size winStride = Size(),
cv::Size padding = Size()) const;
- // evaluate specified ROI and return confidence value for each location in multiple scales
- virtual void detectMultiScaleROI(const cv::Mat& img,
+ // evaluate specified ROI and return confidence value for each location in multiple scales
+ virtual void detectMultiScaleROI(const cv::Mat& img,
CV_OUT std::vector<cv::Rect>& foundLocations,
std::vector<DetectionROI>& locations,
double hitThreshold = 0,
int groupThreshold = 0) const;
- // read/parse Dalal's alt model file
- void readALTModel(String modelfile);
- void groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const;
+ // read/parse Dalal's alt model file
+ void readALTModel(String modelfile);
+ void groupRectangles(std::vector<cv::Rect>& rectList, std::vector<double>& weights, int groupThreshold, double eps) const;
};
void HOGDescriptor::setSVMDetector(InputArray _svmDetector)
{
_svmDetector.getMat().convertTo(svmDetector, CV_32F);
+ CV_Assert(checkDetectorSize());
Mat detector_reordered(1, (int)svmDetector.size(), CV_32FC1);
for (size_t k = 0; k < block_hist_size; ++k)
dst[k] = src[k];
}
+ size_t descriptor_size = getDescriptorSize();
+ free_coef = svmDetector.size() > descriptor_size ? svmDetector[descriptor_size] : 0;
detector_reordered.copyTo(oclSvmDetector);
- CV_Assert(checkDetectorSize());
}
#define CV_TYPE_NAME_HOG_DESCRIPTOR "opencv-object-detector-hog"
return a;
}
+#ifdef HAVE_OPENCL
+
static bool ocl_compute_gradients_8UC1(int height, int width, InputArray _img, float angle_scale,
UMat grad, UMat qangle, bool correct_gamma, int nbins)
{
return k.run(2, globalThreads, localThreads, false);
}
-bool HOGDescriptor::ocl_compute(InputArray _img, Size win_stride, std::vector<float>& _descriptors, int descr_format) const
+static bool ocl_compute(InputArray _img, Size win_stride, std::vector<float>& _descriptors, int descr_format, Size blockSize,
+ Size cellSize, int nbins, Size blockStride, Size winSize, float sigma, bool gammaCorrection, double L2HysThreshold)
{
Size imgSize = _img.size();
Size effect_size = imgSize;
Size wins_per_img = numPartsWithin(imgSize, winSize, win_stride);
UMat labels(1, wins_per_img.area(), CV_8U);
- float sigma = (float)getWinSigma();
float scale = 1.f / (2.f * sigma * sigma);
Mat gaussian_lut(1, 512, CV_32FC1);
int idx = 0;
for(int j=-8; j<8; j++)
gaussian_lut.at<float>(idx++) = (8.f - fabs(j + 0.5f)) * (8.f - fabs(i + 0.5f)) / 64.f;
+ if(!ocl_computeGradient(_img, grad, qangle, nbins, effect_size, gammaCorrection))
+ return false;
+
UMat gauss_w_lut;
gaussian_lut.copyTo(gauss_w_lut);
-
- if(!ocl_computeGradient(_img, grad, qangle, nbins, effect_size, gammaCorrection)) return false;
-
if(!ocl_compute_hists(nbins, blockStride.width, blockStride.height, effect_size.height,
- effect_size.width, grad, qangle, gauss_w_lut, block_hists, block_hist_size)) return false;
+ effect_size.width, grad, qangle, gauss_w_lut, block_hists, block_hist_size))
+ return false;
if(!ocl_normalize_hists(nbins, blockStride.width, blockStride.height, effect_size.height,
- effect_size.width, block_hists, (float)L2HysThreshold)) return false;
+ effect_size.width, block_hists, (float)L2HysThreshold))
+ return false;
Size blocks_per_win = numPartsWithin(winSize, blockSize, blockStride);
wins_per_img = numPartsWithin(effect_size, winSize, win_stride);
case DESCR_FORMAT_ROW_BY_ROW:
if(!ocl_extract_descrs_by_rows(winSize.height, winSize.width,
blockStride.height, blockStride.width, win_stride.height, win_stride.width, effect_size.height,
- effect_size.width, block_hists, descriptors, (int)block_hist_size, descr_size, descr_width)) return false;
+ effect_size.width, block_hists, descriptors, (int)block_hist_size, descr_size, descr_width))
+ return false;
break;
case DESCR_FORMAT_COL_BY_COL:
if(!ocl_extract_descrs_by_cols(winSize.height, winSize.width,
blockStride.height, blockStride.width, win_stride.height, win_stride.width, effect_size.height, effect_size.width,
- block_hists, descriptors, (int)block_hist_size, descr_size, blocks_per_win.width, blocks_per_win.height)) return false;
+ block_hists, descriptors, (int)block_hist_size, descr_size, blocks_per_win.width, blocks_per_win.height))
+ return false;
break;
default:
return false;
descriptors.reshape(1, (int)descriptors.total()).getMat(ACCESS_READ).copyTo(_descriptors);
return true;
}
+#endif //HAVE_OPENCL
void HOGDescriptor::compute(InputArray _img, std::vector<float>& descriptors,
Size winStride, Size padding, const std::vector<Point>& locations) const
Size paddedImgSize(imgSize.width + padding.width*2, imgSize.height + padding.height*2);
CV_OCL_RUN(_img.dims() <= 2 && _img.type() == CV_8UC1 && _img.isUMat(),
- ocl_compute(_img, winStride, descriptors, DESCR_FORMAT_COL_BY_COL))
+ ocl_compute(_img, winStride, descriptors, DESCR_FORMAT_COL_BY_COL, blockSize,
+ cellSize, nbins, blockStride, winSize, (float)getWinSigma(), gammaCorrection, L2HysThreshold))
Mat img = _img.getMat();
HOGCache cache(this, img, padding, padding, nwindows == 0, cacheStride);
Mutex* mtx;
};
+#ifdef HAVE_OPENCL
+
static bool ocl_classify_hists(int win_height, int win_width, int block_stride_y, int block_stride_x,
int win_stride_y, int win_stride_x, int height, int width,
const UMat& block_hists, UMat detector,
return k.run(2, globalThreads, localThreads, false);
}
-bool HOGDescriptor::ocl_detect(InputArray img, std::vector<Point> &hits,
- double hit_threshold, Size win_stride) const
+static bool ocl_detect(InputArray img, std::vector<Point> &hits, double hit_threshold, Size win_stride,
+ const UMat& oclSvmDetector, Size blockSize, Size cellSize, int nbins, Size blockStride, Size winSize,
+ bool gammaCorrection, double L2HysThreshold, float sigma, float free_coef)
{
hits.clear();
- if (svmDetector.empty())
+ if (oclSvmDetector.empty())
return false;
Size imgSize = img.size();
Size wins_per_img = numPartsWithin(imgSize, winSize, win_stride);
UMat labels(1, wins_per_img.area(), CV_8U);
- float sigma = (float)getWinSigma();
float scale = 1.f / (2.f * sigma * sigma);
Mat gaussian_lut(1, 512, CV_32FC1);
int idx = 0;
for(int j=-8; j<8; j++)
gaussian_lut.at<float>(idx++) = (8.f - fabs(j + 0.5f)) * (8.f - fabs(i + 0.5f)) / 64.f;
+ if(!ocl_computeGradient(img, grad, qangle, nbins, effect_size, gammaCorrection))
+ return false;
+
UMat gauss_w_lut;
gaussian_lut.copyTo(gauss_w_lut);
-
- if(!ocl_computeGradient(img, grad, qangle, nbins, effect_size, gammaCorrection)) return false;
-
if(!ocl_compute_hists(nbins, blockStride.width, blockStride.height, effect_size.height,
- effect_size.width, grad, qangle, gauss_w_lut, block_hists, block_hist_size)) return false;
+ effect_size.width, grad, qangle, gauss_w_lut, block_hists, block_hist_size))
+ return false;
if(!ocl_normalize_hists(nbins, blockStride.width, blockStride.height, effect_size.height,
- effect_size.width, block_hists, (float)L2HysThreshold)) return false;
-
- size_t descriptor_size = getDescriptorSize();
- float free_coef = svmDetector.size() > descriptor_size ? svmDetector[descriptor_size] : 0;
+ effect_size.width, block_hists, (float)L2HysThreshold))
+ return false;
Size blocks_per_win = numPartsWithin(winSize, blockSize, blockStride);
if(!ocl_classify_hists(winSize.height, winSize.width, blockStride.height,
blockStride.width, win_stride.height, win_stride.width,
effect_size.height, effect_size.width, block_hists, oclSvmDetector,
- (float)free_coef, (float)hit_threshold, labels, descr_size, (int)block_hist_size)) return false;
+ free_coef, (float)hit_threshold, labels, descr_size, (int)block_hist_size))
+ return false;
Mat labels_host = labels.getMat(ACCESS_READ);
unsigned char *vec = labels_host.ptr();
return true;
}
-bool HOGDescriptor::ocl_detectMultiScale(InputArray _img, std::vector<Rect> &found_locations, std::vector<double>& level_scale,
- double hit_threshold, Size win_stride, double group_threshold) const
+static bool ocl_detectMultiScale(InputArray _img, std::vector<Rect> &found_locations, std::vector<double>& level_scale,
+ double hit_threshold, Size win_stride, double group_threshold,
+ const UMat& oclSvmDetector, Size blockSize, Size cellSize,
+ int nbins, Size blockStride, Size winSize, bool gammaCorrection,
+ double L2HysThreshold, float sigma, float free_coef)
{
std::vector<Rect> all_candidates;
std::vector<Point> locations;
Size effect_size = Size(cvRound(imgSize.width / scale), cvRound(imgSize.height / scale));
if (effect_size == imgSize)
{
- if(!ocl_detect(_img, locations, hit_threshold, win_stride)) return false;
+ if(!ocl_detect(_img, locations, hit_threshold, win_stride, oclSvmDetector, blockSize, cellSize, nbins,
+ blockStride, winSize, gammaCorrection, L2HysThreshold, sigma, free_coef))
+ return false;
}
else
{
resize(_img, image_scale, effect_size);
- if(!ocl_detect(image_scale, locations, hit_threshold, win_stride)) return false;
+ if(!ocl_detect(image_scale, locations, hit_threshold, win_stride, oclSvmDetector, blockSize, cellSize, nbins,
+ blockStride, winSize, gammaCorrection, L2HysThreshold, sigma, free_coef))
+ return false;
}
Size scaled_win_size(cvRound(winSize.width * scale),
cvRound(winSize.height * scale));
cv::groupRectangles(found_locations, (int)group_threshold, 0.2);
return true;
}
+#endif //HAVE_OPENCL
void HOGDescriptor::detectMultiScale(
InputArray _img, std::vector<Rect>& foundLocations, std::vector<double>& foundWeights,
CV_OCL_RUN(_img.dims() <= 2 && _img.type() == CV_8UC1 && scale0 > 1 && winStride.width % blockStride.width == 0 &&
winStride.height % blockStride.height == 0 && padding == Size(0,0) && _img.isUMat(),
- ocl_detectMultiScale(_img, foundLocations, levelScale, hitThreshold, winStride, finalThreshold));
+ ocl_detectMultiScale(_img, foundLocations, levelScale, hitThreshold, winStride, finalThreshold, oclSvmDetector,
+ blockSize, cellSize, nbins, blockStride, winSize, gammaCorrection, L2HysThreshold, (float)getWinSigma(), free_coef));
std::vector<Rect> allCandidates;
std::vector<double> tempScales;