当前位置:文档之家› 灰度图像处理

灰度图像处理

第四章 灰度图像处理
4.1 彩色图像转灰度图像 4.2 直方图 4.3 灰度变换 4.4 图像平滑 4.5 图像锐化 4.6 图像分割 4.7 图像匹配
4.1 彩色图像转灰度图像
1、分量法 2、最大值法 3、平均值法 4、加权平均法
4.1 彩色图像转灰度图像
GetPixel 函数功能:该函数检索指定坐标点的像素的RGB颜色值。 函数原型:COLORREF GetPixel(HDC hdc, int nXPos, int nYPos) 参数:
For j = 0 To h - 1 rgb1 = GetPixel(Picture1.hdc, i, j) Red = rgb1 Mod 256 '获得红色值 Green = rgb1 \ 256 Mod 256 '获得绿色值 Blue = rgb1 \ 65536 '获得兰色值 '取R、G、B分量最大值作为灰度 If Red > Green And Red > Blue Then Col = Red If Green > Red And Green > Blue Then Col = Green If Blue > Red And Blue > Green Then Col = Blue rgb1 = RGB(Col, Col, Col) SetPixelV Picture2.hdc, i, j, rgb1
4.1 彩色图像转灰度图像
4、加权平均法
Private Sub Command5_Click() Picture1.Picture = Picture2.Picture w = Picture1.ScaleWidth h = Picture1.ScaleHeight
For i = 0 To w-1 For j = 0 To h-1 rgb1 = GetPixel(Picture1.hdc, i, j) Blue = rgb1 mod 256 '获得兰色值 Red =rgb1\256 mod 256 '获得红色值 Green = rgb1 \65536 '获得绿色值 '将三原色取其权值转换为灰度 Y = (9798 * Red + 19235 *Green + 3735 * Blue) \ 32768 '将灰度转换为RGB rgb1 = RGB(Y, Y, Y) SetPixelV picture2.hdc, i, j, rgb1 Next j
4.1 彩色图像转灰度图像
SetPixel
函数功能:该函数将指定坐标处的像素设为指定的颜色。 函数原型:COLORREF SetPixel(HDC hdc, int X, int Y, COLORREF crColor); 参数:
hdc:设备环境句柄。 X:指定要设置的点的X轴坐标,按逻辑单位表示坐标。 Y:指定要设置的点的Y轴坐标,按逻辑单位表示坐标。 crColor:指定要用来绘制该点的颜色。 返回值:如果函数执行成功,那么返回值就是函数设置像素的RGB颜色 值。这个值可能与crColor指定的颜色有所不同,之所以有时发生这种情况是 因为没有找到对指定颜色进行真正匹配造成的;如果函数失败,那么返回值是 -1。
Picture2.Picture = Picture2.Image End Sub
4.1 彩色图像转灰度图像
2、最大值法
Private Sub Command3_Click() w = Picture1.ScaleWidth
h = Picture1.ScaleHeight For i = 0 To w - 1
Next j Next i Picture2.Picture = Picture2.Image End Sub
4.1 彩色图像转灰度图像
3、平均值法
Private Sub Command4_Click() Picture1.Picture = Picture2.Picture w = Picture1.ScaleWidth h = Picture1.ScaleHeight For i = 0 To w - 1 For j = 0 To h - 1 rgb1 = GetPixel(Picture1.hdc, i, j)
声明方法: Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal Y As Long, ByVal crColor As Long) As Long
4.1 彩色图像转灰度图像
hdc:设备环境句柄。 nXPos:指定要检查的像素点的逻辑X轴坐标。 nYPos:指定要检查的像素点的逻辑Y轴坐标。 返回值:返回值是该象像点的RGB值。
声明方法: Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal ቤተ መጻሕፍቲ ባይዱ As Long) As Long
1、分量法(R分量法)
Private Sub Command2_Click() Picture1.Picture = Picture2.Picture w = Picture1.ScaleWidth h = Picture1.ScaleHeight For i = 0 To w-1 For j = 0 To h-1 rgb = GetPixel(Picture1.hdc, i, j) Red = rgb mod 256 '获得红色值 Green = rgb \256 mod 256 '获得绿色值 Blue = rgb\65536 '获得兰色值 '取R分量作为灰度 rgb = RGB(Red,Red,Red) SetPixelV Picture2.hdc, i, j, rgb Next j Next i
Red = rgb1 Mod 256 '获得红色值 Green = rgb1 \ 256 Mod 256 '获得绿色值 Blue = rgb1 \ 65536 '获得兰色值 '取R、G、B三分量平均值作为灰度 col = (Red + Green + Blue) / 3 rgb1 = RGB(col, col, col) SetPixelV Picture2.hdc, i, j, rgb1 Next j Next i Picture2.Picture = Picture2.Image End Sub
相关主题