简体   繁体   English

如何制作所有大小相同的图像并将其写入文件夹

[英]How to make all images of same size and write them in a folder

I am trying to convert different sizes of images from my different folders to the same size as defined in the width and height and them save them in different folder or replace them, I use the function cv::resize for it, and surely imwrite may be use for saving them, but its not working for me, as it showing me error in the parameters of resize. 我正在尝试将来自不同文件夹的不同大小的图像转换为宽度和高度中定义的相同大小,并将它们保存在不同的文件夹中或替换它们,我使用了cv::resize函数,当然可以imwrite可用于保存它们,但对我不起作用,因为它向我显示调整大小参数中的错误。

int count = 0;
int width = 144;
int height = 33;
vector<string>::const_iterator i;
string Dir;
for (i = all_names.begin(); i != all_names.end(); ++i)
{
    Dir=( (count < files.size() ) ? YourImagesDirectory_2 : YourImagesDirectory_3);

    Mat row_img = cv::imread( Dir +*i, 0 );

    cv::resize(row_img , width , height);
    imwrite( "D:\\TestData\\img_resize.jpg", img_resize );

    ++count;
}

After resize this function : 调整此函数的大小后:

imwrite( "D:\\TestData\\img_resize.jpg", img_resize );

Only save one image to my folder test , i want all of them in my folder 只将一张图像保存到我的文件夹测试中,我希望所有图像都在我的文件夹中

Here is an example for how to resize an image: 这是如何调整图像大小的示例:

Mat img = imread("C:\\foo.bmp");
Mat img_resize;
resize(img, img_resize, Size(144, 33));

EDIT: 编辑:

Supposed that you have several images named as image001.jpg , image002.jpg , image003.jpg , image004.jpg , image005.jpg ..., and want to save them after resizing. 假设您有几张名为image001.jpgimage002.jpgimage003.jpgimage004.jpgimage005.jpg ...的图像,并且要在调整大小后保存它们。 Hopes the following code works it out. 希望下面的代码能解决。

#include <cv.h>
#include <highgui.h>
using namespace cv;

char pathLoading[255];
char pathSaving[255];
char num[10];
char jpg[10] = ".jpg";
int counter = 1;

int main(int argc, char** argv) {
    while (1) {
        if (counter < 6) {
            // To load 5 images
            strcpy(pathLoading, "c:\\image");
            sprintf(num, "%03i", counter);
            strcat(pathLoading, num);   
            strcat(pathLoading, jpg);
            Mat image = imread(pathLoading);
            Mat image_resize;
            resize(image, image_resize, Size(144, 33));
            // To save 5 images
            strcpy(pathSaving, "c:\\image_resize");
            sprintf(num, "%03i", counter);
            strcat(pathSaving, num);   
            strcat(pathSaving, jpg);
            imwrite(pathSaving, image_resize);
            counter++;          
        }
    }
    return 0;
}

Here is the way through which i can csave multiple images in the folder : 这是我可以在文件夹中保存多个图像的方法:

for (i = all_names.begin() ; i!= all_names.end() ; i++)
    {
        Dir=( (count < files.size() ) ? YourImagesDirectory : YourImagesDirectory_2);
        Mat row_img = cv::imread(Dir+*i );
        //imshow ("show",row_img);
        Mat img_resize;
        resize(row_img, img_resize, Size(144, 33));
        Mat img = img_resize;
        sprintf(buffer,"D:\\image%u.jpg",count);
        imwrite(buffer,img);
        //imwrite("D:\\TestData\\*.jpg" , img_resize);
        count++;
    }

Use the functions : 使用功能:

sprintf(buffer,"D:\\image%u.jpg",count);
imwrite(buffer,img);

For giving directory , name and imwrite for saving there 用于提供目录,名称和名称以保存在此处

If the only goal is to resize the images I would guess it would be simpler to use dedicated software with batch processing capability, eg IrfanView. 如果唯一的目标是调整图像大小,我想使用具有批处理功能的专用软件(例如IrfanView)会更简单。

If this is programming exercise, nevermind my answer and look at answers by other people. 如果这是编程练习,请不要介意我的答案,而要看看其他人的答案。

HINT: You are saving all your images with single filename, thus effectively rewriting the previously converted images with the new ones. 提示:您将所有图像保存为一个文件名,从而有效地用新图像重写了先前转换的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM