Scanner C++ API
opencv.h
1 /* Copyright 2016 Carnegie Mellon University, NVIDIA Corporation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/api/frame.h"
19 #include "scanner/util/common.h"
20 #include "scanner/api/kernel.h"
21 #include "scanner/engine/metadata.h"
22 #include "scanner/util/image.h"
23 
24 #include <opencv2/opencv.hpp>
25 
26 namespace scanner {
27 
28 inline int frame_to_cv_type(FrameType type, int channels = 1) {
29  int cv_type;
30  switch (type) {
31  case FrameType::U8: {
32  cv_type = CV_8U;
33  break;
34  }
35  case FrameType::U16: {
36  cv_type = CV_16U;
37  break;
38  }
39  case FrameType::F32: {
40  cv_type = CV_32F;
41  break;
42  }
43  case FrameType::F64: {
44  cv_type = CV_64F;
45  break;
46  }
47  }
48  return CV_MAKETYPE(cv_type, channels);
49 }
50 
51 inline FrameType cv_to_frame_type(int t) {
52  FrameType type;
53  switch (t) {
54  case CV_8U: {
55  type = FrameType::U8;
56  break;
57  }
58  case CV_16U: {
59  type = FrameType::U16;
60  break;
61  }
62  case CV_32F: {
63  type = FrameType::F32;
64  break;
65  }
66  case CV_64F: {
67  type = FrameType::F64;
68  break;
69  }
70  default: { LOG(FATAL) << "Unsupported OpenCV type: " << t; }
71  }
72  return type;
73 }
74 
75 inline FrameInfo mat_to_frame_info(const cv::Mat& mat) {
76  return FrameInfo(mat.rows, mat.cols, mat.channels(),
77  cv_to_frame_type(mat.depth()));
78 }
79 
80 inline Frame* mat_to_frame(const cv::Mat& mat) {
81  Frame* frame = new_frame(CPU_DEVICE, mat_to_frame_info(mat));
82  if (mat.isContinuous()) {
83  memcpy(frame->data, mat.data, frame->size());
84  } else {
85  u64 offset = 0;
86  u64 row_size = mat.cols * mat.elemSize();
87  for (int i = 0; i < mat.rows; ++i) {
88  memcpy(frame->data + offset, mat.data + i * mat.step, row_size);
89  offset += row_size;
90  }
91  }
92  return frame;
93 }
94 
95 inline cv::Mat frame_to_mat(const Frame* frame) {
96  return cv::Mat(frame->height(), frame->width(),
97  frame_to_cv_type(frame->type, frame->channels()), frame->data);
98 }
99 
100 inline cv::Mat frame_to_mat(Frame* frame) {
101  return cv::Mat(frame->height(), frame->width(),
102  frame_to_cv_type(frame->type, frame->channels()), frame->data);
103 }
104 
105 inline cv::Mat bytesToImage(u8* buf, const FrameInfo& metadata) {
106  return cv::Mat(metadata.height(), metadata.width(), CV_8UC3, buf);
107 }
108 
109 }
110 
111 #ifdef HAVE_CUDA
112 
113 #include "scanner/util/cuda.h"
114 #include <cuda_runtime.h>
115 #include <opencv2/core/cuda.hpp>
116 #include <opencv2/core/cuda_stream_accessor.hpp>
117 
118 namespace cvc = cv::cuda;
119 
120 namespace scanner {
121 
122 class InputFormat;
123 
124 inline cvc::GpuMat frame_to_gpu_mat(const Frame* frame) {
125  return cvc::GpuMat(frame->height(), frame->width(),
126  frame_to_cv_type(frame->type, frame->channels()),
127  frame->data);
128 }
129 
130 inline cvc::GpuMat frame_to_gpu_mat(Frame* frame) {
131  return cvc::GpuMat(frame->height(), frame->width(),
132  frame_to_cv_type(frame->type, frame->channels()),
133  frame->data);
134 }
135 
136 inline cvc::GpuMat bytesToImage_gpu(u8* buf, const FrameInfo& metadata) {
137  return cvc::GpuMat(metadata.height(), metadata.width(), CV_8UC3, buf);
138 }
139 
140 
141 inline FrameInfo gpu_mat_to_frame_info(const cv::cuda::GpuMat& mat) {
142  return FrameInfo(mat.channels(), mat.cols, mat.rows,
143  cv_to_frame_type(mat.depth()));
144 }
145 
146 inline Frame* gpu_mat_to_frame(const cv::cuda::GpuMat& mat) {
147  int device;
148  CU_CHECK(cudaGetDevice(&device));
149  Frame* frame = new_frame(DeviceHandle(DeviceType::GPU, device),
150  gpu_mat_to_frame_info(mat));
151  if (mat.isContinuous()) {
152  cudaMemcpy(frame->data, mat.data, frame->size(), cudaMemcpyDefault);
153  } else {
154  size_t frame_pitch =
155  frame->width() * frame->channels() * size_of_frame_type(frame->type);
156  cudaMemcpy2D(frame->data, frame_pitch, mat.data, mat.step, mat.cols,
157  mat.rows, cudaMemcpyDefault);
158  }
159  return frame;
160 }
161 
162 }
163 #endif
Definition: database.cpp:36