Herr Song 2019-03-11 09:36 采纳率: 0%
浏览 381
已采纳

opencv批量处理图片出现的一个警告

用opencv批量处理一个文件夹里的图片,运行完会出现一个警告,帮忙看一下该怎么修改,谢谢。

#include<iostream>
#include<time.h>
#include<opencv2/opencv.hpp>
#include<fstream>

using namespace std;
using namespace cv;

void main()
{
    ifstream file("d:/filelist.txt");

    int img_index = 0;

    while (!file.eof())//当文件没有读到末尾继续循环
    {
        char txt_cont[24];
        file.getline(txt_cont,24);

        char img_file[24],save_file[24];

        sprintf(img_file, "d:/pic2/%s", txt_cont);
        sprintf(save_file, "d:/pic3/%d.jpg", img_index);

        Mat src = imread(img_file);
        Mat crs;
        cvtColor(src,crs,CV_RGB2GRAY);
        img_index++;

        imwrite(save_file,crs);
    }

}

图片说明

  • 写回答

2条回答

  • J4cks0n 2019-03-11 10:54
    关注

    这个看了下应该是内存越界问题 char数组那里 你可以采取把24变大的方式如改为256 或者用string来代替char数组
    下面是用string的方式

    #include<iostream>
    #include<time.h>
    #include<opencv2/opencv.hpp>
    #include<fstream>
    #include<string>
    
    
    using namespace std;
    using namespace cv;
    
    int main()
    {
        ifstream file("filelist.txt");
    
        int img_index = 0;
        string txt_cont;
    
        while (getline(file,txt_cont))//当文件没有读到末尾继续循环
        {
    
            string img_file,save_file;
    
            img_file = "/home/pi/cv_test/pic2/" + txt_cont;
            save_file = "/home/pi/cv_test/pic3/" + to_string(img_index) + ".jpg";
    
            Mat src = imread(img_file);
            Mat crs;
            cvtColor(src,crs,CV_RGB2GRAY);
            img_index++;
    
            imwrite(save_file,crs);
        }
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决