IMP logo
IMP Reference Guide  develop.eb1b99edaa,2026/06/25
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-2026 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 #include "opencv2/core/core.hpp"
14 #include "opencv2/core/version.hpp"
15 #include "opencv2/imgproc/imgproc.hpp"
16 #include "opencv2/highgui/highgui.hpp"
17 
18 #include <iostream>
19 #include <cereal/access.hpp>
20 #include <cereal/cereal.hpp>
21 
22 IMPEM2D_BEGIN_NAMESPACE
23 
24 typedef cv::Mat_<double> cvDoubleMat;
25 typedef cv::MatIterator_<double> cvDoubleMatIterator;
26 typedef cv::MatConstIterator_<double> cvDoubleConstMatIterator;
27 
28 typedef cv::Mat_<int> cvIntMat;
29 typedef cv::MatIterator_<int> cvIntMatIterator;
30 
31 typedef cv::Point_<int> cvPixel;
32 typedef std::vector<cvPixel> cvPixels;
33 
34 //! Prints a OpenCV matrix
35 IMPEM2DEXPORT void show(const cv::Mat &m, std::ostream &out = std::cout);
36 
37 //! Quick and dirty way of writing a OpenCV matrix to a Spider image
38 IMPEM2DEXPORT void write_matrix(cv::Mat &m, std::string name);
39 
40 //! Show a Mat_
41 template <typename T>
42 void show(const cv::Mat_<T> &m, std::ostream &out = std::cout) {
43  for (int i = 0; i < m.rows; ++i) {
44  for (int j = 0; j < m.cols; ++j) {
45  out << m(i, j) << " ";
46  }
47  out << std::endl;
48  }
49  out << std::endl;
50 }
51 
52 IMPEM2D_END_NAMESPACE
53 
54 namespace cereal {
55  template<class Archive>
56  inline void serialize(Archive &ar, cv::Mat &m) {
57  int rows, cols, type;
58  bool continuous;
59 
60  if (std::is_base_of<cereal::detail::OutputArchiveBase, Archive>::value) {
61  rows = m.rows;
62  cols = m.cols;
63  type = m.type();
64  continuous = m.isContinuous();
65  }
66  ar(rows, cols, type, continuous);
67 
68  if (std::is_base_of<cereal::detail::InputArchiveBase, Archive>::value) {
69  m.create(rows, cols, type);
70  }
71 
72  if (continuous) {
73  size_t data_size = rows * cols * m.elemSize();
74  auto mat_data = cereal::binary_data(m.data, data_size);
75  ar(mat_data);
76  } else {
77  size_t row_size = cols * m.elemSize();
78  for (int i = 0; i < rows; ++i) {
79  auto row_data = cereal::binary_data(m.ptr(i), row_size);
80  ar(row_data);
81  }
82  }
83  }
84 }
85 
86 #endif /* IMPEM2D_OPENCV_INTERFACE_H */
2D transformations. Copyright 2007-2026 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.