Ask Your Question
0

How save multiple image in a "BOOST_FOREACH" loop

asked 2016-10-11 08:08:04 -0600

harfbuzz gravatar image

I have written the snippet code below to save multiple images with different names but it doesn't work. Thanks in advance for your help.

#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <sstream>

 ........
 int nBlur[] = {5, 7, 13};
 BOOST_FOREACH( int i, nBlur)
{
    std::string str = boost::lexical_cast<std::string>(i); // convert i to string
    string imgF = "imgBlur" + i;
    const char *imgFile = imgF.c_str();

    GaussianBlur(imgGray, imgGrayBlur, Size(i,i), 0, 0 );
    //imshow("Blurring image", imgGrayBlur);
    Mat blurred(imgGrayBlur);
    imwrite(imgFile+".png", imgGrayBlur);
}
edit retag flag offensive close merge delete

Comments

you cannot add ".png" to a char*.

berak gravatar imageberak ( 2016-10-11 08:11:16 -0600 )edit

there is another way other than the BOOST loop ?

harfbuzz gravatar imageharfbuzz ( 2016-10-11 08:18:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-10-11 08:17:02 -0600

berak gravatar image

updated 2016-10-11 08:17:59 -0600

come back to earth, get rid of that boost nonsense, and just use cv::format :

int nBlur[] = {5, 7, 13};
for (int i=0; i<3; i++)
{
    int b=nBlur[i];
    GaussianBlur(imgGray, imgGrayBlur, Size(b,b), 0, 0 );
    //imshow("Blurring image", imgGrayBlur);
    Mat blurred(imgGrayBlur);

    cv::String filename = format("imgBlur%d.png", b);
    imwrite(filename, imgGrayBlur);
}
edit flag offensive delete link more

Comments

1

it works fine, thank you very much

harfbuzz gravatar imageharfbuzz ( 2016-10-11 08:26:07 -0600 )edit

Is there in opencv a command allows to save files into a specific folder ?

harfbuzz gravatar imageharfbuzz ( 2016-10-13 05:35:24 -0600 )edit

not really. (and you also probably asked for a way to create folders programatically, which is very os-specific. i personally use some hack like system("mkdir myfolder")

berak gravatar imageberak ( 2016-10-13 05:47:24 -0600 )edit

In order to save files in a new folder, e.g. myFolder, instead of the current directory I've used cv::String filename = format("/path_to_myFolder/imgBlur%d.png", b); but the png files have been saved on both current directory and myFolder. Maybe I missed to add something at this command ?

harfbuzz gravatar imageharfbuzz ( 2016-10-16 04:50:29 -0600 )edit

no idea. can it be, some of them were from a previous run ? it simply can't be , that it's writing to both at the same time

berak gravatar imageberak ( 2016-10-16 05:08:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-11 08:08:04 -0600

Seen: 952 times

Last updated: Oct 11 '16