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" 24 #include <opencv2/opencv.hpp> 28 inline int frame_to_cv_type(FrameType type,
int channels = 1) {
35 case FrameType::U16: {
39 case FrameType::F32: {
43 case FrameType::F64: {
48 return CV_MAKETYPE(cv_type, channels);
51 inline FrameType cv_to_frame_type(
int t) {
59 type = FrameType::U16;
63 type = FrameType::F32;
67 type = FrameType::F64;
70 default: { LOG(FATAL) <<
"Unsupported OpenCV type: " << t; }
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()));
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());
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);
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);
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);
105 inline cv::Mat bytesToImage(u8* buf,
const FrameInfo& metadata) {
106 return cv::Mat(metadata.height(), metadata.width(), CV_8UC3, buf);
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> 118 namespace cvc = cv::cuda;
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()),
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()),
136 inline cvc::GpuMat bytesToImage_gpu(u8* buf,
const FrameInfo& metadata) {
137 return cvc::GpuMat(metadata.height(), metadata.width(), CV_8UC3, buf);
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()));
146 inline Frame* gpu_mat_to_frame(
const cv::cuda::GpuMat& mat) {
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);
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);
Definition: database.cpp:36