Back to listC++
OpenCV-style Matrix Class
Lv.5856@mukitaro0 playsJan 2, 2026
OpenCV Mat class pattern for image processing. Core of computer vision operations.
preview.cpp
1#include <cstddef>2#include <memory>3 4class Mat {5public:6 int rows;7 int cols;8 int type;9 unsigned char* data;10 11private:12 std::shared_ptr<unsigned char[]> dataPtr;13 size_t step;14 15public:16 Mat() : rows(0), cols(0), type(0), data(nullptr), step(0) {}17 18 Mat(int _rows, int _cols, int _type)19 : rows(_rows), cols(_cols), type(_type) {20 size_t elemSize = (type & 7) + 1;21 step = cols * elemSize;22 dataPtr = std::make_shared<unsigned char[]>(rows * step);23 data = dataPtr.get();24 }25 26 bool empty() const { return data == nullptr; }27 size_t total() const { return static_cast<size_t>(rows) * cols; }28 29 Mat clone() const {30 Mat m(rows, cols, type);31 std::copy(data, data + rows * step, m.data);32 return m;33 }34 35 template<typename T>36 T& at(int row, int col) {37 return reinterpret_cast<T*>(data + row * step)[col];38 }39};Custom problems are not included in rankings