Opencv resize has two ways for specifying resize factors.
First one is the cv::Size dsize, second one is the pair of factors fx and fy.
I’d suggest to use cv::Size only. Here is a sample of code doing capture with CSI onboard camera using gstreamer (you need an opencv build with gstreamer support).
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/video.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/cudawarping.hpp>
static inline unsigned long int GetTimeNs()
{
struct timespec curTime;
timespec_get(&curTime, TIME_UTC);
return curTime.tv_sec * 1e9 + curTime.tv_nsec;
}
//#define USE_OPENGL 1
int main()
{
//printf("%s\n", cv::getBuildInformation().c_str());
//setenv("GST_DEBUG", "*:3", 0);
const char* gst = "nvcamerasrc ! video/x-raw(memory:NVMM), format=I420, width=640, height=480, framerate=30/1 ! "
"nvvidconv ! video/x-raw, format=BGRx, width=640, height=480, framerate=30/1 ! "
"videoconvert ! video/x-raw, format=BGR, width=640, height=480, framerate=30/1 ! "
"appsink";
cv::VideoCapture cap(gst);
if(!cap.isOpened()) {
std::cout<<"Failed to open camera."<<std::endl;
return (-1);
}
cv::Mat frame_in, frame_out;
cv::cuda::GpuMat g_frame_in, g_frame_out;
if (!cap.read(frame_in)) {
std::cout<<"Capture read error"<<std::endl;
return (-2);
}
#ifndef USE_OPENGL
cv::namedWindow("MyCamera", cv::WINDOW_AUTOSIZE);
cv::namedWindow("MyResizedCamera", cv::WINDOW_AUTOSIZE);
#else /* USE_OPENGL */
cv::namedWindow("MyCamera", cv::WINDOW_AUTOSIZE | cv::WINDOW_OPENGL);
cv::namedWindow("MyResizedCamera", cv::WINDOW_AUTOSIZE | cv::WINDOW_OPENGL);
#endif /* USE_OPENGL */
unsigned long int endClk = GetTimeNs();
unsigned long int startClk, imshowClk, waitKeyClk;
unsigned int frameCount = 0;
unsigned int lastPos = cap.get(cv::CAP_PROP_POS_MSEC);
unsigned int newPos, interval;
unsigned int prevTraceLen = 0;
while(1)
{
if (!cap.read(frame_in)) {
std::cout<<"Capture read error"<<std::endl;
break;
}
else {
startClk = GetTimeNs();
clock_t captureLen = startClk - endClk;
newPos = cap.get(cv::CAP_PROP_POS_MSEC);
interval = newPos - lastPos;
lastPos = newPos;
// Start Host2Device copy + CUDA Process
g_frame_in.upload(frame_in);
cv::Size newSize( g_frame_in.size().width / 2 , g_frame_in.size().height / 2 );
cv::cuda::resize(g_frame_in, g_frame_out, newSize, 0, 0, cv::INTER_AREA);
g_frame_out.download(frame_out);
// End CUDA Process + Device2Host Process
imshowClk = GetTimeNs();
clock_t processLen = imshowClk - startClk;
#ifndef USE_OPENGL
cv::imshow("MyCamera", frame_in);
cv::imshow("MyResizedCamera", frame_out);
#else /* USE_OPENGL */
cv::imshow("MyCamera", g_frame_in);
cv::imshow("MyResizedCamera", g_frame_out);
#endif /* USE_OPENGL */
waitKeyClk = GetTimeNs();
clock_t imshowLen = waitKeyClk - imshowClk;
if (cv::waitKey(1) == 27) // If ESC key has been pressed then exit
break;
endClk = GetTimeNs();
clock_t waitKeyLen = endClk - waitKeyClk;
clock_t loopLen = endClk - startClk;
printf("#%04d: pos_interval=%d, cap=%f, proc=%f, show=%f, waitKey=%f, loop=%f, prevTrace=%f\n",
frameCount, interval, captureLen/1.e6, processLen/1.e6, imshowLen/1.e6, waitKeyLen/1.e6,
loopLen/1.e6, prevTraceLen/1e6);
frameCount++;
prevTraceLen = GetTimeNs() - endClk;
endClk += prevTraceLen;
}
}
cap.release();
return 0;
}