Skip to content

Instantly share code, notes, and snippets.

@berak
Last active August 29, 2015 14:23
Show Gist options
  • Select an option

  • Save berak/9a556537dccea2709712 to your computer and use it in GitHub Desktop.

Select an option

Save berak/9a556537dccea2709712 to your computer and use it in GitHub Desktop.
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
#ifndef CV_SQR
# define CV_SQR(x) ((x)*(x))
#endif
#ifndef CV_INIT_VECTOR
# define CV_INIT_VECTOR(vname, type, ...) \
static const type vname##_a[] = __VA_ARGS__; \
std::vector <type> vname(vname##_a, \
vname##_a + sizeof(vname##_a) / sizeof(*vname##_a))
#endif
static cv::Mat imsmooth(const cv::Mat &src, const int rad)
{
if (rad == 0)
return src;
else
{
const float p = 12.0f/rad/(rad + 2) - 2;
cv::Mat dst;
if (rad <= 1)
{
CV_INIT_VECTOR(kernelXY, float, {1/(p + 2), p/(p + 2), 1/(p + 2)});
cv::sepFilter2D(src, dst, -1, kernelXY, kernelXY);
}
else
{
float nrml = CV_SQR(rad + 1.0f);
std::vector <float> kernelXY(2*rad + 1);
for (int i = 0; i <= rad; ++i)
{
kernelXY[2*rad - i] = (i + 1) / nrml;
kernelXY[i] = (i + 1) / nrml;
}
sepFilter2D(src, dst, -1, kernelXY, kernelXY);
}
return dst;
}
}
static void gradientHist(const cv::Mat &src, cv::Mat &magnitude, cv::Mat &histogram,
const int nBins, const int pSize, const int gnrmRad)
{
cv::Mat phase, Dx, Dy;
magnitude.create( src.size(), cv::DataType<float>::type );
phase.create( src.size(), cv::DataType<float>::type );
// changing to ceil will make it work !
//histogram.create( cv::Size( cvCeil(src.size().width/float(pSize)),
// cvCeil(src.size().height/float(pSize)) ),
histogram.create( cv::Size( cvRound(src.size().width/float(pSize)),
cvRound(src.size().height/float(pSize)) ),
CV_MAKETYPE(cv::DataType<float>::type, nBins) );
CV_Assert(phase.cols>=histogram.cols); /// added just to show, that both rows and cols are overflowing
histogram.setTo(0);
cv::Sobel( src, Dx, cv::DataType<float>::type,
1, 0, 1, 1.0, 0.0, cv::BORDER_REFLECT );
cv::Sobel( src, Dy, cv::DataType<float>::type,
0, 1, 1, 1.0, 0.0, cv::BORDER_REFLECT );
int nchannels = src.channels();
for (int i = 0; i < src.rows; ++i)
{
const float *pDx = Dx.ptr<float>(i);
const float *pDy = Dy.ptr<float>(i);
float *pMagnitude = magnitude.ptr<float>(i);
float *pPhase = phase.ptr<float>(i);
for (int j = 0; j < src.cols*nchannels; j += nchannels)
{
float fMagn = float(-1e-5), fdx = 0, fdy = 0;
for (int k = 0; k < nchannels; ++k)
{
float cMagn = CV_SQR( pDx[j + k] ) + CV_SQR( pDy[j + k] );
if (cMagn > fMagn)
{
fMagn = cMagn;
fdx = pDx[j + k];
fdy = pDy[j + k];
}
}
pMagnitude[j/nchannels] = sqrtf(fMagn);
float angle = cv::fastAtan2(fdy, fdx) / 180.0f - 1.0f * (fdy < 0);
if (std::fabs(fdx) + std::fabs(fdy) < 1e-5)
angle = 0.5f;
pPhase[j/nchannels] = angle;
}
}
magnitude /= imsmooth( magnitude, gnrmRad )
+ 0.01*cv::Mat::ones( magnitude.size(), magnitude.type() );
for (int i = 0; i < phase.rows; ++i)
{
const float *pPhase = phase.ptr<float>(i);
const float *pMagn = magnitude.ptr<float>(i);
float *pHist = histogram.ptr<float>(i/pSize);
for (int j = 0; j < phase.cols; ++j)
pHist[cvRound((j/pSize + pPhase[j])*nBins)] += pMagn[j] / CV_SQR(pSize);
}
}
int main( int argc, const char** argv )
{
Mat src = Mat(400,400,CV_8U); // content does not matter
for (int i=0; i<100; i++)
{
Mat rez,hist,mag;
Size siz(400,400+i);
cerr << i << " " << siz << endl;
try
{
resize(src,rez,siz);
gradientHist(rez,mag,hist,8,2,8);
} catch(cv::Exception &e){}
}
return 0;
}
@berak
Copy link
Copy Markdown
Author

berak commented Jun 19, 2015


0 [400 x 400]
1 [400 x 401]
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (u
nsigned)size.p[0])) in cv::Mat::ptr, file E:\code\opencv\build\include\opencv2/c
ore/mat.inl.hpp, line 774
2 [400 x 402]
3 [400 x 403]
4 [400 x 404]
5 [400 x 405]
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (u
nsigned)size.p[0])) in cv::Mat::ptr, file E:\code\opencv\build\include\opencv2/c
ore/mat.inl.hpp, line 774
6 [400 x 406]
7 [400 x 407]
8 [400 x 408]
9 [400 x 409]
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (u
nsigned)size.p[0])) in cv::Mat::ptr, file E:\code\opencv\build\include\opencv2/c
ore/mat.inl.hpp, line 774
10 [400 x 410]
11 [400 x 411]
12 [400 x 412]
13 [400 x 413]
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (u
nsigned)size.p[0])) in cv::Mat::ptr, file E:\code\opencv\build\include\opencv2/c
ore/mat.inl.hpp, line 774
14 [400 x 414]
15 [400 x 415]
16 [400 x 416]
17 [400 x 417]
OpenCV Error: Assertion failed (y == 0 || (data && dims >= 1 && (unsigned)y < (u
nsigned)size.p[0])) in cv::Mat::ptr, file E:\code\opencv\build\include\opencv2/c
ore/mat.inl.hpp, line 774

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment