opencv读取文件夹中的图片

http://blog.csdn.net/yang6464158/article/details/41622711
http://jingyan.baidu.com/article/656db918a7e848e381249c0e.html
在opencv里读取文件夹里的图是一件很重要的工作,尤其是一组序列图像,然后对每一张图像都进行操作。

方法一,用 filename,sprintf_s,imread

    char filename[100];
    Mat srcImage, dstImage;

    for (unsigned int i = 0; i < 100; i++)
    {
        sprintf_s(filename, "E://imageseries//bw_%04d.png", i);
        srcImage = imread(filename, 0);//读取文件夹里的图像
    if (!srcImage.data){ printf("error!\n"); return false; }
        imshow("image", srcImage);
        waitKey(1000);
...
//保存处理的图像到文件夹,在读入程序里面添加
sprintf__s(filename, "E://imageseries//after//bw_%04d.png", i);
imwrite(filename,dstImage)

}

方法二、用VideoCapture sequence

    string first_file = "E://imageseries//bw_%04d.png";
    VideoCapture sequence(first_file);

    if (!sequence.isOpened())
    {
        cerr << "Failed to open the image sequence!\n" << endl;
        return 1;
    }
    Mat image;
    namedWindow("Image sequence", 1);
    for (;;){
        sequence >> image;
        if (image.empty())
        {
            cout << "End of Sequence" << endl;
            break;
        }
        imshow("Image sequence", image);
        waitKey(300);
    }

方法三:
string src_image_name = “F:\imageseries\before\”;
string src_image_name_after = “F:\imageseries\after\”;
string src_image_name1 = src_image_name;
char num[3], num1[4] = “1 (“, num2[6] = “).png”;
string num3 = “.png”;
for (i = 1; i <= 3; i++)
{
itoa(i, num, 10);
src_image_name += num1;
src_image_name += num;
src_image_name += num2;
cout << src_image_name << endl;
src = imread(src_image_name, 1);
imshow(src_image_name, src);
}
opencv读取文件夹中的图片_第1张图片

你可能感兴趣的:(opencv-3-0)