Commit f53e0e64 authored by Cédric Traizet's avatar Cédric Traizet

Merge branch 'compatibility_opencv4' into 'develop'

Compatibility with OpenCV 4

See merge request !593
parents 96347f4b f9aa3713
Pipeline #2637 passed with stages
in 99 minutes and 42 seconds
......@@ -28,11 +28,11 @@ if(OpenCV_DIR)
if(OPENCV_SEARCH_PATH)
find_path(
opencv_INCLUDE_DIR
opencv/cv.h
opencv2/opencv.hpp
PATHS "${OPENCV_SEARCH_PATH}"
#no additional paths are added to the search if OpenCV_DIR
NO_DEFAULT_PATH
PATH_SUFFIXES "include"
PATH_SUFFIXES "include" "include/opencv4"
DOC "The directory where opencv/cv.h is installed")
endif()
endif()
......@@ -40,17 +40,12 @@ endif()
if(NOT opencv_INCLUDE_DIR)
find_path(
opencv_INCLUDE_DIR
opencv/cv.h
opencv2/opencv.hpp
PATHS "${OpenCV_DIR}"
PATH_SUFFIXES "include"
PATH_SUFFIXES "include" "include/opencv4"
DOC "The directory where opencv/cv.h is installed")
endif()
if(NOT EXISTS ${opencv_INCLUDE_DIR}/opencv2/opencv.hpp)
message(FATAL_ERROR "${opencv_INCLUDE_DIR}/opencv2/opencv.hpp does not exists. "
"Make sure you have opencv 2.3 or higher. We had searched in ${OPENCV_SEARCH_PATHS}")
endif()
set(opencv_core_NAMES opencv_core)
set(opencv_ml_NAMES opencv_ml)
......@@ -61,7 +56,7 @@ if (opencv_INCLUDE_DIR)
if(NOT OpenCV_VERSION)
file(READ "${opencv_INCLUDE_DIR}/opencv2/core/version.hpp" _header_content)
# detect the type of version file (2.3.x , 2.4.x or 3.x)
# detect the type of version file (2.3.x , 2.4.x, 3.x or 4.x)
string(REGEX MATCH ".*# *define +CV_VERSION_EPOCH +([0-9]+).*" has_epoch ${_header_content})
string(REGEX MATCH ".*# *define +CV_MAJOR_VERSION +([0-9]+).*" has_old_major ${_header_content})
string(REGEX MATCH ".*# *define +CV_MINOR_VERSION +([0-9]+).*" has_old_minor ${_header_content})
......@@ -109,6 +104,10 @@ endif()
if(NOT OPENCV_SEARCH_PATH)
get_filename_component(OPENCV_SEARCH_PATH "${opencv_INCLUDE_DIR}" PATH)
# include dir is include/opencv4 in v4 UNIX
if(UNIX AND OpenCV_VERSION_MAJOR EQUAL 4)
get_filename_component(OPENCV_SEARCH_PATH "${OPENCV_SEARCH_PATH}" PATH)
endif()
endif()
# Prefer the static library.
......
......@@ -104,6 +104,10 @@ public:
OTB_CV_WRAP_CSTREF_GET(std::vector<cv::ml::DTrees::Split>, Splits)
OTB_CV_WRAP_CSTREF_GET(std::vector<int>, Subsets)
#ifdef OTB_OPENCV_4
virtual void getVotes(cv::InputArray samples, cv::OutputArray results, int flags) const override;
#endif
virtual cv::Mat getVarImportance() const override;
virtual cv::String getDefaultName () const override;
......
......@@ -210,7 +210,7 @@ private:
int m_MaxIter;
double m_Epsilon;
CvMat* m_CvMatOfLabels;
cv::Mat m_MatrixOfLabels;
MapOfLabelsType m_MapOfLabels;
};
......
......@@ -46,8 +46,7 @@ NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::NeuralNetworkMachi
m_RegPropDWMin(FLT_EPSILON),
m_TermCriteriaType(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS),
m_MaxIter(1000),
m_Epsilon(0.01),
m_CvMatOfLabels(nullptr)
m_Epsilon(0.01)
{
this->m_ConfidenceIndex = true;
this->m_IsRegressionSupported = true;
......@@ -59,7 +58,6 @@ NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::~NeuralNetworkMach
#ifndef OTB_OPENCV_3
delete m_ANNModel;
#endif
cvReleaseMat(&m_CvMatOfLabels);
}
/** Sets the topology of the NN */
......@@ -107,26 +105,17 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::LabelsToMat(c
}
unsigned int nbClasses = m_MapOfLabels.size();
typename MapOfLabelsType::iterator itMapOfLabels = m_MapOfLabels.begin();
unsigned itLabel = 0;
for (; itMapOfLabels != m_MapOfLabels.end(); ++itMapOfLabels)
{
classLabel = itMapOfLabels->first;
m_MapOfLabels[classLabel] = itLabel;
if (itLabel == 0)
{
if (m_CvMatOfLabels)
{
cvReleaseMat(&m_CvMatOfLabels);
}
m_CvMatOfLabels = cvCreateMat(1, nbClasses, CV_32SC1);
}
m_CvMatOfLabels->data.i[itLabel] = classLabel;
m_MatrixOfLabels = cv::Mat(1,nbClasses, CV_32FC1);
unsigned int itLabel = 0;
for (auto& kv : m_MapOfLabels)
{
classLabel = kv.first;
kv.second = itLabel;
m_MatrixOfLabels.at<float>(0,itLabel) = classLabel;
++itLabel;
}
}
// Allocate CvMat
// Sample index
unsigned int sampleIdx = 0;
labelSampleIt = labels->Begin();
......@@ -257,9 +246,8 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
// MODE CLASSIFICATION : find the highest response
float secondResponse = -1e10;
target[0] = m_CvMatOfLabels->data.i[0];
unsigned int nbClasses = m_CvMatOfLabels->cols;
target[0] = m_MatrixOfLabels.at<TOutputValue>(0);
unsigned int nbClasses = m_MatrixOfLabels.size[1];
for (unsigned itLabel = 1; itLabel < nbClasses; ++itLabel)
{
currentResponse = response.at<float> (0, itLabel);
......@@ -267,7 +255,7 @@ typename NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::TargetSam
{
secondResponse = maxResponse;
maxResponse = currentResponse;
target[0] = m_CvMatOfLabels->data.i[itLabel];
target[0] = m_MatrixOfLabels.at<TOutputValue>(itLabel);
}
else
{
......@@ -296,10 +284,9 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Save(const st
cv::FileStorage fs(filename, cv::FileStorage::WRITE);
fs << (name.empty() ? m_ANNModel->getDefaultName() : cv::String(name)) << "{";
m_ANNModel->write(fs);
if (m_CvMatOfLabels != nullptr)
if (!m_MatrixOfLabels.empty())
{
std::string labelsName("class_labels");
fs.writeObj(labelsName,m_CvMatOfLabels);
fs << "class_labels" << m_MatrixOfLabels;
}
fs << "}";
fs.release();
......@@ -316,9 +303,12 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Save(const st
}
m_ANNModel->write(fs, lname);
if (m_CvMatOfLabels != nullptr)
cvWrite(fs, "class_labels", m_CvMatOfLabels);
if (!m_MatrixOfLabels.empty())
{
// cvWrite can't write cv::Mat
auto tmpMat = CvMat(m_MatrixOfLabels);
cvWrite(fs, "class_labels", &tmpMat);
}
cvReleaseFileStorage(&fs);
#endif
}
......@@ -331,7 +321,7 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Load(const st
cv::FileStorage fs(filename, cv::FileStorage::READ);
cv::FileNode model_node(name.empty() ? fs.getFirstTopLevelNode() : fs[name]);
m_ANNModel->read(model_node);
m_CvMatOfLabels = (CvMat*)cvReadByName( *fs, *model_node, "class_labels" );
model_node["class_labels"] >> m_MatrixOfLabels;
fs.release();
#else
const char* lname = nullptr;
......@@ -357,8 +347,7 @@ void NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::Load(const st
}
m_ANNModel->read(*fs,*model_node);
m_CvMatOfLabels = (CvMat*)cvReadByName( *fs, *model_node, "class_labels" );
model_node["class_labels"] >> m_MatrixOfLabels;
fs.release();
#endif
}
......@@ -386,7 +375,6 @@ bool NeuralNetworkMachineLearningModel<TInputValue, TOutputValue>::CanReadFile(c
#endif
)
{
//std::cout << "Reading a " << CV_TYPE_NAME_ML_ANN_MLP << " model" << std::endl;
return true;
}
}
......
......@@ -197,6 +197,13 @@ cv::Mat CvRTreesWrapper::getVarImportance() const
return m_Impl->getVarImportance();
}
#ifdef OTB_OPENCV_4
void CvRTreesWrapper::getVotes(cv::InputArray samples, cv::OutputArray results, int flags) const
{
m_Impl->getVotes(samples, results, flags);
}
#endif
cv::String CvRTreesWrapper::getDefaultName () const
{
return m_Impl->getDefaultName();
......
......@@ -26,8 +26,15 @@ if("${OpenCV_VERSION}" VERSION_LESS "2.4.0")
"it may not be fully compatible with OTB")
endif()
if(OpenCV_VERSION_MAJOR EQUAL 3)
# OTB_OPENCV_3 is true for OpenCV 3 and 4
if(OpenCV_VERSION_MAJOR GREATER_EQUAL 3)
set(OTB_OPENCV_3 1)
else()
set(OTB_OPENCV_3 0)
endif()
if(OpenCV_VERSION_MAJOR EQUAL 4)
set(OTB_OPENCV_4 1)
else()
set(OTB_OPENCV_4 0)
endif()
......@@ -21,5 +21,6 @@
#define otb_opencv_api_h
#cmakedefine OTB_OPENCV_3
#cmakedefine OTB_OPENCV_4
#endif
......@@ -34,8 +34,8 @@ ADD_SUPERBUILD_CMAKE_VAR(OPENCV PNG_LIBRARY)
ExternalProject_Add(OPENCV
PREFIX OPENCV
URL "https://github.com/opencv/opencv/archive/3.4.1.tar.gz"
URL_MD5 a0b7a47899e67b3490ea31edc4f6e8e6
URL "https://github.com/opencv/opencv/archive/4.1.1.tar.gz"
URL_MD5 d9d3138d12e52bb0684f4a5862f6a46c
BINARY_DIR ${OPENCV_SB_BUILD_DIR}
INSTALL_DIR ${SB_INSTALL_PREFIX}
DOWNLOAD_DIR ${DOWNLOAD_LOCATION}
......
diff -burN opencv-3.4.1.orig/CMakeLists.txt opencv-3.4.1/CMakeLists.txt
--- opencv-3.4.1.orig/CMakeLists.txt Wed Sep 26 15:10:50 2018
+++ opencv-3.4.1/CMakeLists.txt Wed Sep 26 15:12:40 2018
@@ -373,7 +373,7 @@
diff -burN opencv.orig/CMakeLists.txt opencv/CMakeLists.txt
--- opencv.orig/CMakeLists.txt 2019-09-17 09:33:13.196155000 +0200
+++ opencv/CMakeLists.txt 2019-09-17 09:39:18.076164185 +0200
@@ -531,7 +531,7 @@
if(WIN32 AND CMAKE_HOST_SYSTEM_NAME MATCHES Windows)
if(DEFINED OpenCV_RUNTIME AND DEFINED OpenCV_ARCH)
- ocv_update(OpenCV_INSTALL_BINARIES_PREFIX "${OpenCV_ARCH}/${OpenCV_RUNTIME}/")
+ ocv_update(OpenCV_INSTALL_BINARIES_PREFIX "") #${OpenCV_ARCH}/${OpenCV_RUNTIME}/")
+ ocv_update(OpenCV_INSTALL_BINARIES_PREFIX "") #"${OpenCV_ARCH}/${OpenCV_RUNTIME}/")
else()
message(STATUS "Can't detect runtime and/or arch")
ocv_update(OpenCV_INSTALL_BINARIES_PREFIX "")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment