简体   繁体   中英

OpenCV: How to save images using cv::imwrite without overwriting them

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.

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM