C++HLSL实现简单的图像处理功能

软件编程 C 语言 分类:[default] 更新日期: 2017-05-24
本文主要介绍了HLSL实现简单的图像处理功能的方法,具有很好的参考价值,下面跟着小编一起来看下吧

由于对于dxva2解码得到的数据不宜copy回内存给CPU处理,所以最好的办法是在GPU上直接进行处理。D3D的像素着色器能够对像素直接进行操作,实现点运算极其简单方便,简单的卷积运算效果也非常好。但D3D9的限制也很多,对于过于复杂的图像处理则显得有些不能胜任。

1.点运算

点运算用HLSL非常容易实现,几乎是公式怎么写,代码就怎么写。以RGB转灰度图显示为例:

texture Tex0 ;
int iFlag = 0 ;
float aValue= 0.0 ;
float bValue= 0.0 ;
sampler2D YTex = 
sampler_state
{
  Texture = <Tex0> ;
  MipFilter = LINEAR ;
  MinFilter = LINEAR ;
  MagFilter = LINEAR ;

  AddressU = CLAMP ;
  AddressV = CLAMP ;
};
struct PS_INPUT
{
  float2 uvCoords0 : TEXCOORD0 ;
};
float4 Main( PS_INPUT input ) : COLOR0
{
  float4 yuvColor ;
  //rgb to gray 不知道是不是这么显示的,姑且这么认为
  float gray = tex2D( YTex, input.uvCoords0 ).r * 0.299 + tex2D( YTex, input.uvCoords0 ).g * 0.587 + tex2D( YTex, input.uvCoords0 ).b * 0.114 ;
  float s = 0 ;
  if(iFlag == 0)
  {
    s = aValue * gray + bValue/255 ;
  }
  else if(iFlag == 1)
  {
    s = aValue * log(1+gray) ;
  }
  else if(iFlag == 2)
  {
    s = aValue * pow(abs(gray),bValue) ;
  }
  yuvColor.r = s ;
  yuvColor.g = s ;
  yuvColor.b = s ;
  yuvColor.a = 1.0 ;
  return yuvColor ;
}

点运算如此简单是因为GPU是并行运算的,我个人认为可以看成是每一个像素点(BGRA)对应一个线程,这大概就是OpenCL中所谓的数据并行。这是一个非常简单的程序,指令数少,程序结构也很简单,shader 的版本用2.0就可以轻松编过。

2.卷积运算举例

指令数较多的情况2.0版本的shader就搞不定了,上3.0版本可以做一些简单的卷积运算。以中值滤波为例:

texture Tex0 ;
matrix WorldMatrix;
matrix ViewMatrix;
matrix ProjMatrix;
sampler2D YTex = 
sampler_state
{
  Texture = <Tex0> ;
  MipFilter = LINEAR ;
  MinFilter = LINEAR ;
  MagFilter = LINEAR ;
  AddressU = CLAMP ;
  AddressV = CLAMP ;
};
struct VS_INPUT
 {
  float4 pos  : POSITION;
  float4 color : COLOR0;
  float2 tex  : TEXCOORD0;
 };
// 
struct VS_OUTPUT
 {
  float4 pos   : POSITION;
  float4 color  : COLOR0;
  float2 tex   : TEXCOORD0;
 };
float2 g_v4ScreenSize;
int ksize = 1 ;
float fLeft = -1.0f ;
float fTop = -1.0f ;
float fRight = -1.0f ;
float fBottom = -1.0f ;
//--------------------------------- BurTechnique --------------------------------------
VS_OUTPUT MainVS_Screen( VS_INPUT In )
{
  VS_OUTPUT Out = ( VS_OUTPUT )0;
  float4x4 matWorldView = mul(WorldMatrix,ViewMatrix);
  float4x4 matProject = mul(matWorldView,ProjMatrix);
  Out.pos = mul(In.pos,matProject);
  Out.tex = In.tex;
  Out.color = In.color;
  return Out;
}
float4 MainPS_Screen( VS_INPUT In ) : COLOR0
{
  float4 outColor = tex2D( YTex, In.tex ).rgba ;
  if(ksize <= 1 || ksize%2 == 0)
  {
    return outColor ;
  }
  if( ksize > 11 || ksize < 3)
  {
    return outColor ;
  }
  if(!(In.tex.x < fRight && In.tex.y < fBottom && In.tex.x > fLeft && In.tex.y > fTop))
  {
    return outColor ;
  }
  // 纹理大小
  float2 TexSize = float2( g_v4ScreenSize.x , g_v4ScreenSize.y );
  float x_off = 1.0f / TexSize.x;
  float y_off = 1.0f / TexSize.y;
  float2 fX0Y0 = In.tex - float2(x_off * ksize/2, y_off*ksize/2) ;
  float3 sum = {0.0f, 0.0f, 0.0f} ;
  if(ksize >= 3)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*2)).rgb;
  }
  if(ksize >= 5)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*4)).rgb;
  }
  if(ksize >= 7)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*6)).rgb;
  }
  if(ksize >= 9)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*8)).rgb;
  }
  if(ksize >= 11)
  {
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 9, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*0)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*1)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*2)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*3)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*4)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*5)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*6)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*7)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*8)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 10, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*9)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 0, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 1, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 2, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 3, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 4, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 5, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 6, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 7, y_off*10)).rgb;
    sum += tex2D( YTex , fX0Y0 + float2(x_off * 8, y_off*10)).rgb;
  }
  outColor = float4(sum/(ksize*ksize),1.0f);
  return outColor ;
}
//--------------------------- 技术---------------------------
technique BurTechnique
{
  pass P0
  {  
    LightEnable[0] = false;
    VertexShader = compile vs_3_0 MainVS_Screen();
    PixelShader = compile ps_3_0 MainPS_Screen();
  }
}

由于3.0版本的shader似乎不允许pixel shader单独出现,所以我从点运算用像素着色器实现改为用特效来实现。HLSL语法中有if语句,也有for语句,可是这个程序却不厌其烦的把所有的都给列出来来,而没有使用for循环。这是因为在实际使用中发现有一些限制,比如if语句的if(A>B),A与B中必须有一个是常量,就像上面见到的那种形式;for循环中间的判断也是如此,只是在第二层j循环中可以是第一层循环的i,即不可以

 for(int i=0;i<ksize;i++)
{
  for(int j=0;j<ksize1;j++)
    {
  ..........
    }
}

以上代码的ksize与ksize1都必须为常数,例外的情况是ksize1可以为第一层循环的 i 。这个问题不知道后续版本的shader有没有,反正我当前使用的版本有。

另外有一个需要注意的地方是指令数,2.0版本的shader支持的指令数相当少,3.0版本则要多好多,我最长写到了400多条快500条时才导致编译失败。 还有一个需要提醒的是3.0版本的shader只支持D3D 9.0C以后的。如果要求做更为复杂的图像处理,可以的话建议上D3D11,compute shader虽然我没用过,但从介绍来说,应该可以处理一些更为复杂的图像处理。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!

相关文章
  • 简单谈谈GET和POST有什么区别
    本文给大家总结了下get与post的具体区别,以及网上的那么多答案为什么都是错误的,非常的实用,这里推荐给大家如果有人问你,GET和POST,有什么区别?你会如何回答? 真实案例      前几天有人问我这个问题.我说GET是用于获取数据的,POST,一般用于将数据发给服务器之用.     这个答案好像并不是他想要的.于是他继续追问有没有别的区别?我说这就是 ...
  • asp.netcore实现文件上传功能
    asp.netcore实现文件上传功能
    这篇文章主要为大家详细介绍了asp.net core实现文件上传功能,怎么做单文件和多文件上传,感兴趣的小伙伴们可以参考一下本文实例为大家分享了单文件上传.多文件上传的功能,供大家参考,具体内容如下 单文件上传 上传文件在Web应用程序中是一个常见的功能.在asp.net core中上传文件并保存在服务器上,是很容易的.下面就来演示一下怎么样在 ASP.NE ...
  • 将xml文件作为一个小的数据库,进行学生的增删改查的简单实例
    下面小编就为大家带来一篇将xml文件作为一个小的数据库,进行学生的增删改查的简单实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧1.xml文件: <?xml version="1.0" encoding="UTF-8"?><Students> <stude ...
  • jsp输出九九乘法表的简单实例
    jsp输出九九乘法表的简单实例
    下面小编就为大家带来一篇jsp输出九九乘法表的简单实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 <% String st = ""; for(int i = 1; i <= 9; i++){ for(int j = 1; j <= i; j++){ st += j+"x&q ...
  • gVim,gVimEasy,gVimRead-only的简单区别
    今天打算好好学习一下python,买了一本书看了介绍无语了,gVim, gVim Diff, gVim Easy, gVim Read-only 的简单区别,需要的朋友可以参考下我在Window XP中安装了Gvim7.2,然后桌面上出现了三个快捷方式,gvim72.exe,gvim read-only,gvim easy,大家谁比较熟悉介绍一下啊,谢谢了! ...
  • PHP微信开发之查询微信精选文章
    这篇文章主要为大家详细介绍了PHP微信开发之简单实现查询微信精选文章的相关资料,感兴趣的小伙伴们可以参考一下查询微信里的一些精选的,点击量比较大的文章. 别忘记申请apikey(登录百度账号即可获取),要完成的功能是: 1.用户回复"文章",公众号要返回文章分类的编号(比如9.科技). 2.用户回复wz9,1,腾讯     则能返回科技类 ...
猜你喜欢