Hello everybody. I am seeking help on how to save 3 output images into a specific path after filter by 3 different mask size e.g. 3x3, 5x5 and 7x7 mask. My program is in C:\Cpp\Project\, I wish to save those output images inside "filter" folder with path in C:\Cpp\Project\filter. Thus, the "filter" folder will have output images with name - "outputBOHE 3 x 3.pgm", "outputBOHE 5 x 5.pgm", and "outputBOHE 7 x 7.pgm". However, when I run my program, those output images are save at C:\Cpp\Project. Although I had created the "filter" folder, the output images cannot be save in C:\Cpp\Project\filter. Please advise me on how to solve this problem. Thanks in advance.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

string NumberToString(int t)
       ostringstream os;
       os << t;
       return os.str();

int main()
   int cr_w, cr_h; 
   int ind=0;
   image= cv::imread("BOHE.pgm", 0);  //openCV format for reading an image

   nl = image.rows;  //openCV format to obtain image rows
   nc = image.cols;  //openCV format to obtain image columns

    for (int i = 0; i < 3; i++)
     cr_w = 3 + 2*(i%3); //mask width
     cr_h = 3 + 2*(i%3); //mask height

     //create 3 output images with name of "outputBOHE 3 x 3.pgm", "outputBOHE 5 x 5.pgm", and "outputBOHE 7 x 7.pgm" after filtering with 3x3, 5x5, and 7x7 mask
       std::string name = "outputBOHE " + NumberToString(cr_w) +" x "+   NumberToString(cr_h)+".pgm";

     //save the 3 output images into the following path
       ofstream fout ("C:\Cpp\Project\filter");

     fout.open (name.c_str());

     fout <<"P2\n"<<nc<<'\t'<<nl<<endl<<cr_w<<'\t'<<cr_h<<endl<<255<<endl;  //P2 is for image with pgm extension

     for (int y=0; y<nl; y++)
        for (int x=0; x<nc; x++) 
          fout << int(image.at<uchar>(y,x))<<' ';


return 0;
4 Years
Discussion Span
Last Post by deceptikon

I think you're confused about the string passed to ofstream for opening a file. It's the file path, not the folder. So for each file, you need to make sure that the folder is included in the path. In other words:

std::string name = "outputBOHE " + NumberToString(cr_w) +" x "+   NumberToString(cr_h)+".pgm";
std::string path = "C:\\Cpp\\Project\\filter\\" + name;

ofstream fout (path.c_str());

fout <<"P2\n"<<nc<<'\t'<<nl<<endl<<cr_w<<'\t'<<cr_h<<endl<<255<<endl;


Edited by deceptikon


However, when i tried to use the full path (std::string path = "C:\Cpp\Project\filter\" + name;), there was no output image in the "filter" folder.

When I edited the above code to (std::string path = "filter\" + name;), there were output images in the "filter" folder.

Why does this happen? Is the full path redundant since the "filter" folder located in the same path as my program?

How about when I want to save those output images in other path, for example in "D:\output\"? In this case, when i specify the full path ("D:\output\"), there were no images save in the "output" folder. My program still in "C:\Cpp\Project\"

Please advise. Thanks.

Edited by hsc852: changes


Please read the code I posted very carefully. A single backslash and two backslashes in a string literal are very different things.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.