Opencv图像视频读取

Opencv图像视频读取

图片读取

使用opencv数据结构Mat存放,imread指令用作读取图片数据,nameWindow指令创建窗口,imshow显示图片

int main( void )
{
    Mat image1 =imread("1.jpg");
    namedWindow("im",WINDOW_AUTOSIZE);
    imshow("im",image1);
    waitKey(0);
    return 0;
}

imread函数参数:

CV_EXPORTS_W Mat imread( const
string& filename, int
flags=1 );

filename —— 文件的位置。如果只提供文件名,那么文件应该和C++文件在同一目录,否则必须提供图片的全路径。
flags —— 有5个可能的输入。
CV_LOAD_IMAGE_UNCHANGED – 在每个通道中,每个像素的位深为8 bit,通道数(颜色)保持不变。
CV_LOAD_IMAGE_GRAYSCALE – 位深=8 bit 通道数=1(颜色变灰)
CV_LOAD_IMAGE_COLOR -位深=?, 通道数=3
CV_LOAD_IMAGE_ANYDEPTH – 位深不变 ,通道数=?
CV_LOAD_IMAGE_ANYCOLOR – 位深=?, 通道数不变
上面的值还可以组合使用,比如:
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR – 位深不变,通道数比便
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH – 位深不变,通道数=3
如果你不确定使用哪个,就是用CV_LOAD_IMAGE_COLOR 。

注:没有waitkey指令,图片显示不出来,

官方解释如下:
A common mistake for opencv newcomers is to call cv::imshow() in a loop through video frames, without following up each draw with cv::waitKey(30). In this case, nothing appears on screen, because highgui is never given time to process the draw requests from cv::imshow().

参考详细图像博客

视频读取

代码

    Mat frame;
    VideoCapture capture;
    capture.open( -1 );//打开本地摄像头输入
    capture.open("xx.mp4");//读取工程目录下的某个mp4视频
    while(capture.read(frame)){//frame为视频的每帧

    }

点个赞呗

猜你喜欢

转载自blog.csdn.net/yanrong1095/article/details/78689628