c++ - 如何返回定义明确的内存部分?例如图像数据中像素的颜色值
问题描述
我需要对图像进行一些操作。图像可以是彩色/灰度和 8 位/16 位。我想在不使用任何第三方库(opencv、IPP 等)的情况下做到这一点。
我有:
- 图像数据为 void *。
- 图像的宽度和高度。
- 通道数
- 每个颜色通道的位分辨率。
我正在考虑使用以下结构来表示颜色和图像。
颜色结构
template<typename ColorDataType, std::size_t channelCount = 3, std::size_t bitResolution = 8>
struct Color
{
using DataType = ColorDataType;
std::array<DataType, channelCount> colorData;
};
图像结构
template<typename ColorType>
class Image
{
std::size_t width;
std::size_t height;
ColorType::DataType * data; // Or a unique_ptr<DataType[]> haven't decided on the ownership yet.
public:
Image(std::size_t inWidth, std::size_t inHeight, ColorType::DataType * inData)
: width(inWidth), height(inHeight), data(inData)
{}
Color & GetColor(std::size_t row, std::size_t col)
{
// How can I return a color element here that could be manipulated from the receiving side?
// The change should be reflected in the memory addressed by data pointer.
}
};
返回图像数据的一部分以便我以后可以操作它的最佳方法是什么?我也想有一个相同的 const 机制,以防我的 Image 是 const。
附加信息: 要求更多是将给定的数据流视为 3-D 矩阵,其维度为:
- 图像的行
- 图像的列
- 图像颜色
第 3 维颜色可以有 1 个或 3 个元素,具体取决于图像是灰度图像还是 RGB 图像。我想有一种方法可以根据提供的行和列访问图像的颜色元素,并能够根据接收到的颜色元素编辑数据流中的真实数据。
通过这样做,我希望将 Color 表示为一个单独的实体,以便我可以说“Color 具有 DataType、channelCount 和 bitResolution”并且 Image 由特定的 Color Type 组成
解决方案
我假设你的Color
类知道关于它自己的一切,只是从一个指向你的内存的指针就可以构造。为了让它实际操作你的图像,将迭代器传递data
给构造函数Color
并存储这些迭代器。每次操作颜色对象时,请确保(注意线程安全!)也操作迭代器后面的数据。
如果您不想强制接收者接受引用,也可以decltype(auto)
从 GetColor 返回。
我在 2D-array 类中使用了类似的东西,我想拥有类似于 a 的东西operator[][]
:
#include <cassert>
#include <vector>
template <typename T> class Matrix {
std::vector<T> _matrix;
const int _rows;
const int _cols;
public:
Matrix(int rows, int cols, T def) : _matrix(rows * cols, def), _rows(rows), _cols(cols) {}
Matrix(int rows, int cols) : Matrix(rows, cols, T()) {}
class Row {
private:
Matrix<T> &_m;
const int _row;
public:
Row(Matrix<T> &m, int row) : _m(m), _row(row) {}
decltype(auto) operator[](int col) {
assert(col >= 0);
assert(col < _m.cols());
return _m._matrix[_row * _m.cols() + col];
}
};
friend class Row;
Row operator[](int row) {
assert(row >= 0);
assert(row < _rows);
return Row(*this, row);
}
int rows() const { return _rows; }
int cols() const { return _cols; }
};
编辑:这应该更像您正在寻找的内容:
#include <array>
#include <cassert>
#include <iostream>
#include <vector>
template <std::size_t channelCount = 3, std::size_t bitResolution = 8> struct Color {
private:
std::vector<int>::iterator begin;
public:
Color(std::vector<int>::iterator begin) : begin(begin) {}
std::string toString() const {
std::string rep = "(";
for (std::size_t i = 0; i < channelCount; ++i) {
rep += std::to_string(*(begin + i));
if (i + 1 < channelCount)
rep += ", ";
}
rep += ")";
return rep;
}
template <std::size_t channel> void setChannelColor(int color) {
static_assert(channel < channelCount);
*(begin + channel) = color;
}
static constexpr std::size_t getChannelCount() { return channelCount; }
static constexpr std::size_t getBitResolution() { return bitResolution; }
};
template <class ColorTypeT> class Image {
using ColorType = Color<ColorTypeT::getChannelCount(), ColorTypeT::getBitResolution()>;
static constexpr std::size_t channelCount = ColorTypeT::getChannelCount();
static constexpr std::size_t bitResolution = ColorTypeT::getBitResolution();
std::size_t width;
std::size_t height;
std::vector<int> data;
public:
Image(std::size_t inWidth, std::size_t inHeight, std::vector<int> inData) : width(inWidth), height(inHeight), data(std::move(inData)) {}
decltype(auto) GetColor(std::size_t row, std::size_t col) { return ColorType(data.begin() + (row * width + col) * channelCount); }
std::size_t getWidth() const { return width; }
std::size_t getHeight() const { return height; }
};
template <class Image> void printImage(Image &image) {
for (std::size_t i = 0; i < image.getHeight(); ++i) {
for (std::size_t j = 0; j < image.getWidth(); ++j) {
std::cout << image.GetColor(i, j).toString() << " ";
}
std::cout << std::endl;
}
}
int main(void) {
std::vector<int> data = {255, 0, 0, 0, 255, 255, 0, 0, 0, 128, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 255, 0, 0, 0, 0, 255};
using Color1 = Color<3, 8>;
using Color2 = Color<1, 8>;
Image<Color1> i1{3, 3, data};
Image<Color2> i2{9, 3, data};
printImage(i1);
std::cout << std::endl;
printImage(i2);
std::cout << std::endl;
i1.GetColor(0, 0).setChannelColor<1>(128);
// i2.GetColor(0, 0).setChannelColor<2>(128); // static_assert fails
i2.GetColor(0, 0).setChannelColor<0>(128);
printImage(i1);
std::cout << std::endl;
printImage(i2);
return 0;
}
它尚未考虑该决议,但这应该不是很多工作。
编辑:这两个类现在没有“连接”,您可以插入任何具有方法getChannelCount
和getBitResolution
. 要强制执行此操作,您可以使用一个概念:
template<typename T>
concept bool ColorType = requires(T a) {
{ a.getChannelCount() } -> std::size_t { a.getBitResolution() } -> std::size_t;
};
然后将Image
类的定义更改为:
template <ColorType ColorTypeT> class Image
clang 还不支持这个,但是 GCC 8 支持。
推荐阅读
- sql - SQL Server 2019 中的存储过程版本控制
- python - 如何迭代熊猫中的唯一值并计算关联值的频率
- node.js - 有没有办法通过我可以将用户定义的值传递给 Neo4j 中的 Limit 子句
- angular - 错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“键”
- postgresql - 来自大表的基于时间的报告,每天有许多更新
- typescript - TypeScript - Jest 的 mockResolvedValueOnce 方法中实体上的“不可分配给从不输入”错误
- javascript - 字符串不能表示值 GraphQL
- javascript - 我的平滑滚动在单击时不起作用,它会将我引导到我的 ID 名称的页面?
- c# - 在源代码中查找链接并复制整个链接 webclient
- c++ - C++ Rookie 需要帮助来纠正 ofstream 二进制写入问题