Skip to content

Commit

Permalink
Corrects DCT padding. This fixes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Mack committed Sep 28, 2016
1 parent 04da911 commit 8196dec
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 42 deletions.
1 change: 1 addition & 0 deletions include/openCVHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace vdff {
}

std::string getOSSeparator();
bool areImagesEquallySized(const std::vector<std::string>& imgFileNames, bool inGrayScale);
std::vector<std::string> getAllImagesFromFolder(const char *dirname, int skipNthPicture=1);
}

Expand Down
91 changes: 50 additions & 41 deletions src/DataPreparator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <DataPreparator.cuh>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <openCVHelpers.h>

#include <cuda.h>
Expand Down Expand Up @@ -64,16 +65,20 @@ namespace vdff {
}
}

void DataPreparator::determineSharpnessFromAllImagesSingleStream(const vector<string> &imgFileNames, const Mat& firstImage,
int paddingTop, int paddingBottom, int paddingLeft, int paddingRight,
size_t nrPixels, const int diffW, const int diffH,bool grayscale) {
cout << "Executing with a single stream" << endl;
Mat curImg = firstImage.clone();
int numberChannelsImage = curImg.channels();
void DataPreparator::determineSharpnessFromAllImagesSingleStream(const vector<string> &imgFileNames, const Mat& firstImage,
int paddingTop, int paddingBottom, int paddingLeft, int paddingRight,
size_t nrPixels, const int diffW, const int diffH,bool grayscale) {
cout << "Executing with a single stream" << endl;
Mat curImg = firstImage.clone();
int numberChannelsImage = curImg.channels();

size_t nrBytes = nrPixels * sizeof(float);

float *l_img = new float[nrPixels];
if (l_img == NULL) {
throw std::runtime_error("could not allocate memory for l_img");
}

float *d_img;
cudaMalloc(&d_img, nrBytes); CUDA_CHECK;

Expand Down Expand Up @@ -106,17 +111,15 @@ void DataPreparator::determineSharpnessFromAllImagesSingleStream(const vector<st
cout << "\r" << flush;
cout << "Determining sharpness from picture " << (i+1) << " from " << info.nrImgs;

if (i != 0) {
imgFile = imgFileNames[i];
curImg = openCVHelpers::imreadFloat(imgFileNames[i],grayscale);
}
// check if we got the same size, before the (possible) padding!
assert(firstImage.cols == curImg.cols && firstImage.rows == curImg.rows && firstImage.channels() == curImg.channels());

// pad the image if we need to, to guarantee that DCT is possible
if (diffW != 0 || diffH != 0)
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);

if (i != 0) {
imgFile = imgFileNames[i];
curImg = openCVHelpers::imreadFloat(imgFileNames[i],grayscale);

// pad the image if we need to, to guarantee that DCT is possible
if (diffW != 0 || diffH != 0) {
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);
}
}
openCVHelpers::convert_mat_to_layered(l_img, curImg);

cudaMemcpy(d_img, l_img, nrBytes, cudaMemcpyHostToDevice); CUDA_CHECK;
Expand All @@ -129,11 +132,11 @@ void DataPreparator::determineSharpnessFromAllImagesSingleStream(const vector<st
cudaFree(d_sharpnessCurImg);

delete[] l_img;
}
}

void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector<string> &imgFileNames, const Mat& firstImage,
int paddingTop, int paddingBottom, int paddingLeft, int paddingRight,
size_t nrPixels, const int diffW, const int diffH, bool grayscale) {
void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector<string> &imgFileNames, const Mat& firstImage,
int paddingTop, int paddingBottom, int paddingLeft, int paddingRight,
size_t nrPixels, const int diffW, const int diffH, bool grayscale) {
cout << "Executing with 2 streams" << endl;
Mat curImg = firstImage.clone();
int numberChannelsImage = curImg.channels();
Expand Down Expand Up @@ -178,48 +181,48 @@ void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector
cout << "Determining sharpness from picture " << (i+1) << " from " << info.nrImgs;

if (i != 0) {
imgFile = imgFileNames[i];
curImg = openCVHelpers::imreadFloat(imgFileNames[i],grayscale);
imgFile = imgFileNames[i];
curImg = openCVHelpers::imreadFloat(imgFileNames[i],grayscale);
}

// check if we got the same size, before the (possible) padding!
assert(firstImage.cols == curImg.cols && firstImage.rows == curImg.rows && firstImage.channels() == curImg.channels());

// pad the image if we need to, to guarantee that DCT is possible
if (diffW != 0 || diffH != 0)
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);

openCVHelpers::convert_mat_to_layered(l_img0, curImg);

size_t iPlusOne = i+1;
bool isIPlusOneValid = iPlusOne < info.nrImgs;

if (isIPlusOneValid) {
imgFile = imgFileNames[iPlusOne];
curImg = openCVHelpers::imreadFloat(imgFileNames[iPlusOne],grayscale);
imgFile = imgFileNames[iPlusOne];
curImg = openCVHelpers::imreadFloat(imgFileNames[iPlusOne],grayscale);

// check if we got the same size, before the (possible) padding!
assert(firstImage.cols == curImg.cols && firstImage.rows == curImg.rows && firstImage.channels() == curImg.channels());
// check if we got the same size, before the (possible) padding!
assert(firstImage.cols == curImg.cols && firstImage.rows == curImg.rows && firstImage.channels() == curImg.channels());

// pad the image if we need to, to guarantee that DCT is possible
if (diffW != 0 || diffH != 0)
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);
// pad the image if we need to, to guarantee that DCT is possible
if (diffW != 0 || diffH != 0)
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);

openCVHelpers::convert_mat_to_layered(l_img1, curImg);
openCVHelpers::convert_mat_to_layered(l_img1, curImg);
}

cudaMemcpyAsync(d_img0, l_img0, nrBytes, cudaMemcpyHostToDevice, stream0); CUDA_CHECK;
if (isIPlusOneValid)
cudaMemcpyAsync(d_img1, l_img1, nrBytes, cudaMemcpyHostToDevice, stream1); CUDA_CHECK;
cudaMemcpyAsync(d_img1, l_img1, nrBytes, cudaMemcpyHostToDevice, stream1); CUDA_CHECK;

cudaComputeMLAP(gridSize, blockSize, d_img0, d_sharpnessCurImg0, info.w, info.h, numberChannelsImage, 1, stream0); CUDA_CHECK;
if (isIPlusOneValid)
cudaComputeMLAP(gridSize, blockSize, d_img1, d_sharpnessCurImg1, info.w, info.h, numberChannelsImage, 1, stream1); CUDA_CHECK;
cudaComputeMLAP(gridSize, blockSize, d_img1, d_sharpnessCurImg1, info.w, info.h, numberChannelsImage, 1, stream1); CUDA_CHECK;

copyMLAPIntoPageLockedMemory(d_sharpnessCurImg0, i, stream0);

if (isIPlusOneValid)
copyMLAPIntoPageLockedMemory(d_sharpnessCurImg1, iPlusOne, stream1);
copyMLAPIntoPageLockedMemory(d_sharpnessCurImg1, iPlusOne, stream1);
}
cudaStreamSynchronize(stream0); CUDA_CHECK;
cudaStreamSynchronize(stream1); CUDA_CHECK;
Expand All @@ -244,7 +247,10 @@ void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector
size_t nrImgs = imgFileNames.size();
string imgFile = imgFileNames[0];

cout << "loading img: " << imgFileNames[0] << endl;
Mat curImg = openCVHelpers::imreadFloat(imgFileNames[0],grayscale);
cout << "\tdone" << endl;

int w = curImg.cols;
int h = curImg.rows;
int nc = curImg.channels();
Expand All @@ -263,8 +269,9 @@ void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector
paddingBottom += diffH % 2 == 1;
paddingRight += diffW % 2 == 1;

if (diffW != 0 || diffH != 0)
if (diffW != 0 || diffH != 0) {
copyMakeBorder(curImg, curImg, paddingTop, paddingBottom, paddingLeft, paddingRight, BORDER_REPLICATE);
}

// fill info struct
info.w = curImg.cols;
Expand All @@ -273,20 +280,22 @@ void DataPreparator::determineSharpnessFromAllImagesMultipleStreams(const vector
// ATTENTION: we have only one channel in the sharpness image!
info.nc = 1;

size_t nrPixels = static_cast<size_t>(w*h*nc);
size_t nrPixels = static_cast<size_t>(curImg.cols*curImg.rows*curImg.channels());

// check if the graphics device can handle overlaps --> no: only use one stream (default), else: do it with multiple streams
// (at the moment 2)
if (!deviceProperties.deviceOverlap || !usePageLockedMemory) {
cout << "determine sharpness - single stream" << endl;
useMultipleStreams = false;
determineSharpnessFromAllImagesSingleStream(imgFileNames, curImg,
paddingTop, paddingBottom, paddingLeft, paddingRight,
nrPixels, diffW, diffH,grayscale);
nrPixels, diffW, diffH,grayscale);
} else {
useMultipleStreams = true;
useMultipleStreams = true;
cout << "determine sharpness - multi stream" << endl;
determineSharpnessFromAllImagesMultipleStreams(imgFileNames, curImg,
paddingTop, paddingBottom, paddingLeft, paddingRight,
nrPixels, diffW, diffH,grayscale);
nrPixels, diffW, diffH,grayscale);
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/openCVHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ namespace vdff {
#endif
}

bool areImagesEquallySized(const vector<string>& imgFileNames, bool inGrayScale) {
Mat firstImg = openCVHelpers::imreadFloat(imgFileNames[0], inGrayScale);
size_t rows = firstImg.rows;
size_t cols = firstImg.cols;
size_t nrChannels = firstImg.channels();

for (size_t i = 1; i < imgFileNames.size(); ++i) {
Mat tmp = openCVHelpers::imreadFloat(imgFileNames[i], inGrayScale);
if (tmp.rows != rows ||
tmp.cols != cols ||
tmp.channels() != nrChannels) {
return false;
}
}
return true;
}
vector<string> getAllImagesFromFolder(const char *dirname, int useNthPicture) {
DIR *dir = NULL;
struct dirent *entry;
Expand Down Expand Up @@ -392,7 +408,6 @@ Mat imreadFloat(string filename,bool grayscale){

cv::Mat original = imread(filename,flags);
//cout<<endl;
imgInfo(original);
convertToFloat(original);
//imgInfo(original);
return original;
Expand Down
8 changes: 8 additions & 0 deletions src/vdff.cu
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ int main(int argc, char **argv) {
}
printGeneralParameters(params);

bool areImagesOK = Utils::areImagesEquallySized(Utils::getAllImagesFromFolder(params.folderPath.c_str(),
params.useNthPicture),
params.grayscale);
if (!areImagesOK) {
cerr << "\nThe provided images do not have equal size, but we require that - aborting..." << endl;
exit(1);
}

cout << "\n================================== Executing =======================================" << endl;
Utils::memprint();
cout<<"calling cudaDeviceReset to try to free GPU memory"<<endl;
Expand Down

0 comments on commit 8196dec

Please sign in to comment.