none
Low performance while processing Kinect v2 data in OpenCV. RRS feed

  • Question

  • I am making an application to track a ball and recognize faces using the Kinect V2 and OpenCV.

    The problem is the frame rate that I am getting, its too low.

    This is how I am obtaining the Color Frame from Kinect.

    cv::Mat getColorFrame(IColorFrameReader* _color_reader, HRESULT sourceInitHresult)
    	{
    		HRESULT hr = sourceInitHresult;
    		IColorFrame* frame = NULL;
    		IFrameDescription* frameDesc;
    		cv::Mat colorImage;
    		if (SUCCEEDED(hr)) {
    			hr = _color_reader->AcquireLatestFrame(&frame);
    			if (SUCCEEDED(hr)) {
    				hr = frame->get_FrameDescription(&frameDesc);
    				if (SUCCEEDED(hr)) {
    					int frameWidth = 0, frameHeight = 0;
    					hr = frameDesc->get_Width(&frameWidth);
    					if (SUCCEEDED(hr)) {
    						hr = frameDesc->get_Height(&frameHeight);
    					}
    					if (SUCCEEDED(hr)) {
    						const int imgSize = frameWidth*frameHeight * 4; //4 Channels(BGRA)
    						BYTE* frameData = new BYTE[imgSize];
    						hr = frame->CopyConvertedFrameDataToArray(imgSize, frameData, ColorImageFormat_Bgra);
    						if (SUCCEEDED(hr)) {
    							colorImage = cv::Mat(frameHeight, frameWidth, CV_8UC4, reinterpret_cast<void*>(frameData));
    						}
    					}
    				}
    				SafeRelease(frameDesc);
    				SafeRelease(frame);
    			}
    		}
    		return colorImage;
    	}

    and this is the code that I am using to detect the ball using the color filtering and HoughCircle method.

    setUseOptimized(true);
    	//VideoCapture cap(0);
    	CKinectSources* cap = new CKinectSources();
    	cap->initSensor();
    	cap->initSourceReader(COLOR_S | DEPTH_S);
    	namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
    
    	int iLowH = 0;
    	int iHighH = 25;
    
    	int iLowS = 133;
    	int iHighS = 255;
    
    	int iLowV = 136;
    	int iHighV = 192;
    
    	//Create trackbars in "Control" window
    	createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
    	createTrackbar("HighH", "Control", &iHighH, 179);
    
    	createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
    	createTrackbar("HighS", "Control", &iHighS, 255);
    
    	createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
    	createTrackbar("HighV", "Control", &iHighV, 255);
    
    while (true)
    	{
    		Mat imgOriginal , depth;
    		imgOriginal = cap->getFrame(COLOR_F);
    		if (imgOriginal.data) {
    			resize(imgOriginal, imgOriginal, Size(),0.5,0.5);
    			Mat imgHSV;
    			vector<Vec3f> circles;
    
    			medianBlur(imgOriginal, imgOriginal, 3);
    
    			cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
    
    
    			Mat imgThresholded;
    
    			inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
    
    			GaussianBlur(imgThresholded, imgThresholded, Size(9, 9), 0, 0);
    
    HoughCircles(imgThresholded, circles, CV_HOUGH_GRADIENT, 1, imgThresholded.rows / 8, 100, 18, 1, 50);
    
    
    			for (int i = 0; i < circles.size(); i++) {
    				Point centr(round(circles[i][0]), round(circles[i][1]));
    				int radius = round(circles[i][2]);
    				circle(imgOriginal, centr, radius, Scalar(0, 255, 0), 2);
    				string rad = to_string(radius);
    				string x = to_string(circles[i][0]);
    				string y = to_string(circles[i][1]);
    				string outputx("x = " + x);
    				string outputy(" y = " + y);
    				centr.x += 30;
    				putText(imgOriginal,outputx,
    					centr, CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 255, 0));
    				Point centry = centr;
    				centry.y += 30;
    				putText(imgOriginal, outputy,
    					centry, CV_FONT_HERSHEY_PLAIN, 2, Scalar(0, 255, 0));
    			}
    imshow("Thresholded Image", imgThresholded);
    imshow("Original", imgOriginal);
    if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
    			{
    				//cout << "esc key is pressed by user" << endl;
    				destroyAllWindows();
    				break;
    			}
    		}
    	}
    And If I use the default Webcam instead of the Kinect I get very smooth framerate, I even scaled down the Kinect Frame to Half its size but still the performance is very low. Why is this happening?

    Friday, April 1, 2016 12:26 PM