Ask Your Question

Load images to crop automatically

asked 2015-03-27 00:12:26 -0500

zms gravatar image

Hello, I have like 10 images and need to be cropped. What I can do right now is I can load-crop-save the image one by one.

Next i'll have like thousands of images which i think could take me sometimes to do one by one. Im thinking of using the for loop but looks like i received the error string to int conversion which i could not understand.

here is my code... the problem is to load the image automatically with the different at the postfix using number increment i.e. image0, image1, image2 and so on.

for( int i = 0; i < 12000; i = i + 1 )
image = imread(("C://BITWISE//Image%d.jpg",a), CV_LOAD_IMAGE_COLOR);   
Mat croppedImage = image(myROI);
cv::imwrite( cv::format( "C:/BITWISE/Croped%d.jpg",i), croppedImage ); 

waitKey(0);                                          // Wait for a keystroke in the window
return 0;


appreciate on your help

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-27 01:14:26 -0500

Hi! You can get all the files names of a folder as follows. Thanks to Theodore for his nice answer.

int main()
    vector<String> filenames; // notice here that we are using the Opencv's embedded "String" class
    String folder = "<some_folder_path>"; // again we are using the Opencv's embedded "String" class

    glob(folder, filenames); // new function that does the job ;-)

    for(size_t i = 0; i < filenames.size(); ++i)
        Mat src = imread(filenames[i]);

            cerr << "Problem loading image!!!" << endl;

        /* do whatever you want with your images here */
edit flag offensive delete link more



thanks thanks thankssssssssssssssssssssssss!!! to Theodore and you balaji and everyone on the answer..

zms gravatar imagezms ( 2015-03-27 01:44:28 -0500 )edit

Hello balaji, If the file in the folder I need to have the name to be put in the string, how can I do that? I need this info for me to rename the new post image processing with the same name of the original one. APpreciate if there any idea on that. TQ

zms gravatar imagezms ( 2015-07-22 02:41:45 -0500 )edit

@zms i did not understand what you want to do.

sturkmen gravatar imagesturkmen ( 2015-07-23 21:15:30 -0500 )edit

@sturkmen, ok like this, the code above is to automatically taking all the files under one folder. Put example in the folder, there are files names file1, file2 and file3. I want to process the files, put into other folder and rename the files exactly the name in the original folder which are file2, file2 and file3. the problem right now I could not get the string filename and save on another location. What I can do is only save the file in sequence number and not following the original name of the file. TQ

zms gravatar imagezms ( 2015-07-28 02:53:18 -0500 )edit

:) i now i am a bit late... @zms did you solve your problem about extracting file names?

sturkmen gravatar imagesturkmen ( 2015-10-19 14:22:46 -0500 )edit

Question Tools

1 follower


Asked: 2015-03-27 00:12:26 -0500

Seen: 1,271 times

Last updated: Mar 27 '15