Ask Your Question
0

DNN module - Can I pass in an arbitrary sized input ?

asked 2016-12-28 04:17:38 -0600

Nbb gravatar image

The deep NN module in opencv requires me to specify the input size http://docs.opencv.org/trunk/d5/de7/t...

resize(img, img, Size(224, 224));       //GoogLeNet accepts only 224x224 RGB-images
dnn::Blob inputBlob = dnn::Blob(img);   //Convert Mat to dnn::Blob image batch

Is it possible to send in an arbitrary sized input because some of the recent papers accept an arbitrary sized input.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-28 05:49:59 -0600

pi-null-mezon gravatar image

You could simple try. But the right answer is NO, you should pre scale image before input:

cv::Mat cropAndResizeFromCenter(const cv::Mat &input, const cv::Size size)
{
    cv::Rect roiRect(0,0,0,0);
    if( (float)input.cols/input.rows > (float)size.width/size.height) {
        roiRect.height = input.rows;
        roiRect.width = input.rows * (float)size.width/size.height;
        roiRect.x = (input.cols - roiRect.width)/2.0;
    } else {
        roiRect.width = input.cols;
        roiRect.height = input.cols * (float)size.height/size.width;
        roiRect.y = (input.rows - roiRect.height)/2.0;
    }
    roiRect &= cv::Rect(0, 0, input.cols, input.rows);
    cv::Mat output;
    if(roiRect.area() > 0)  {
        cv::Mat croppedImg(input, roiRect);
        int interpolationMethod = 0;
        if(size.area() > roiRect.area())
            interpolationMethod = CV_INTER_CUBIC;
        else
            interpolationMethod = CV_INTER_AREA;
        cv::resize(croppedImg, output, size, 0, 0, interpolationMethod);
    }
    return output;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-12-28 04:17:38 -0600

Seen: 191 times

Last updated: Dec 28 '16