Rocket
Rocket

Reputation: 553

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.

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

Upvotes: 0

Views: 3342

Answers (3)

jnovacho
jnovacho

Reputation: 2903

If the only goal is to resize the images I would guess it would be simpler to use dedicated software with batch processing capability, e.g. 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.

Upvotes: 0

Rocket
Rocket

Reputation: 553

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

Upvotes: 0

WangYudong
WangYudong

Reputation: 4423

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. 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;
}

Upvotes: 1

Related Questions