c++讀txt 圖像路徑代碼

問題:E:/data.txt存放的是一個文件夾所有的圖像文件路徑(例如:E:\data\凸體卡shrink\08-51-57-0001-00.jpg ),現在要讀出每張圖像,還要讀出每張圖像的名字(08-51-57-0001-00.jpg

   ifstream imglist("E:/data.txt");                     //用ifstream 

   char  perImgpath[255];                               //定義一個數組,存每幅圖像的路徑

     while (!imglist.eof())                                  //如果沒有到達末尾

    {

          imglist.getline(perImgpath, 255);          // 此時,perimgpath存的是imglist每一行的數據

          Mat srcImg = imread(perImgpath) ;      //  opencv讀入圖像

         

          string imgName = perImgpath;                 

           if  (imgName.find_last_of("\\") != -1)        //   找到最後一個"\\", 若存在,返回那個位置,不存在,返回-1

                imgName = imgName.substr(imgName.find_last_of("\\") + 1);     //  返回 位置加1 的一個字符串,即08-51-                                                                                                                             57-0001-00.jpg 

           else

                imgName = imgName.substr(imgName.find_last_of("/") + 1);

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章