Skip to content

Commit

Permalink
support for both opencv versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vinay0410 committed Aug 4, 2018
1 parent 16d2c67 commit 629fdb7
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
#include <boost/algorithm/string/erase.hpp>
#include <glog/logging.h>

#if CV_MAJOR_VERSION == 2

namespace cv {
#define COLOR_RGB2BGR CV_RGB2BGR
}


#endif


RecorderReader::RecorderReader(const std::string &colorImagesPath, const std::string &depthImagesPath):DatasetReader(true), colorPath(colorImagesPath), depthPath(depthImagesPath) {
Expand Down Expand Up @@ -89,7 +97,7 @@ bool RecorderReader::getNextSample(Sample &sample) {

cv::Mat colorImage= cv::imread(getPathByIndex(this->colorPath,closest(colorIndexes,indexValue),this->syncedData?"-rgb":""));
// if (!this->syncedData)
cv::cvtColor(colorImage,colorImage,CV_RGB2BGR);
cv::cvtColor(colorImage,colorImage,cv::COLOR_RGB2BGR);

sample.setColorImage(colorImage);
sample.setDepthImage(getPathByIndex(this->depthPath,indexValue,this->syncedData?"-depth":""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
#include <Utils/JsonHelper.h>
#include <Utils/DepthUtils.h>

#if CV_MAJOR_VERSION == 2

namespace cv {
#define IMREAD_ANYDEPTH CV_LOAD_IMAGE_ANYDEPTH

}
#endif


PrincetonDatasetReader::PrincetonDatasetReader(const std::string &path, const std::string &classNamesFile,const bool imagesRequired):DatasetReader(imagesRequired) {
this->classNamesFile=classNamesFile;
Expand Down Expand Up @@ -49,7 +57,7 @@ bool PrincetonDatasetReader::appendDataset(const std::string &datasetPath, const
ssDepth << "d-" << depthTimestamp[i] << "-" << depthFrameID[i] << ".png";
std::string depthImagePath = PathHelper::concatPaths(datasetPath, "depth");
depthImagePath = PathHelper::concatPaths(depthImagePath, ssDepth.str());
cv::Mat depthImage = cv::imread(depthImagePath, CV_LOAD_IMAGE_ANYDEPTH);
cv::Mat depthImage = cv::imread(depthImagePath, cv::IMREAD_ANYDEPTH);
cv::Mat ownDepthImage;
DepthUtils::mat16_to_ownFormat(depthImage,ownDepthImage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
#include <Utils/DepthUtils.h>
#include <glog/logging.h>


#if CV_MAJOR_VERSION == 2

namespace cv {

#define IMREAD_ANYDEPTH CV_LOAD_IMAGE_ANYDEPTH

}

#endif

SpinelloDatasetReader::SpinelloDatasetReader(const std::string &path,const std::string& classNamesFile,const bool imagesRequired):DatasetReader(imagesRequired) {
this->classNamesFile=classNamesFile;
appendDataset(path);
Expand Down Expand Up @@ -60,7 +71,7 @@ bool SpinelloDatasetReader::appendDataset(const std::string &datasetPath, const
std::string colorImagePath=datasetPath + "/" + "rgb" + "/" + imageID + ".ppm";
std::string depthImagePath=datasetPath + "/" + "depth" + "/" + imageID + ".pgm";
cv::Mat colorImage= cv::imread(colorImagePath);
cv::Mat depthImage= cv::imread(depthImagePath, CV_LOAD_IMAGE_ANYDEPTH);
cv::Mat depthImage= cv::imread(depthImagePath, cv::IMREAD_ANYDEPTH);

cv::Mat ownDepthImage;
//DepthUtils::mat16_to_ownFormat(depthImage,ownDepthImage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void DetectionsValidator::validate(const cv::Mat& colorImage,const cv::Mat& dept
for (auto it= detections.begin(), end = detections.end(); it != end; ++it){
int idx= (int)std::distance(detections.begin(),it);
cv::Scalar color( 150);
cv::drawContours( mask, detections, idx, color, CV_FILLED, 8);
cv::drawContours( mask, detections, idx, color, -1, 8);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void ContourRegions::drawRegions(cv::Mat &image) {
cv::Scalar color(255);
std::vector<std::vector<cv::Point>> contours;
contours.push_back(it->region);
cv::drawContours(mask, contours, 0, color, CV_FILLED, 8);
cv::drawContours(mask, contours, 0, color, -1, 8);
std::vector<cv::Mat> channels;
cv::split(image, channels);
cv::Mat colorMask(image.size(), CV_8UC1, cv::Scalar(255));
Expand Down
13 changes: 13 additions & 0 deletions DeepLearningSuite/DeepLearningSuiteLib/Regions/Regions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@
#include <opencv2/opencv.hpp>
#include <boost/shared_ptr.hpp>


#if CV_MAJOR_VERSION == 2

namespace cv { // Fix for different opencv versions

#define RETR_CCOMP CV_RETR_CCOMP
#define CHAIN_APPROX_SIMPLE CV_CHAIN_APPROX_SIMPLE

}

#endif


struct Regions{
Regions(){};
virtual void saveJson(const std::string& outPath)=0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void RleRegions::drawRegions(cv::Mat &image) {
color = cv::Scalar(2,166,101);
} else {
color = cv::Scalar((unsigned int)(distribution(generator)*170), (unsigned int)(distribution(generator)*170), (unsigned int)(distribution(generator)*170));
cv::findContours( mask.clone(), contours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );
cv::findContours( mask.clone(), contours, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE, cv::Point(0, 0) );

}

Expand Down
73 changes: 48 additions & 25 deletions DeepLearningSuite/libs/utils/colorspaces/imagecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
#include <unistd.h>
#include <stdexcept>

#if CV_MAJOR_VERSION == 2

namespace cv { // Fix for different opencv versions

#define COLOR_BGR2RGB CV_BGR2RGB
#define COLOR_RGB2BGR CV_RGB2BGR
#define COLOR_RGB2GRAY CV_RGB2GRAY
#define COLOR_RGB2YCrCb CV_RGB2YCrCb
#define COLOR_RGB2HSV CV_RGB2HSV
#define COLOR_RGB2YCrCb CV_RGB2YCrCb
#define COLOR_YCrCb2RGB CV_YCrCb2RGB
#define COLOR_GRAY2RGB CV_GRAY2RGB
#define COLOR_HSV2RGB CV_HSV2RGB
#define COLOR_YCrCb2RGB CV_YCrCb2RGB
#define COLOR_RGB2YCrCb CV_RGB2YCrCb



}

#endif


namespace colorspaces {
Image::Format::Format(const std::string name, const int id, const int cvType, imageCtor ctor, imageCvt cvt)
: name(name), id(id), cvType(cvType),ctor(ctor),cvt(cvt) {}
Expand Down Expand Up @@ -40,17 +63,17 @@ namespace colorspaces {
return ctor(width,height,data);
return 0;
}

Image& Image::convert(Image& dst) const throw(NoConversion){
//std::cerr << "colorspaces: imagecv: convert: " << *_format << "->" << *dst._format << std::endl;
//std::cerr << "colorspaces: imagecv: convert: " << *_format << "->" << *dst._format << std::endl;
return _format->cvt(*this,dst);
}

Image Image::clone() const{
Image copy(cv::Mat::clone(),_format);
return copy;
}


//static definitions
const Image::FormatPtr Image::FORMAT_NONE = Image::Format::createFormat("NONE",0,0,0);
Expand Down Expand Up @@ -84,7 +107,7 @@ namespace colorspaces {

ImageRGB8::ImageRGB8(const int width, const int height)
: Image(width,height,FORMAT_RGB8) {}

ImageRGB8::ImageRGB8(const int width, const int height, void *const data)
: Image(width,height,FORMAT_RGB8,data) {}

Expand All @@ -98,13 +121,13 @@ namespace colorspaces {
if (stat(filename.c_str(),&s) == -1)
throw std::runtime_error(filename+" not found");
cv::Mat readImage(cv::imread(filename));//BGR
cv::cvtColor(readImage,readImage,CV_BGR2RGB);
cv::cvtColor(readImage,readImage,cv::COLOR_BGR2RGB);
return ImageRGB8(Image(readImage,FORMAT_RGB8));
}

bool ImageRGB8::write(const std::string& filename,const std::vector<int>& params){
cv::Mat bgrImage(this->size(),this->type());
cv::cvtColor(*this,bgrImage,CV_RGB2BGR);
cv::cvtColor(*this,bgrImage,cv::COLOR_RGB2BGR);
return cv::imwrite(filename, bgrImage, params);
}

Expand Down Expand Up @@ -134,7 +157,7 @@ namespace colorspaces {
void ImageRGB8::toGRAY8(Image& dst) const throw(Image::FormatMismatch){
if (dst.format() != ImageGRAY8::FORMAT_GRAY8)
throw Image::FormatMismatch("FORMAT_GRAY8 required for dst");
cv::cvtColor(*this,dst,CV_RGB2GRAY);
cv::cvtColor(*this,dst,cv::COLOR_RGB2GRAY);
}

void ImageRGB8::toYUY2(Image& dst) const throw(Image::FormatMismatch){
Expand All @@ -145,7 +168,7 @@ namespace colorspaces {

cv::Mat_<cv::Vec3b> ycrcb(dst.height,dst.width,dst.type());//YUV444 previous conversion
cv::Mat_<cv::Vec2b> yuy2(dst);
cv::cvtColor(*this,ycrcb,CV_RGB2YCrCb);
cv::cvtColor(*this,ycrcb,cv::COLOR_RGB2YCrCb);

for (int i=0; i < height; i++){
for (int j=0; j < width; j+=2){//two pixels each loop
Expand All @@ -160,13 +183,13 @@ namespace colorspaces {
void ImageRGB8::toHSV8(Image& dst) const throw(FormatMismatch){
if (dst.format() != ImageHSV8::FORMAT_HSV8)
throw Image::FormatMismatch("FORMAT_HSV8 required for dst");
cv::cvtColor(*this,dst,CV_RGB2HSV);
cv::cvtColor(*this,dst,cv::COLOR_RGB2HSV);
}

void ImageRGB8::toYCRCB(Image& dst) const throw(FormatMismatch){
if (dst.format() != ImageYCRCB::FORMAT_YCRCB)
throw Image::FormatMismatch("FORMAT_YCRCB required for dst");
cv::cvtColor(*this,dst,CV_RGB2YCrCb);
cv::cvtColor(*this,dst,cv::COLOR_RGB2YCrCb);
}

Image* ImageRGB8::createInstance(const int width, const int height, void *const data){
Expand All @@ -178,7 +201,7 @@ namespace colorspaces {

ImageYUY2::ImageYUY2(const int width, const int height)
: Image(width,height,FORMAT_YUY2) {}

ImageYUY2::ImageYUY2(const int width, const int height, void *const data)
: Image(width,height,FORMAT_YUY2,data) {}

Expand Down Expand Up @@ -221,8 +244,8 @@ namespace colorspaces {

ImageYCRCB ycrcbImg(dst.height,dst.width);//YCRCB previous conversion
toYCRCB(ycrcbImg);
cv::cvtColor(ycrcbImg,dst,CV_YCrCb2RGB);

cv::cvtColor(ycrcbImg,dst,cv::COLOR_YCrCb2RGB);
}

void ImageYUY2::toYCRCB(Image& dst) const throw(FormatMismatch){
Expand All @@ -249,10 +272,10 @@ namespace colorspaces {
else
return new ImageYUY2(width,height);
}

ImageGRAY8::ImageGRAY8(const int width, const int height)
: Image(width,height,FORMAT_GRAY8) {}

ImageGRAY8::ImageGRAY8(const int width, const int height, void *const data)
: Image(width,height,FORMAT_GRAY8,data) {}

Expand Down Expand Up @@ -292,7 +315,7 @@ namespace colorspaces {
if (dst.format() != ImageRGB8::FORMAT_RGB8)
throw Image::FormatMismatch("FORMAT_RGB8 required for dst");

cv::cvtColor(*this,dst,CV_GRAY2RGB);
cv::cvtColor(*this,dst,cv::COLOR_GRAY2RGB);
}

void ImageGRAY8::toYUY2(Image& dst) const throw(Image::FormatMismatch){
Expand All @@ -316,7 +339,7 @@ namespace colorspaces {

ImageHSV8::ImageHSV8(const int width, const int height)
: Image(width,height,FORMAT_HSV8) {}

ImageHSV8::ImageHSV8(const int width, const int height, void *const data)
: Image(width,height,FORMAT_HSV8,data) {}

Expand All @@ -343,7 +366,7 @@ namespace colorspaces {
if (dst.format() != ImageRGB8::FORMAT_RGB8)
throw Image::FormatMismatch("FORMAT_RGB8 required for dst");

cv::cvtColor(*this,dst,CV_HSV2RGB);
cv::cvtColor(*this,dst,cv::COLOR_HSV2RGB);
}

Image* ImageHSV8::createInstance(const int width, const int height, void *const data){
Expand All @@ -355,7 +378,7 @@ namespace colorspaces {

ImageYCRCB::ImageYCRCB(const int width, const int height)
: Image(width,height,FORMAT_YCRCB) {}

ImageYCRCB::ImageYCRCB(const int width, const int height, void *const data)
: Image(width,height,FORMAT_YCRCB,data) {}

Expand All @@ -381,8 +404,8 @@ namespace colorspaces {
void ImageYCRCB::toRGB8(Image& dst) const throw(FormatMismatch){
if (dst.format() != ImageRGB8::FORMAT_RGB8)
throw Image::FormatMismatch("FORMAT_RGB8 required for dst");
cv::cvtColor(*this,dst,CV_YCrCb2RGB);

cv::cvtColor(*this,dst,cv::COLOR_YCrCb2RGB);
}

Image* ImageYCRCB::createInstance(const int width, const int height, void *const data){
Expand All @@ -394,7 +417,7 @@ namespace colorspaces {

ImageNV21::ImageNV21(const int width, const int height)
: Image(width,height,FORMAT_NV21) {}

ImageNV21::ImageNV21(const int width, const int height, void *const data)
: Image(width,height,FORMAT_NV21,data) {}

Expand Down Expand Up @@ -435,8 +458,8 @@ namespace colorspaces {
if ((dst.width % 2 != 0) || (this->width % 2 != 0))
throw Image::FormatMismatch("src and dst images have to have even number of columns");

//cv::cvtColor(*this,dst,CV_YUV420sp2RGB);
//cv::cvtColor(*this,dst,cv::COLOR_YUV420sp2RGB);

unsigned char *rgb = (unsigned char *)dst.data;
unsigned char *yuv = (unsigned char *)this->data,
*yuv_y = yuv, *yuv_uv = yuv + dst.width * dst.height;
Expand Down Expand Up @@ -464,7 +487,7 @@ namespace colorspaces {
throw Image::FormatMismatch("FORMAT_YCRCB required for dst");
ImageYCRCB rgbImg(dst.height,dst.width);
toRGB8(rgbImg);
cv::cvtColor(rgbImg,dst,CV_RGB2YCrCb);
cv::cvtColor(rgbImg,dst,cv::COLOR_RGB2YCrCb);
}

Image* ImageNV21::createInstance(const int width, const int height, void *const data){
Expand Down

0 comments on commit 629fdb7

Please sign in to comment.