如何在C或C ++中将彩色图像转换为灰度 [英] how to convert coloured image to grayscale in C or C++

查看:127
本文介绍了如何在C或C ++中将彩色图像转换为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C或C ++中将彩色图像转换为灰度?
此处存在的所有线程均在C#

how to convert coloured image to grayscale in C or C++ ?
All the threads present here give solution in C#

推荐答案

:)中给出解决方案:

0.任何颜色都可以表示为具有坐标(r,g,b)的3D矢量
1.对于任何w/b颜色均有效(r == g == b)
2.任何向量的长度
3.任何转换后的w/b向量都具有与原始向量相同的长度
4.图像的任何像素都具有颜色

另请参见 [
:) :

0. Any color can be presented as a 3D vector with coordinates (r, g, b)
1. For any w/b color is valid (r == g == b)
2. Any vector has a length
3. Any converted w/b vector has the same length to the original
4. Any pixel of an image has a color

See also[^]
void convert(CImage* pcImage)
{
  ASSERT(pcImage);

  int iWidth(pcImage->GetWidth());
  int iHeight(pcImage->GetHeight());
  
  if (iWidth && iHeight) {
    for (int i = 0; i < iWidth; i++) {
      for (int j = 0; j < iHeight; j++) {
        COLORREF clrOriginal(pcImage->GetPixel(i, j));
        float fR(GET_R(clrOriginal));
        float fG(GET_G(clrOriginal));
        float fB(GET_B(clrOriginal));

        float fWB = sqrt((fR * fR + fG * fG + fB * fB) /3);
        pcImage->SetPixel(i, j, RGB(fWB, fWB, fWB));
      }
    }
  }
}


转换颜色到灰色阴影 [ ^ ]

在不使用指针的情况下将RGB转换为灰度 [ ^ ](C#中的技巧/技巧)
Converting Colors to Gray Shades[^]

Convert RGB to Gray Scale without using Pointers[^](Couple of Tip/Tricks in C#)


这篇关于如何在C或C ++中将彩色图像转换为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆