C++ 用纯c+模糊图像+;
我正试图模糊图像。在这一点上,我可以模糊它没有边界。我读到,我可以通过将我的数据容器放入一个更大的数据容器来实现我的目标。我试过了,但没有成功。也许有人知道一个更简单的解决办法?这是我的密码:C++ 用纯c+模糊图像+;,c++,C++,我正试图模糊图像。在这一点上,我可以模糊它没有边界。我读到,我可以通过将我的数据容器放入一个更大的数据容器来实现我的目标。我试过了,但没有成功。也许有人知道一个更简单的解决办法?这是我的密码: Image Blur::transform(const Image &inputImage) { Image input = (inputImage.type != ImageType::Grayscale) ? Grayscale().transform(inpu
Image Blur::transform(const Image &inputImage)
{
Image input = (inputImage.type != ImageType::Grayscale)
? Grayscale().transform(inputImage)
: inputImage;
std::cout << "Blurring" << std::endl;
Image output = input;
auto indexAt = [&input](int row, int col) { return row * input.size.m_width + col; };
for (int row = m_BLUR_MASK_RADIUS; row < output.size.m_height - m_BLUR_MASK_RADIUS; row++)
{
for (int col = m_BLUR_MASK_RADIUS; col < output.size.m_width - m_BLUR_MASK_RADIUS; col++)
{
std::vector<uint8_t> pixel_values;
for (int row_offset = -m_BLUR_MASK_RADIUS; row_offset <= m_BLUR_MASK_RADIUS; row_offset++)
{
for (int col_offset = -m_BLUR_MASK_RADIUS; col_offset <= m_BLUR_MASK_RADIUS; col_offset++)
{
const int offset_pixel_index = indexAt(row + row_offset, col + col_offset);
pixel_values.push_back(input.data[offset_pixel_index]);
}
}
const int center_pixel_index = indexAt(row, col);
output.data[center_pixel_index] = getModifiedValue(pixel_values);
}
}
return output;
}
那么你能帮忙吗?我不太确定你的问题是什么,如果你想模糊它,用边框或无边框模糊它,所以我将尝试用这两种方法回答 首先,当边界模糊时,需要假设边界后面的值是什么。通常,您重用边界值
x index -2 | -1 | 0 | 1 | 2
---------------------------
color ? | ? | C1 | C2 | C3
因此,将C1复制到-1,-2索引
x index -2 | -1 | 0 | 1 | 2
---------------------------
color C1 | C1 | C1 | C2 | C3
就是
我可以通过将数据容器放入更大的数据中来实现我的目标
容器
您可以通过创建新图像或更大的图像并将边界值复制到边界外的值来实现。您可以模糊它的内部部分(在源标记[0,N]),然后丢弃边界外的值(因为它们在原始图像中不相关)
3x3图像示例:
C1 | C2 | C3
------------
C5 | C5 | C6
------------
C7 | C8 | C9
添加1个模糊半径
C1 | C1 | C2 | C3 | C3
----------------------
C1 | C1 | C2 | C3 | C3
----------------------
C5 | C5 | C5 | C6 | C6
----------------------
C7 | C7 | C8 | C9 | C9
----------------------
C7 | C7 | C8 | C9 | C9
现在,您可以使用标记[1,3]上的索引上的3x3框计算5x图像的模糊度,并将其写入3x3模糊图像。这是您的函数几乎已经在做的事情(除了调整大小)
通过放入更大的容器进行模糊:
Image transformWithExtending(const Image &inputImage)
{
Image input = (inputImage.type != ImageType::Grayscale)
? Grayscale().transform(inputImage)
: inputImage;
Image newInput = Image({ input.size.m_width + 2 * m_BLUR_MASK_RADIUS, input.size.m_height + 2 * m_BLUR_MASK_RADIUS }, input.type);
auto indexAt = [&input](int row, int col) { return row * input.size.m_width + col; };
auto clamp = [&input](int x, int minv, int maxv) {return std::min(std::max(x, minv), maxv); } // std::clamp is only in C++17
// indexing in source image (with negatives)
for (int row = -m_BLUR_MASK_RADIUS; row < input.size.m_height + m_BLUR_MASK_RADIUS; row++)
for (int col = -m_BLUR_MASK_RADIUS; col < input.size.m_width + m_BLUR_MASK_RADIUS; col++)
newInput.data.push_back(input.data[indexAt(clamp(row, 0,input.size.m_width - 1), clamp(row, 0, input.size.m_height - 1))]);
// now transform it with previous function
Transform(newInput)
// and resize...
// TODO
//
return output;
}
Image transformWithExtending(常量图像和输入图像)
{
图像输入=(inputImage.type!=图像类型::灰度)
?灰度().变换(输入图像)
:输入图像;
Image newInput=图像({input.size.m_width+2*m_BLUR_MASK_RADIUS,input.size.m_height+2*m_BLUR_MASK_RADIUS},input.type);
自动索引=[&input](int行,int列){返回行*input.size.m_width+col;};
自动箝位=[&input](intx,intminv,intmaxv){return std::min(std::max(x,minv,maxv);}//std::clamp仅在C++17中使用
//源图像中的索引(带底片)
对于(int row=-m_BLUR_MASK_RADIUS;row
但现在你们可以问自己:若你们在填充过滤框值时可以通过选择边界像素而不是其中一个边界像素得到相同的结果,为什么要将所有这些数据复制到临时图像中?只需将标记从边框夹到边框外即可。其中:
模糊而不放入更大的容器中:(只有少数车道与功能不同)
图像模糊::transformWithBorders(常量图像和输入图像)
{
图像输入=(inputImage.type!=图像类型::灰度)
?灰度().变换(输入图像)
:输入图像;
std::cout如果您尝试过,您应该发布您编写的不起作用的代码。否则,仅提及这一点并没有多大意义。而且您不需要一个更大的容器来模糊图像,边缘检查条件也可以。@BartekBanachewicz上周尝试过。不幸的是,没有现成的代码!很好的解决方案
Image transformWithExtending(const Image &inputImage)
{
Image input = (inputImage.type != ImageType::Grayscale)
? Grayscale().transform(inputImage)
: inputImage;
Image newInput = Image({ input.size.m_width + 2 * m_BLUR_MASK_RADIUS, input.size.m_height + 2 * m_BLUR_MASK_RADIUS }, input.type);
auto indexAt = [&input](int row, int col) { return row * input.size.m_width + col; };
auto clamp = [&input](int x, int minv, int maxv) {return std::min(std::max(x, minv), maxv); } // std::clamp is only in C++17
// indexing in source image (with negatives)
for (int row = -m_BLUR_MASK_RADIUS; row < input.size.m_height + m_BLUR_MASK_RADIUS; row++)
for (int col = -m_BLUR_MASK_RADIUS; col < input.size.m_width + m_BLUR_MASK_RADIUS; col++)
newInput.data.push_back(input.data[indexAt(clamp(row, 0,input.size.m_width - 1), clamp(row, 0, input.size.m_height - 1))]);
// now transform it with previous function
Transform(newInput)
// and resize...
// TODO
//
return output;
}
Image Blur::transformWithBorders(const Image &inputImage)
{
Image input = (inputImage.type != ImageType::Grayscale)
? Grayscale().transform(inputImage)
: inputImage;
std::cout << "Blurring" << std::endl;
Image output = input;
auto indexAt = [&input](int row, int col) { return row * input.size.m_width + col; };
auto clamp = [&input](int x, int minv, int maxv) {return std::min(std::max(x, minv), maxv); } // std::clamp is only in C++17
for (int row = 0; row < output.size.m_height; row++) // go over whole image
{
for (int col = 0; col < output.size.m_width; col++) // go over whole image
{
std::vector<uint8_t> pixel_values;
for (int row_offset = -m_BLUR_MASK_RADIUS; row_offset <= m_BLUR_MASK_RADIUS; row_offset++)
{
for (int col_offset = -m_BLUR_MASK_RADIUS; col_offset <= m_BLUR_MASK_RADIUS; col_offset++)
{
// and clamp indicies here
const int offset_pixel_index = indexAt(clamp(row + row_offset, 0, output.size.m_height - 1), clamp(col + col_offset,0, output.size.m_width - 1));
pixel_values.push_back(input.data[offset_pixel_index]);
}
}
const int center_pixel_index = indexAt(row, col);
output.data[center_pixel_index] = getModifiedValue(pixel_values);
}
}
return output;
}