将每个通道像素乘以给定的向量
Multiply each channel pixel with given vector
基本上我想将图像的每个通道值与向量相乘。例如,vec4f(1,2,3,4(1 *图像2的红色通道 *绿色频道等等,这是我的代码(不是完整的代码,有一些错误(,但我的老板说必须有更好,更简单的方法使用OpenCV,但我找不到。预先感谢。
void scaleImage(TextureData& dst, TextureData const& src, cv::Vec4f
const& scale)
{
auto size = src.info.size;
dst=src;
cv::Mat bgr[4];
cv::split(src.levels[0].images[0], bgr);
for (int y = 0; y < size.height; ++y)
{
for (int x = 0; x < size.width; ++x)
{
src.levels[0].images[0].channels();
if (src.levels[0].images[0].channels() == 4)
{
auto& imgRGB = dst.levels[0].images[0].at<cv::Vec4f>(x, y);
imgRGB[3] = static_cast<uint8_t>(scale.w()*bgr[3].at[x, y]);
imgRGB[2] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
imgRGB[1] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
imgRGB[0] = static_cast<uint8_t>(scale.z()*bgr[0].at[x, y]);
}
if (src.levels[0].images[0].channels() == 3)
{
auto& imgRGB = dst.levels[0].images[0].at<cv::Vec3f>(x, y);
imgRGB[2] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
imgRGB[1] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
imgRGB[0] = static_cast<uint8_t>(scale.z()*bgr[0].at[x, y]);
}
if (src.levels[0].images[0].channels() == 2)
{
auto& imgRGB = dst.levels[0].images[0].at<cv::Vec2f>(x, y);
imgRGB[1] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
imgRGB[0] = static_cast<uint8_t>(scale.y()*bgr[1].at[x, y]);
}
if (src.levels[0].images[0].channels() == 2)
{
auto& imgRGB = dst.levels[0].images[0].at<float>(x, y);
imgRGB[0] = static_cast<uint8_t>(scale.x()*bgr[2].at[x, y]);
}
}
}
好吧,这有点奇怪。我认为没有人需要此功能,但如果...因为……因为它花了我2-3天(只有25行(
void scaleImage(TextureData& dst, TextureData const& src, Vec4f const&
scale)
{
std::vector<cv::Mat> bgr(3);
std::vector<cv::Mat> resultMask(3);
src.info;
cv::split(src.levels[0].images[0], bgr);
//dst = src;
//auto size = src.info.size;
cv::Point anchor = cv::Point(-1, -1);
float a = scale.x();
float b = scale.y();
float c = scale.z();
float d = scale.w();
cv::Mat kern = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, a, 0,
0, 0, 0);
cv::Mat kern1 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, b, 0,
0, 0, 0);
cv::Mat kern2 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, c, 0,
0, 0, 0);
cv::Mat kern3 = (cv::Mat_<float>(3, 3) << 0, 0, 0,
0, d, 0,
0, 0, 0);
cv::filter2D(bgr[0], resultMask[0], bgr[0].depth(), kern, anchor, 0, 1);
cv::filter2D(bgr[1], resultMask[1], bgr[1].depth(), kern1, anchor, 0, 1);
cv::filter2D(bgr[2], resultMask[2], bgr[2].depth(), kern2, anchor, 0, 1);
//cv::filter2D(bgr[3], resultMask[3], bgr[3].depth(), kern3, anchor, 0, 1);
cv::merge(resultMask, dst.levels[0].images[0]);
我不确定您想做什么,但是考虑了cv ::倍数?
- 您使用的openCV版本吗?
- 它是因素的常量因素还是const矩阵?
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 将每个通道像素乘以给定的向量