在visual studio 2010中使用c++将彩色图像转换为灰度
Convert a colored image to greyscale with visual studio 2010 using c++
在尝试使用c++将彩色图像转换为灰度图像时,我面临一个问题。我认为函数GetRGB()的使用有问题。
这是源代码和相关的头文件。
void CCorner::RGBToGrayScale(CImage* pIn, CImage* pOut)
{
//
// INPUT:
// CImage* pIn: The input image with 24bit depth
//
// OUTPUT:
// CImage* pOut: The output image. It has ALREADY been initialized
// with the same dimension as the input image (pIN) and
// formatted to 8bit depth (256 gray levels).
//
int height = pIn->GetHeight();
int width = pIn->GetWidth();
int clrOriginal=pIn->GetColorType();
if (height && width){
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
byte *r,*g,*b;
r=NULL; g=NULL; b=NULL;
bool colors=pIn->GetRGB(i,j,r,g,b);
double newindex = 0.299**r+0.587**g+0.114**b;
pOut->SetIndex(i,j,newindex);
}
}
}
}
而GetRGB()定义为
virtual BOOL GetRGB(int x, int y, byte* r, byte* g, byte* b)
{ return implementation->GetRGB(x, y, r, g, b); }
谢谢你的帮助!
GetRGB
期望您提供指向实际byte
s的指针,它将输出r
, g
和b
的结果。这是一种在c++中通过使用输出参数从函数返回多个结果的方法。
然而,你在这里给它指向NULL
的指针:
byte *r,*g,*b;
r=NULL; g=NULL; b=NULL;
所以没有地方写结果。当你稍后解引用它们时(通过*r
, *g
, *b
),你会得到未定义的行为,因为它们是null
。
你应该做的是:
byte r, g, b;
bool colors = pIn->GetRGB(i, j, &r, &g, &b);
当然,后面不需要遵守它们,因为它们不是指针:
double newindex = 0.299*r+0.587*g+0.114*b;
您正在将空指针传递给期望写入这些内存地址的函数。给它一个有效的内存地址,它可以在那里为你存储结果。
byte r = 0, g = 0, b = 0;
bool colors = pIn->GetRGB(i, j, &r, &g, &b);
相关文章:
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 彩色图像的卤化物处理平均值
- OpenCV C++将灰色图像组合成彩色图像
- 将 BGR 图像转换为 jpeg 格式的 base64 字符串
- C++ Gdi+将图像转换为灰度
- 将 BMP 图像转换为灰度
- Qmage:如何将灰色图像转换为RGB热图
- 更快地将卤化物图像转换为 opencv 垫?
- 使用 OpenCV 和 C++将 16 位灰度图像更改为彩色图像
- 将原始视频图像转换为VPX编码器的I420
- ROS中未定义的参考误差到OpenCV图像转换器
- 如何将图像转换为灰度 opengl c++
- 将 24 位".bmp"图像转换为黑白/单色图像的可能算法是什么?
- 将任何彩色PPM图像转换为只有8种颜色的彩色图像
- 在 OpenCV 中将图像转换为彩色铅笔素描
- 在visual studio 2010中使用c++将彩色图像转换为灰度
- OpenCV将彩色图像转换为灰度
- Opencv:: 将Mat_< Vec3f>转换为普通的 8 位彩色图像
- 图像转换器在Qt,彩色图像到黑白