基本上我想将图像的每个channnel值乘以向量。例如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]);
}
}
}
答案 0 :(得分:0)
好吧这是一个有点奇怪的问题。我不认为任何人需要这个功能,但万一...因为它花了我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]);
答案 1 :(得分:-1)
我不确定你喜欢做什么,但想过cv :: multiply?