0

I have a small application that saves images using cv::imwrite. The problem that I have is that I have been trying to find a way to store cropped images after matching them. I successfully extract the cropped image but it is overwritten in the same folder instead just added to the folder. How can I save images without overwriting them?

See below the most important piece of the code:

stereo.h

public:
    void cropMatches(int xa, int ya, int xb, int yb);

stereo.cpp

void StereoCal::cropMatches(int xa, int ya, int xb, int yb){
    cv::Point ptA=cv::Point(xa, ya);
    cv::Point ptB=cv::Point(xb, yb);
    cv::Size sz;
    sz.height=regionSize;
    sz.width=regionSize;

    cv::Mat regionA, regionB;
    cv::getRectSubPix(currentImages.getA().get8Bitmap(),sz,ptA,regionA);
    cv::getRectSubPix(currentImages.getB().get8Bitmap(),sz,ptB,regionB);
    std::string path = "/home/path/to/folder/";
    std::string outA = path+"cropA.tiff";
    std::string outB = path+"cropB.tiff";
    cv::imwrite(outA,regionA);
    cv::imwrite(outB,regionB);
}

Please shed a little bit of light on how to solve this issue.

Emanuele
  • 2,194
  • 6
  • 32
  • 71
  • choose individual filenames for each image. e.g. with outA=path+std::to_string(counter++)+"_cropA.tiff"; where counter is an integer variable initialized with 0 somewhere globally or given to the function as a parameter and managed/incremented outside. – Micka Dec 15 '18 at 21:52
  • btw. if you want to imwrite from.within that function you should give the user the possibility to choose the path and filenames. But I would recommend to just return the result mats to the user and code individual functions for saving the images to files. – Micka Dec 15 '18 at 21:54
  • Hi Micka and thanks for your help. I changed the code, but my problem still remains. After I run the first match and extract the first crops, I save the two images but when I run the second match the new two cropped images are overwritten in the same folder. I always end up with two images instead of an increasing number. Here is how I changes putting to zero a count variable: – Emanuele Dec 16 '18 at 16:00
  • `std::string path = "/path/to/Desktop/croppedMatches/";` `int i = 0, int j = 0;` `std::string outA = path+std::to_string(i++) + "_cropA.tiff";` `cv::imwrite(outA,regionA);` `std::string outB = path+std::to_string(j++) + "_cropB.tiff";` `cv::imwrite(outB,regionB);` – Emanuele Dec 16 '18 at 16:01
  • i and j have to be known before the function, so that the value will not be set to 0 in the function. You will have to make sure that it is 1 after the first call of the function, 2 after the second call, and so on. – Micka Dec 16 '18 at 16:05
  • it still not working. I still overwrite images. I am not sure how to position the initialized counters `i=0` and `j=0`. Is there any way you can provide some edited code to the function I wrote so that it clarifies the doubt that I still have?. Thank you for your hep so far. – Emanuele Dec 16 '18 at 17:50

1 Answers1

0

in stereo.h:

public:
void cropMatches(int xa, int ya, int xb, int yb);

private:
int counterA=0;
int counterB=0;

in stereo.cpp

void StereoCal::cropMatches(int xa, int ya, int xb, int yb){
cv::Point ptA=cv::Point(xa, ya);
cv::Point ptB=cv::Point(xb, yb);
cv::Size sz;
sz.height=regionSize;
sz.width=regionSize;

cv::Mat regionA, regionB;
cv::getRectSubPix(currentImages.getA().get8Bitmap(),sz,ptA,regionA);
cv::getRectSubPix(currentImages.getB().get8Bitmap(),sz,ptB,regionB);
std::string path = "/home/path/to/folder/";
std::string outA = path+"cropA_"+std::to_string(counterA++)+".tiff";
std::string outB = path+"cropB_"+std::to_string(counterB++)+".tiff";
cv::imwrite(outA,regionA);
cv::imwrite(outB,regionB);

}

Micka
  • 19,585
  • 4
  • 56
  • 74