【问题标题】:How to convert image to greyscale opengl c++ [duplicate]如何将图像转换为灰度opengl c ++ [重复]
【发布时间】:2018-09-26 13:11:38
【问题描述】:

任何人都知道如何转换为灰度,下面是我需要使用的一些骨架代码。具体来说,将“之前”转换为灰度, 应用 Sobel 边缘检测卷积滤波器,并存储 导致“之后”。 before 必须为非空。

template <typename color_depth> void 
edge_detect(gfx::image<color_depth>& after,
const gfx::image<color_depth>& before) {

  // Check arguments.
  assert(!before.empty());

  // TODO: replace this function body with working code. Make sure
  // to delete this comment.

  // Hint: Use the grayscale(...) and extend_edges(...) filters to
  // prepare for the Sobel convolution. Then compute the Sobel
  // operator one pixel at a time. Finally use crop_extended_edges
  // to un-do the earlier extend_edges.

}

【问题讨论】:

    标签: c++ opengl grayscale


    【解决方案1】:

    这看起来是一个家庭作业问题,所以我不会给出完整的实现。我也无法确定您是要在 CPU 上还是在着色器中转换为灰度。无论您在何处执行转换,公式都是相同的。

    没有确定的方法可以转换为灰度,因为您要丢弃信息,最终结果是否正确完全是主观的。下面是一些从RGB转换为灰度的常用方法:

    一种天真的方法是找到具有最高值的颜色通道并使用它。

    grey = max(colour.r, max(colour.g, colour.b));
    

    幼稚方法的缺点在于,如果图像的某些区域不包含具有最高值的颜色,它们将完全丢失细节。为了防止这种情况,我们可以使用所有颜色分量的简单平均值。

    grey = (colour.r + colour.g + colour.b) / 3.0;
    

    “更好”的方法是使用luma 值。人眼比其他人更好地感知某些颜色波长。因此,如果我们赋予这些颜色更多的权重,我们会产生更合理的灰度。

    grey = dot_product(colour, vec3(0.299, 0.587, 0.114));
    

    还有一种方法是“去饱和”图像。这包括首先将 RGB 颜色空间转换为HSL。然后将饱和度降低到零。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      相关资源
      最近更新 更多