影象處理常用演算法GPU實現一:影象二值化

NO IMAGE

  本系列文章將使用GPU實現影象處理中常用的演算法,此處只給出程式碼,關於演算法原理描述有很多資料,大家可以自行查詢。不足之處請大家不吝賜教,共同提高。

/********************************
*  Author: rabbit729
*  E-mail: [email protected]
*  Date:   2012-09-20
*  Description: 影象的二值化
********************************/
#include <d3dx9.h>
//-----------------------------------------------------------------------------
// Desc: 全域性變數
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D                 = NULL;  //Direct3D物件
LPDIRECT3DDEVICE9       g_pd3dDevice           = NULL;  //Direct3D裝置物件
LPDIRECT3DTEXTURE9      g_pTextureScreen        = NULL; //待處理圖片
ID3DXEffect*            g_pEffect               = NULL;  //效果指標
//常量控制代碼
D3DXHANDLE              hTechScreen             = NULL;  //Effect控制代碼
D3DXHANDLE              hTexScreen              = NULL;  //紋理控制代碼
D3DXHANDLE              hViewPortWidthInv       = NULL;  //視口寬倒數控制代碼
D3DXHANDLE              hViewPortHeightInv      = NULL;  //視口高倒數控制代碼
LPDIRECT3DVERTEXBUFFER9 g_pScreenSpaceQuad      = NULL;  //背板VB
const int WIDTH  = 465;
const int HEIGHT = 669;
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_TEX1)
struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z, float w)
{
_x = x;   _y = y;   _z = z; _w = w;
}
float _x, _y, _z, _w;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZW;
//-----------------------------------------------------------------------------
// Desc: 設定世界矩陣
//-----------------------------------------------------------------------------
VOID SetWorldMatrix()
{
//建立並設定世界矩陣
D3DXMATRIXA16 matWorld, matRotateX, matRotateY;
D3DXMATRIXA16 matScale;
D3DXMatrixIdentity(&matScale);
matScale._11 = matScale._22 = matScale._33 = 0.5f;
D3DXMatrixIdentity(&matWorld);
D3DXMatrixIdentity(&matRotateX);
D3DXMatrixIdentity(&matRotateY);
D3DXMatrixRotationX( &matRotateX, D3DX_PI / 3.0 );
D3DXMatrixRotationY( &matRotateY, -D3DX_PI / 8.0 );
D3DXMatrixMultiply(&matWorld, &matRotateX, &matRotateY);
D3DXMatrixMultiply(&matWorld, &matScale, &matWorld);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
}
//-----------------------------------------------------------------------------
// Desc: 設定觀察矩陣和投影矩陣
//-----------------------------------------------------------------------------
VOID SetViewAndProjMatrix()
{
//建立並設定觀察矩陣
D3DXVECTOR3 vEyePt( 0.0f, 0.0f,-250.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
//建立並設定投影矩陣
D3DXMATRIXA16 matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
//-----------------------------------------------------------------------------
// Desc: 初始化Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
//建立Direct3D物件, 該物件用於建立Direct3D裝置物件
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
//設定D3DPRESENT_PARAMETERS結構, 準備建立Direct3D裝置物件
D3DPRESENT_PARAMETERS d3dpp; 
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
//建立Direct3D裝置物件
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
//建立Effect
ID3DXBuffer* errBuffer = NULL;
if (FAILED(D3DXCreateEffectFromFile(g_pd3dDevice, L"erzhihua.fx", NULL, NULL, D3DXSHADER_DEBUG | D3DXSHADER_SKIPOPTIMIZATION, NULL, &g_pEffect, &errBuffer)))
{
if (errBuffer)
{
MessageBox(0, (LPCTSTR)errBuffer->GetBufferPointer(), 0, 0);
errBuffer->Release();
}
return E_FAIL;
}
//獲取常量控制代碼
hTechScreen         = g_pEffect->GetTechniqueByName("Screen");
hTexScreen          = g_pEffect->GetParameterByName(0, "TexScreen");
hViewPortWidthInv   = g_pEffect->GetParameterByName(0, "viewport_inv_width");
hViewPortHeightInv  = g_pEffect->GetParameterByName(0, "viewport_inv_height");
//設定環境光
g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0xffffffff );
//設定觀察矩陣和投影矩陣
SetViewAndProjMatrix();
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 建立場景圖形
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
// 建立螢幕板
g_pd3dDevice->CreateVertexBuffer(
6 * sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF, 
D3DPOOL_MANAGED,
&g_pScreenSpaceQuad,
0);
Vertex* vertices;
g_pScreenSpaceQuad->Lock(0, 0, (void**)&vertices, 0);
vertices[0] = Vertex(-1.0f, 1.0f, 0.5f, 1.0f);
vertices[1] = Vertex(1.0f,  1.0f, 0.5f, 1.0f);
vertices[2] = Vertex( 1.0f, -1.0f, 0.5f, 1.0f);
vertices[3] = Vertex(-1.0f, 1.0f, 0.5f, 1.0f);
vertices[4] = Vertex( 1.0f, -1.0f, 0.5f, 1.0f);
vertices[5] = Vertex( -1.0f, -1.0f, 0.5f, 1.0f);
g_pScreenSpaceQuad->Unlock();
//載入紋理
HRESULT hr = D3DXCreateTextureFromFile(g_pd3dDevice, L"meinv.jpg", &g_pTextureScreen);
if (FAILED(hr))
{
return E_FAIL;
} 
return S_OK;
}
//-----------------------------------------------------------------------------
// Desc: 釋放建立的物件
//-----------------------------------------------------------------------------
VOID Cleanup()
{
if (g_pScreenSpaceQuad != NULL)
{
g_pScreenSpaceQuad->Release();
}
if (g_pTextureScreen != NULL)
{
g_pTextureScreen->Release();
}
if (g_pEffect != NULL)
{
g_pEffect->Release();
}
//釋放Direct3D裝置物件
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
//釋放Direct3D物件
if( g_pD3D != NULL )
g_pD3D->Release();
}
VOID RenderScreen()
{
//將RenderTarget作為紋理
g_pEffect->SetTexture(hTexScreen, g_pTextureScreen);
float fWidthInv = 1.0f / WIDTH;
float fHeightInv = 1.0f / HEIGHT;
g_pEffect->SetFloat(hViewPortWidthInv, fWidthInv);
g_pEffect->SetFloat(hViewPortHeightInv, fHeightInv);
g_pd3dDevice->SetStreamSource(0, g_pScreenSpaceQuad, 0, sizeof(Vertex));
g_pd3dDevice->SetFVF(Vertex::FVF);
// 設定要使用的Technique
g_pEffect->SetTechnique(hTechScreen);
UINT numPasses = 0;
g_pEffect->Begin(&numPasses, 0);
for (int i = 0; i < numPasses; i  )
{
g_pEffect->BeginPass(i);
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
g_pEffect->EndPass();
}
g_pEffect->End();
}
//-----------------------------------------------------------------------------
// Desc: 渲染場景
//-----------------------------------------------------------------------------
VOID Render()
{    
// 獲取backbuffer
LPDIRECT3DSURFACE9 pBackbuffer;
g_pd3dDevice->GetRenderTarget(0, &pBackbuffer);
//開始渲染場景
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
//設回backbuffer
g_pd3dDevice->SetRenderTarget(0, pBackbuffer);
RenderScreen();
//場景渲染結束
g_pd3dDevice->EndScene();
}
//在螢幕上顯示場景
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Desc: 視窗過程, 處理訊息
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Desc: 入口函式
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
//註冊視窗類
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
L"ClassName", NULL };
RegisterClassEx( &wc );
//建立視窗
HWND hWnd = CreateWindow( L"ClassName", L"影象二值化", 
WS_OVERLAPPEDWINDOW, 200, 100, WIDTH, HEIGHT,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
//初始化Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{ 
//建立場景圖形
if( SUCCEEDED( InitGeometry() ) )
{
//顯示視窗
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
//進入訊息迴圈
MSG msg; 
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();  //渲染場景
}
}
}
}
UnregisterClass( L"ClassName", wc.hInstance );
return 0;
}

Effect程式碼如下:

/********************************
*  Author: rabbit729
*  E-mail: [email protected]
*  Date:   2011-09-20
********************************/
//------------------------------
//  頂點著色器
//------------------------------
float viewport_inv_width;
float viewport_inv_height;
struct VS_OUTPUTSCREEN {
float4 Pos: POSITION;
float2 texCoord: TEXCOORD0;
};
VS_OUTPUTSCREEN vs_mainPassScreen(float4 Pos: POSITION){
VS_OUTPUTSCREEN Out;
Out.Pos = float4(Pos.xy, 0, 1);
Out.texCoord.x = 0.5 * (1   Pos.x - viewport_inv_width);
Out.texCoord.y = 0.5 * (1 - Pos.y - viewport_inv_height);
return Out;
}
//------------------------------
//  畫素著色器
//------------------------------
Texture2D TexScreen;
sampler2D TexMapScreen {
Texture = <TexScreen>;
};
float4 ps_mainPassScreen(float2 texCoord: TEXCOORD0) : COLOR 
{
float4 col = tex2D(TexMapScreen,  texCoord);
float Intensity;
// 灰度化
// I = 0.299*R   0.587*G   0.184*B
Intensity = 0.299*col.r   0.587*col.g   0.184*col.r;
// 閾值為0.5,關於怎麼選取閾值有好多方法,大家可以自行查閱
if(Intensity > 0.5)
Intensity = 0.5f;
else
Intensity = 0.1f;
return float4(Intensity.xxx,col.a);
}
technique Screen
{
pass P0
{
VertexShader = compile vs_3_0 vs_mainPassScreen();
PixelShader  = compile ps_3_0 ps_mainPassScreen();
}
}

 結果:

原始圖片:

二值化後結果: