Quantcast
Channel: OpenCV Q&A Forum - Latest question feed
Viewing all articles
Browse latest Browse all 19555

Malloc Error while pushing back Keypoint Vector

$
0
0
I got an Malloc Error while calculating a Descriptor for feature Candidates that I find with FAST. After the Detection I try to calculate the Descriptor with parts from SIFT. The Code I am using is this: const int n = SIFT_ORI_HIST_BINS; float hist [n]; vector> candidates2d; vector> features2d; vector candidates; vector features; vector Pyr; String imgName("/users/mtuchner/desktop/test/test/example.jpg"); bool gotKeypoint = false; bas = imread(imgName, -1); int threshold = 55; int substract = 16; int l = 0; cvtColor(bas, src, CV_BGR2GRAY); double factor = sqrt(2); int k = 1; do{ rows = src.rows/factor; cols = src.cols/factor; resize(src, tmp, Size(rows, cols),0,0,INTER_NEAREST); //!!! Malloc Error Here !!! GaussianBlur(tmp, dst, Size(5,5),0,0); // !!!Malloc Error here!!! FAST(dst, candidates, threshold, true); if(candidates.size() > 100){ for(int i = 0; i < candidates.size(); i++){ if(candidates[i].pt.x > 7 && candidates[i].pt.y > 7){ KeyPoint kpt = candidates[i]; int px = kpt.pt.x -7; int py = kpt.pt.y -7; if(px + 15 >= src.cols || py + 15 >= src.rows) continue; patch = src(Rect(px,py,15,15)); Point centre(7,7); float scl_octv = kpt.size * 0.5f / (1 << 1); float omax = calcOrientationHist(patch, centre, cvRound(SIFT_ORI_RADIUS * scl_octv), SIFT_ORI_SIG_FCTR * scl_octv, hist, n); float mag_thr = (float)(omax * SIFT_ORI_PEAK_RATIO); for(int j = 0; j < n; j++){ int l = j > 0 ? j - 1 : n -1; int r2 = j < n-1 ? j+1 : 0; if( hist[j] > hist[l] && hist[j] > hist[r2] && hist[j] >= mag_thr ) { float bin = j + 0.5f * ( hist[i] - hist[r2] ) / ( hist[l] - 2 * hist[j] + hist[r2]); bin = bin < 0 ? n +bin : bin >= n ? bin - n : bin; kpt.angle = 360.f - (float)((360.f/n) * bin); if(std::abs(kpt.angle - 360.f) < FLT_EPSILON) kpt.angle = 0.f; features.push_back(kpt); //!!! Malloc Error here!!! gotKeypoint = true; } } if(gotKeypoint){ for(int i = 0; i < patch.rows; i += 5 ){ for(int j = 0; j < patch.cols; j+= 5){ patch5_5 = patch(Rect(j,i,5,5)); gotKeypoint = false; } } } } } k++; Pyr.push_back(src); src = dst.clone(); if(threshold > 0){ threshold -= substract; if(threshold < 0){ threshold = 0; } if(substract > 0){ substract -=6; if(substract <= 0){ substract = 5 + l; l++; } } } features2d.push_back(features); features.clear(); candidates2d.push_back(candidates); // !!!Malloc Error thrown Here!!! } }while(candidates.size() > 100); I push the candidates vector in another Vector so I can reference to it with the right Image in the Pyr vector. The Threshold is set and varys with each iteration so I get ~150 Keypoints with FAST for each step. The Error is thrown when I push_back candidates into the candidates2D vector. The Function calcOrientationHist looks like this: static float calcOrientationHist(const Mat& img, Point pt, int radius, float sigma, float* hist, int n){ int i, j, k, len = (radius*2+1)*(radius*2+1); float expf_scale = -1.f/(2.f * sigma * sigma); AutoBuffer buf(len*4+n+4); float *X = buf, *Y = X + len, *Mag = X, *Ori = Y + len, *W = Ori + len; float* temphist = W + len + 2; for( i = 0; i < n; i++){ temphist[i] = 0.f; } for(i = -radius, k = 0; i <= radius; i++){ int y = pt.y+i; if( y <= 0 || y >= img.rows -1) continue; for(j = - radius; j <= radius; j++){ int x = pt.x + j; if(x <= 0 || x >= img.cols -1) continue; //in opencv sift at float dx = (float)(img.at(y, x+1) - img.at(y,x-1)); float dy = (float)(img.at(y-1,x) - img.at(y+1,x)); X[k] = dx; Y[k] = dy; W[k] = (i*i + j*j)*expf_scale; k++; } } len = k; //compute gradient values, orientations and the weights over the pixel neighborhood hal::exp32f(W, W, len); hal::fastAtan2(Y, X, Ori, len, true); hal::magnitude32f(X, Y, Mag, len); for( k = 0; k < len; k ++){ int bin = cvRound((n/360.f)+Ori[k]); if(bin >= n) bin -=n; if(bin < 0 ) bin +=n; temphist[bin] += W[k]*Mag[k]; } //smooth the histogram temphist[-1] = temphist[n-1]; temphist[-2] = temphist[n-2]; temphist[n] = temphist[0]; temphist[n+1] = temphist[1]; for(i = 0; i < n; i++){ hist[i] = (temphist[i-2] + temphist[i+2])*(1.f/16.f)+ (temphist[i-1] +temphist[i+1])*(4.f/16.f)+ temphist[i]*(6.f/16.f); } float maxval = hist[0]; for(i = 1; i < n; i++) maxval = std::max(maxval, hist[i]); return maxval; } While the Malloc Error is thrown when I push_back the candidates Vector, I dont think this is the Reason it appears. But until now I couldn't find the reason for that. **EDIT** After Running the Project multiple times now It seems to through the malloc error at different Locations each time. The Memory it uses is at 10MB I wonder why it throws an malloc error... I Marked the Spots in the Code.

Viewing all articles
Browse latest Browse all 19555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>