void fft2(Mat_<float> src) { int x = getOptimalDFTSize(2* src.rows ); int y = getOptimalDFTSize(2* src.cols ); copyMakeBorder(src, src, 0, (x - src.rows), 0, (y - src.cols), BORDER_CONSTANT, Scalar::all(0)); // Get padded image size const int wx = src.cols, wy = src.rows; const int cx = wx/2, cy = wy/2; //--------------------------------// // DFT - performing // cv::Mat_<float> imgs[] = {src.clone(), Mat::zeros(src.size(), CV_32F)}; cv::Mat_<cv::Vec2f> img_dft; merge(imgs,2,img_dft); dft(img_dft, img_dft); split(img_dft,imgs); cv::Mat_<float> magnitude, phase; cartToPolar(imgs[0],imgs[1],magnitude,phase); dftshift(magnitude); magnitude = magnitude + 1.0f; log(magnitude,magnitude); normalize(magnitude,magnitude,0,1,CV_MINMAX); namedWindow("img_dft",WINDOW_NORMAL); imshow("img_dft",magnitude); waitKey(0); cout << "out" << endl; }
void opticalFlowMagnitudeAngle(const Mat& flow, Mat& magnitude, Mat& angle) { if (magnitude.rows != flow.rows || magnitude.cols != flow.cols) magnitude.create(flow.rows, flow.cols, CV_32FC1); if (angle.rows != flow.rows || angle.cols != flow.cols) angle.create(flow.rows, flow.cols, CV_32FC1); Mat xy[2]; split(flow, xy); try { cartToPolar(xy[0], xy[1], magnitude, angle, true); } catch (Exception e) { throwError(e); } }
void CvHOGEvaluator::integralHistogram( const Mat &img, std::vector<Mat> &histogram, Mat &norm, int nbins ) const { CV_Assert( img.type() == CV_8U || img.type() == CV_8UC3 ); int x, y, binIdx; Size gradSize( img.size() ); Size histSize( histogram[0].size() ); Mat grad( gradSize, CV_32F ); Mat qangle( gradSize, CV_8U ); AutoBuffer<int> mapbuf( gradSize.width + gradSize.height + 4 ); int* xmap = (int*) mapbuf + 1; int* ymap = xmap + gradSize.width + 2; const int borderType = (int) BORDER_REPLICATE; for ( x = -1; x < gradSize.width + 1; x++ ) xmap[x] = borderInterpolate( x, gradSize.width, borderType ); for ( y = -1; y < gradSize.height + 1; y++ ) ymap[y] = borderInterpolate( y, gradSize.height, borderType ); int width = gradSize.width; AutoBuffer<float> _dbuf( width * 4 ); float* dbuf = _dbuf; Mat Dx( 1, width, CV_32F, dbuf ); Mat Dy( 1, width, CV_32F, dbuf + width ); Mat Mag( 1, width, CV_32F, dbuf + width * 2 ); Mat Angle( 1, width, CV_32F, dbuf + width * 3 ); float angleScale = (float) ( nbins / CV_PI ); for ( y = 0; y < gradSize.height; y++ ) { const uchar* currPtr = img.data + img.step * ymap[y]; const uchar* prevPtr = img.data + img.step * ymap[y - 1]; const uchar* nextPtr = img.data + img.step * ymap[y + 1]; float* gradPtr = (float*) grad.ptr( y ); uchar* qanglePtr = (uchar*) qangle.ptr( y ); for ( x = 0; x < width; x++ ) { dbuf[x] = (float) ( currPtr[xmap[x + 1]] - currPtr[xmap[x - 1]] ); dbuf[width + x] = (float) ( nextPtr[xmap[x]] - prevPtr[xmap[x]] ); } cartToPolar( Dx, Dy, Mag, Angle, false ); for ( x = 0; x < width; x++ ) { float mag = dbuf[x + width * 2]; float angle = dbuf[x + width * 3]; angle = angle * angleScale - 0.5f; int bidx = cvFloor( angle ); angle -= bidx; if( bidx < 0 ) bidx += nbins; else if( bidx >= nbins ) bidx -= nbins; qanglePtr[x] = (uchar) bidx; gradPtr[x] = mag; } } integral( grad, norm, grad.depth() ); float* histBuf; const float* magBuf; const uchar* binsBuf; int binsStep = (int) ( qangle.step / sizeof(uchar) ); int histStep = (int) ( histogram[0].step / sizeof(float) ); int magStep = (int) ( grad.step / sizeof(float) ); for ( binIdx = 0; binIdx < nbins; binIdx++ ) { histBuf = (float*) histogram[binIdx].data; magBuf = (const float*) grad.data; binsBuf = (const uchar*) qangle.data; memset( histBuf, 0, histSize.width * sizeof ( histBuf[0] ) ); histBuf += histStep + 1; for ( y = 0; y < qangle.rows; y++ ) { histBuf[-1] = 0.f; float strSum = 0.f; for ( x = 0; x < qangle.cols; x++ ) { if( binsBuf[x] == binIdx ) strSum += magBuf[x]; histBuf[x] = histBuf[-histStep + x] + strSum; } histBuf += histStep; binsBuf += binsStep; magBuf += magStep; } } }
void computeChannels(InputArray image, vector<Mat>& channels) { Mat_<float> grad; Mat_<float> angles; Mat luv, gray, src; if(image.getMat().channels() > 1) { src = Mat(image.getMat().rows, image.getMat().cols, CV_32FC3); image.getMat().convertTo(src, CV_32FC3, 1./255); cvtColor(src, gray, CV_RGB2GRAY); cvtColor(src, luv, CV_RGB2Luv); } else { src = Mat(image.getMat().rows, image.getMat().cols, CV_32FC1); image.getMat().convertTo(src, CV_32FC1, 1./255); src.copyTo(gray); } Mat_<float> row_der, col_der; Sobel(gray, row_der, CV_32F, 0, 1); Sobel(gray, col_der, CV_32F, 1, 0); cartToPolar(col_der, row_der, grad, angles, true); //magnitude(row_der, col_der, grad); Mat_<Vec6f> hist = Mat_<Vec6f>::zeros(grad.rows, grad.cols); //const float to_deg = 180 / 3.1415926f; for (int row = 0; row < grad.rows; ++row) { for (int col = 0; col < grad.cols; ++col) { //float angle = atan2(row_der(row, col), col_der(row, col)) * to_deg; float angle = angles(row, col); if (angle < 0) angle += 180; int ind = (int)(angle / 30); // If angle == 180, prevent index overflow if (ind == 6) ind = 5; hist(row, col)[ind] = grad(row, col) * 255; } } channels.clear(); if(image.getMat().channels() > 1) { Mat luv_channels[3]; split(luv, luv_channels); for( int i = 0; i < 3; ++i ) channels.push_back(luv_channels[i]); } channels.push_back(grad); vector<Mat> hist_channels; split(hist, hist_channels); for( size_t i = 0; i < hist_channels.size(); ++i ) channels.push_back(hist_channels[i]); }