IMP logo
IMP Reference Guide  develop.d97d4ead1f,2024/11/21
The Integrative Modeling Platform
opencv_interface.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/opencv_interface.h
3  * \brief Interface with OpenCV
4  * Copyright 2007-2022 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_OPENCV_INTERFACE_H
8 #define IMPEM2D_OPENCV_INTERFACE_H
9 
10 #include <IMP/em2d/em2d_config.h>
12 
13 #if IMP_EM2D_HAS_OPENCV22 || IMP_EM2D_HAS_OPENCV3
14 #include "opencv2/core/core.hpp"
15 #include "opencv2/core/version.hpp"
16 #include "opencv2/imgproc/imgproc.hpp"
17 #include "opencv2/highgui/highgui.hpp"
18 #else
19 #include "opencv/cv.h"
20 #include "opencv/highgui.h"
21 #endif
22 
23 #include <iostream>
24 #include <cereal/access.hpp>
25 #include <cereal/cereal.hpp>
26 
27 IMPEM2D_BEGIN_NAMESPACE
28 
29 typedef cv::Mat_<double> cvDoubleMat;
30 typedef cv::MatIterator_<double> cvDoubleMatIterator;
31 typedef cv::MatConstIterator_<double> cvDoubleConstMatIterator;
32 
33 typedef cv::Mat_<int> cvIntMat;
34 typedef cv::MatIterator_<int> cvIntMatIterator;
35 
36 typedef cv::Point_<int> cvPixel;
37 typedef std::vector<cvPixel> cvPixels;
38 
39 //! Prints a OpenCV matrix
40 IMPEM2DEXPORT void show(const cv::Mat &m, std::ostream &out = std::cout);
41 
42 //! Quick and dirty way of writing a OpenCV matrix to a Spider image
43 IMPEM2DEXPORT void write_matrix(cv::Mat &m, std::string name);
44 
45 //! Show a Mat_
46 template <typename T>
47 void show(const cv::Mat_<T> &m, std::ostream &out = std::cout) {
48  for (int i = 0; i < m.rows; ++i) {
49  for (int j = 0; j < m.cols; ++j) {
50  out << m(i, j) << " ";
51  }
52  out << std::endl;
53  }
54  out << std::endl;
55 }
56 
57 IMPEM2D_END_NAMESPACE
58 
59 namespace cereal {
60  template<class Archive>
61  inline void serialize(Archive &ar, cv::Mat &m) {
62  int rows, cols, type;
63  bool continuous;
64 
65  if (std::is_base_of<cereal::detail::OutputArchiveBase, Archive>::value) {
66  rows = m.rows;
67  cols = m.cols;
68  type = m.type();
69  continuous = m.isContinuous();
70  }
71  ar(rows, cols, type, continuous);
72 
73  if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
74  m.create(rows, cols, type);
75  }
76 
77  if (continuous) {
78  size_t data_size = rows * cols * m.elemSize();
79  auto mat_data = cereal::binary_data(m.data, data_size);
80  ar(mat_data);
81  } else {
82  size_t row_size = cols * m.elemSize();
83  for (int i = 0; i < rows; ++i) {
84  auto row_data = cereal::binary_data(m.ptr(i), row_size);
85  ar(row_data);
86  }
87  }
88  }
89 }
90 
91 #endif /* IMPEM2D_OPENCV_INTERFACE_H */
2D transformations. Copyright 2007-2022 IMP Inventors. All rights reserved.
void write_matrix(cv::Mat &m, std::string name)
Quick and dirty way of writing a OpenCV matrix to a Spider image.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.