最近在練習使用OpenCV,順手紀錄一些用法。下面這段code可以用來控制電腦上的webcam進行拍照並且轉換成QImage物件,以便後續的使用。
#include <opencv2/opencv.hpp>
CvCapture *camera;
IplImage *frame;
camera = cvCreateCameraCapture(-1);
/* We need to call cvRetrieveFrame twice here to get the last frame.
* TODO: figure out the root cause and use correct method instead of
* the workaround.
*/
cvGrabFrame(camera);
frame = cvRetrieveFrame(camera);
frame = cvRetrieveFrame(camera);
if (frame)
{
QImage image(reinterpret_cast
frame->width, frame->height,
frame->widthStep, QImage::Format_RGB888);
QImage colorCorrectedImage(image.rgbSwapped());
/* Show colorCorrectedImage which contains the picture. */
...
}
cvReleaseCapture(&camera);
留言