SURF_CUDA produces nan values
I'm using SURF_CUDA to describe around 5k images from the Oxford dataset. All the images are resized keeping the aspect ratio such that the largest size is a given amount of pixels (usually I set 400). This is the code that I'm using to compute descriptors for each image:
void SURF_CUDAOpenCV::ComputeDescriptors(cv::Mat &img, cv::Mat1f &descriptors){
cv::cuda::GpuMat imgGpu, descriptorsGpu;
std::vector<float> vf_descriptors;
std::vector<cv::KeyPoint> keypoints;
imgGpu.upload(img);
if(imgGpu.empty())
throw std::runtime_error("Error uploading gpuImg");
surf_cuda(imgGpu, cv::cuda::GpuMat(), keypoints, vf_descriptors);
descriptors = cv::Mat1f(vf_descriptors);
descriptors = descriptors.reshape(1,(int)vf_descriptors.size()/surf_cuda.descriptorSize());
for(size_t i=0; i<vf_descriptors.size(); i++)
assert(!std::isnan(vf_descriptors[i]));
}
For some reason, the assert fails for some images, which means that some descriptors values are nan
, which is obviously an error.
I noticed that resizing the images to 500 delay the error (the first image containing at least a nan
is the 1902-th, instead of 159-th), however I really wonder why this happens.
The error came out because I'm using these descriptors for k-means and some centroids values are nan
.
UPDATE:
I found out that not resizing the images the error doesn't happen and everything is fine. The function that I use to down-resize the image keeping the aspect ratio is this one:
void resize(cv::Mat &img, float targetSize){
float max = std::max(img.rows,img.cols);
if(max>targetSize){
float ratio = targetSize/max;
cv::resize(img,img, cv::Size((int)(img.cols*ratio),(int)(img.rows*ratio)),0,0,cv::INTER_CUBIC);
}
}
Why this happens?