2017-08-07 20 views
0

我想模糊的圖像模糊的圖像。在這一點上,我可以模糊它沒有邊界。我讀過我可以通過將數據容器放入更大的數據容器來實現我的目標。我試過了,但是我不能成功地做到這一點。也許有人知道更簡單的解決方案來做到這一點這裏是我的代碼:用純C++

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; 
} 

哪裏Image是如下:

struct Image 
{ 
    std::vector<uint8_t> data; 
    Size size; 
    ImageType type; 

    int pixelCount() const { 
     return size.m_height * size.m_width; 
    } 

    Image() {} 
    Image(Size _size, ImageType _type) : size(_size), type(_type) {} 
    ~Image() {} 
}; 

struct Size 
{ 
    int m_width; 
    int m_height; 

    Size() {} 
    Size(int width, int height) : m_width(width), m_height(height) {} 
}; 

enum class ImageType 
{ 
    Rgba, 
    Grayscale 
}; 

所以你能幫忙嗎?

+1

如果你嘗試過,你應該張貼,你寫了沒有工作的代碼。否則,唯一提及並不意味着太多。而且你不需要一個更大的容器來模糊圖像,邊緣檢查條件就可以做到。 –

+0

@BartekBanachewicz上週試了一下。不幸的是,沒有該代碼已經 – gawron103

回答

0

我不完全知道什麼是你的問題,如果要對其進行模糊處理,與邊框或無邊界模糊,所以我會盡量回答關於這兩種方法。

首先,如果模糊了邊界上,你需要承擔什麼是落後的邊界值。通常你會重用邊界值。

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 

也就是說,

我可以把我的數據容器到更大的數據達到我的目標 容器

您可以通過創建新的圖像更大的圖像做,複製邊境VAL你要走出去邊境。如果模糊的它的內部部分(源indicies [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 

現在您在指數上indicies [1,3]計算5倍圖像的模糊用3×盒和它寫成模糊圖像與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; 
} 

但現在你可以問自己:爲什麼所有這些數據拷貝到臨時圖像,如果你能選擇的邊界像素,而不是一個-OF-得到相同的結果通過投入更大的容器

模糊填充過濾器值時邊框?只需將邊界以外的指示符夾緊即可。這給:

迷離,無需投入更大的容器:(只從你的函數幾道變化)

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; 
} 
+0

謝謝!好的解決方案 – gawron103