找回密码
 加入
搜索
查看: 5644|回复: 3

[图形处理] 【已解决】获取灰度图rgb颜色分量不正确,恳求指教

[复制链接]
发表于 2014-7-14 17:03:17 | 显示全部楼层 |阅读模式
本帖最后由 xiehuahere 于 2014-7-14 21:28 编辑

图片:


代码:
#include <GDIPlus.au3>

;Load image
Local $imgFile = FileOpenDialog("Choose Picture", @ScriptDir, "Pictures (*.jpg)", 1)
If @error Then Exit
ConsoleWrite("Image file: " & $imgFile & @CRLF)

_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile($imgFile)
Local $iWidth = _GDIPlus_ImageGetWidth($hImage)
Local $iHeight = _GDIPlus_ImageGetHeight($hImage)
_GDIPlus_ImageDispose($hImage)
ConsoleWrite("Resolution: " & $iWidth & "x" & $iHeight & @CRLF)

Local $hBitmap = _GDIPlus_BitmapCreateFromFile($imgFile)
Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iWidth, $iHeight, $GDIP_ILMREAD, $GDIP_PXF32RGB)
Local $grayValue1 = get_luma_jpg($hBitmap, 279, 3)
Local $grayValue2 = get_luma_jpg($hBitmap, 279, 5)
ConsoleWrite("$grayValue1: " & $grayValue1 & ", $grayValue2: " & $grayValue2 & @CRLF)
_GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

;~         Luma = 0.299 * Red + 0.587 * Green + 0.114 * Blue
Func get_luma_jpg($hBitmap, $iX, $iY)
        Local $rgb = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
        Return (0.299 * $rgb[0] + 0.587 * $rgb[1] + 0.114 * $rgb[2])
EndFunc   ;==>get_luma_jpg

; http://www.autoitscript.com/forum/topic/97922-semi-solved-need-help-with-dllcall-gdi-function/
Func _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
        Local $str = "byte r;byte g;byte b"
        Local $color = DllStructCreate($str)
        DllCall($ghGDIPDll, "int", "GdipBitmapGetPixel", "hwnd", $hBitmap, "int", $iX, "int", $iY, 'ptr', DllStructGetPtr($color))
        If @error Then
                MsgBox(48 + 262144, 'GdipBitmapGetPixel', @error & ': ' & @extended)
                Return SetError(@error, @extended, 0)
        EndIf
        
        Local $r = DllStructGetData($color, 'r')
        Local $g = DllStructGetData($color, 'g')
        Local $b = DllStructGetData($color, 'b')
        ;RGB shall be of the same value for grayscale image
        ConsoleWrite('R: ' & $r & ', G: ' & $g & ', B: ' & $b & @CRLF)
        
        Local $retValues[3] = [$r, $g, $b]
        Return $retValues
EndFunc   ;==>_GDIPlus_BitmapGetPixel
求解两点问题:
1. 获取出来的RGB分量明显不对。灰度图的RGB分量应该都是相等的,用imageJ可以看出:
  

2. _GDIPlus_BitmapLockBits 的作用是什么?网上搜索了还是没怎么明白。MSDN上没看到详细解释啊。

恳求图像处理方面的大神出手相救!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2014-7-14 20:38:40 | 显示全部楼层
1、获取颜色不成功的原因是用_GDIPlus_BitmapLockBits锁定了,另获取颜色在UDF中本来就有,不需要自己写。
2、_GDIPlus_BitmapLockBits我也不清楚,应该是锁定图像数据到内存,这时可能不能用_GDIPlus_BitmapGetPixel颜色。
因为这时图像的数据已经返回在 $tagGDIPBITMAPDATA 结构,要读取数据要对$tagGDIPBITMAPDATA才有效。

#include <GDIPlus.au3>

;Load image
Local $imgFile = FileOpenDialog("Choose Picture", @ScriptDir, "Pictures (*.jpg)", 1)
If @error Then Exit
ConsoleWrite("Image file: " & $imgFile & @CRLF)

_GDIPlus_Startup()
Local $hBitmap = _GDIPlus_BitmapCreateFromFile($imgFile)
Local $iWidth = _GDIPlus_ImageGetWidth($hBitmap)
Local $iHeight = _GDIPlus_ImageGetHeight($hBitmap)
ConsoleWrite("Resolution: " & $iWidth & "x" & $iHeight & @CRLF)
;~ Local $tBitmapData = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iWidth, $iHeight, $GDIP_ILMREAD, $GDIP_PXF24RGB)
Local $grayValue1 = get_luma_jpg($hBitmap, 309, 17)
Local $grayValue2 = get_luma_jpg($hBitmap, 279, 5)
ConsoleWrite("$grayValue1: " & $grayValue1 & ", $grayValue2: " & $grayValue2 & @CRLF)
;~ _GDIPlus_BitmapUnlockBits($hBitmap, $tBitmapData)
_GDIPlus_BitmapDispose($hBitmap)
_GDIPlus_Shutdown()

;~         Luma = 0.299 * Red + 0.587 * Green + 0.114 * Blue
Func get_luma_jpg($hBitmap, $iX, $iY)
        Local $rgb = _GDIPlus_BitmapGetPixel($hBitmap, $iX, $iY)
        $b = BitAND(0xFF, BitShift($rgb, 16))
        $g = BitAND(0xFF, BitShift($rgb, 8))
        $r = BitAND(0xFF, $rgb)
        ConsoleWrite($r & @TAB & $g & @TAB & $b & @CR)
        $ret = Int((30 * $r + $g * 59 + $b * 11) / 100)
        Return $ret
EndFunc   ;==>get_luma_jpg

评分

参与人数 1金钱 +25 收起 理由
xiehuahere + 25 谢谢帮助!

查看全部评分

 楼主| 发表于 2014-7-14 21:25:02 | 显示全部楼层
本帖最后由 xiehuahere 于 2014-7-14 21:29 编辑

回复 2# seniors


试过的确可以了。
我用的Au3版本是3.3.7.15,低了点,_GDIPlus_BitmapGetPixel还没被收录到<GDIPlus.au3>里。我本地有个当时收录的<GDIPlusEx.au3>,里面倒是有这个函数。看来不追新就要落后了。

谢谢前辈指教!
发表于 2014-7-16 08:10:55 | 显示全部楼层
这个要学一下,获取颜色
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-19 14:33 , Processed in 0.086058 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表