您的位置:首页 > 运维架构 > Linux

Linux平台下用OpenCV读取文件夹内图片并显示

2017-10-24 13:57 615 查看
     #include <cv.h>

     #include <highgui.h>

     #include <iostream>

     #include <stdio.h>

     #include <unistd.h>

     #include <dirent.h>

     #include <stdlib.h>

     #include <sys/stat.h>

     #include <string.h>

      //定义文件夹的绝对路径

      const char* dir_name = "/home/zhhangfuqiang/images/";

      struct dirent * filename;    // return value for readdir()
      DIR * dir;                   // return value for opendir()

      //打开文件夹

      dir = opendir( dir_name );

      cout<<"Successfully opened the dir !"<<endl;

      /* 遍历文件夹中的所有文件 */

      while( ( filename = readdir(dir) ) != NULL )

         {

      get rid of "." and ".."

           if( strcmp( filename->d_name , "." ) == 0 ||

             strcmp( filename->d_name , "..") == 0    )

             continue;
      cout<<filename ->d_name <<endl;

      //为文件名赋值

      string name=filename->d_name;

      //OpenCV读取Mat格式的图片

      Mat img=imread("/home/zhangfuqiang/images/"+name, CV_LOAD_IMAGE_UNCHANGED);

      //实例化窗口

      cv:: namedWindow("Depth image");
      Mat img_update;

      //因为我是用Kinect2采集的图片,尺寸为1920x1080,我把它压缩一下

      cv::resize(img,img_update,cv::Size(640,480),(0,0),(0,0),cv::INTER_LINEAR);

      //在窗口上展示图片

      imshow("Depth image",img_update);

      //wait key for 5000ms

      cv::waitKey(500);

     }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: