Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 50 additions & 75 deletions lib/gpc/SintelOpticalFlow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class SintelOpticalFlow {
private:
typedef typename gpc::training::Feature F;
typedef typename F::GPCPatchTriplet GPCTriplet_t;
bool canDoExtraction = false;

/**
* @brief Checks dataset location sanity.
Expand Down Expand Up @@ -107,9 +106,12 @@ class SintelOpticalFlow {

// count images in first scene
numFrames = countImages();
canDoExtraction = true;
if (numFrames == 0) {
throw std::runtime_error(
"0 images found at specified dataset path");
}
}
SintelOpticalFlow() { canDoExtraction = false; }
SintelOpticalFlow() {}
/**
* @brief Extract training dataset made up of triplets (reference, positive,
* negative) image patches.
Expand All @@ -125,20 +127,12 @@ class SintelOpticalFlow {
std::vector<GPCTriplet_t> extractTrainingData(int numTripletsPerPair,
int radiusLower,
int radiusUpper) {
std::vector<GPCTriplet_t> trainingData;
if (canDoExtraction == false) {
cout << "ERR: No path for Sintel dataset specified" << endl;
return trainingData;
}
// Verify directory structure for Sintel Optical Flow dataset
if (!(isDir(cleanDir) && isDir(finalDir) && isDir(flowDir) &&
isDir(oclDir) && isDir(invDir))) {
cout << "ERR: This does not look like the Sintel Optical Flow "
"dataset. "
"Please verify paths."
<< endl;
return trainingData;
throw std::runtime_error("Directory structure invalid. ");
}
std::vector<GPCTriplet_t> trainingData;

// cycle through scenes
for (int sceneId = 0; sceneId < 20; sceneId++) {
Expand All @@ -150,39 +144,29 @@ class SintelOpticalFlow {
// Keep the necessary images
ndb::Buffer<uint8_t> oSrc, oTar, invSrc, invTar, imgL, imgR;
// Get images and disparity
try {
int err = 0;
Eigen::MatrixXd u, v;
err |= getFlow(imgId, u, v);
err |= getBW(imgId, imgL, imgR);
err |= getOcclusion(imgId, oSrc);
err |= getOcclusion(imgId + 1, oTar);
err |= getInvalid(imgId, invSrc);
err |= getInvalid(imgId + 1, invTar);
if (err)
throw std::invalid_argument(
"could not open dataset file. Verify paths to "
"Sintel dataset "
"are set correctly.");

// Get Keypoint coordinate lists for given image pair
getGroundTruthMatches(u,
v,
oSrc,
oTar,
invSrc,
invTar,
numTripletsPerPair,
radiusLower,
radiusUpper,
kptsL,
kptsR,
kptsN);
// Extract features under Feature requested
Feature.extractAllTriplets(
imgL, imgR, kptsL, kptsR, kptsN, trainingData);
} catch (const std::invalid_argument& e) {
}
Eigen::MatrixXd u, v;
getFlow(imgId, u, v);
getBW(imgId, imgL, imgR);
getOcclusion(imgId, oSrc);
getOcclusion(imgId + 1, oTar);
getInvalid(imgId, invSrc);
getInvalid(imgId + 1, invTar);
// Get Keypoint coordinate lists for given image pair
getGroundTruthMatches(u,
v,
oSrc,
oTar,
invSrc,
invTar,
numTripletsPerPair,
radiusLower,
radiusUpper,
kptsL,
kptsR,
kptsN);
// Extract features under Feature requested
Feature.extractAllTriplets(
imgL, imgR, kptsL, kptsR, kptsN, trainingData);
} // image loop
} // scene loop

Expand Down Expand Up @@ -212,13 +196,10 @@ class SintelOpticalFlow {
std::vector<GPCTriplet_t> loadTrainingData(std::string path) {
struct stat buffer;
if (stat(path.c_str(), &buffer) != 0) {
std::vector<GPCTriplet_t> emptyset;
cout << "ERR: No extracted training set found at given path"
<< endl;
return emptyset;
} else {
return Feature.loadAllTriplets(path);
throw std::runtime_error(
"No extracted training set found at given path");
}
return Feature.loadAllTriplets(path);
}

private:
Expand Down Expand Up @@ -330,7 +311,7 @@ class SintelOpticalFlow {
return cnt;
} else {
/* could not open directory */
std::cout << "ERR:couldn't open directory" << std::endl;
throw std::runtime_error("couldn't open directory");
return 0;
}
}
Expand All @@ -347,8 +328,7 @@ class SintelOpticalFlow {
sceneNames.end()) {
selectedScene = sceneName;
} else {
std::cout << "ERR:Scene with name (" << sceneName
<< ") was not found" << std::endl;
throw std::runtime_error("Scene " + sceneName + " was not found");
}
}

Expand All @@ -375,12 +355,10 @@ class SintelOpticalFlow {
* @param L left image
* @param R right image
*
* @return The bw.
*/
int getBW(int id, ndb::Buffer<uint8_t>& L, ndb::Buffer<uint8_t>& R) {
int err1 = L.readPNG(makeFramePath(cleanDir, selectedScene, id));
int err2 = R.readPNG(makeFramePath(cleanDir, selectedScene, id + 1));
return err1 | err2;
void getBW(int id, ndb::Buffer<uint8_t>& L, ndb::Buffer<uint8_t>& R) {
L.readPNG(makeFramePath(cleanDir, selectedScene, id));
R.readPNG(makeFramePath(cleanDir, selectedScene, id + 1));
}

/**
Expand All @@ -390,12 +368,10 @@ class SintelOpticalFlow {
* @param L left image
* @param R right image
*
* @return 0 if success
*/
int getRGB(int id, ndb::Buffer<uint8_t>& L, ndb::Buffer<uint8_t>& R) {
int err1 = L.readPNG(makeFramePath(cleanDir, selectedScene, id));
int err2 = R.readPNG(makeFramePath(cleanDir, selectedScene, id + 1));
return err1 | err2;
void getRGB(int id, ndb::Buffer<uint8_t>& L, ndb::Buffer<uint8_t>& R) {
L.readPNG(makeFramePath(cleanDir, selectedScene, id));
R.readPNG(makeFramePath(cleanDir, selectedScene, id + 1));
}

/**
Expand All @@ -407,7 +383,7 @@ class SintelOpticalFlow {
*
* @return 0 if success
*/
int getFlow(int id, Eigen::MatrixXd& uMat, Eigen::MatrixXd& vMat) {
void getFlow(int id, Eigen::MatrixXd& uMat, Eigen::MatrixXd& vMat) {
std::ostringstream os;
os << std::setw(4) << std::setfill('0') << id;
std::string filename =
Expand All @@ -422,8 +398,7 @@ class SintelOpticalFlow {
uint8_t* fileptr = buf;
size_t res = fread(buf, sizeof(uint8_t), length, readFile);
if (res != length) {
cout << "Read error" << endl;
return 1;
throw std::runtime_error("Could not read flow file");
}
fclose(readFile);

Expand All @@ -440,7 +415,9 @@ class SintelOpticalFlow {
uMat.resize(width, height);
vMat.resize(width, height);

if (tag != 202021.25) cout << "TAG not found" << endl;
if (tag != 202021.25) {
throw std::runtime_error("Invalid .flo file (tag not found)");
}
// Read in flow data
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Expand All @@ -454,18 +431,16 @@ class SintelOpticalFlow {
}
}
delete[] fileptr;
return 0;
}
/**
* @brief Gets the occlusion map
*
* @param[in] id The image id
* @param O image the occlusion map
*
* @return 0 if success.
*/
int getOcclusion(int id, ndb::Buffer<uint8_t>& O) {
return O.readPNG(makeFramePath(oclDir, selectedScene, id));
void getOcclusion(int id, ndb::Buffer<uint8_t>& O) {
O.readPNG(makeFramePath(oclDir, selectedScene, id));
}
/**
* @brief Gets the invalid pixel map (out of frame)
Expand All @@ -475,8 +450,8 @@ class SintelOpticalFlow {
*
* @return The invalid.
*/
int getInvalid(int id, ndb::Buffer<uint8_t>& I) {
return I.readPNG(makeFramePath(invDir, selectedScene, id));
void getInvalid(int id, ndb::Buffer<uint8_t>& I) {
I.readPNG(makeFramePath(invDir, selectedScene, id));
}

/**
Expand Down
Loading