图像梯度计算-compute image gradient
时间:2011-01-08 来源:pixels
运行在GPU上面,大大加速的处理速度。这里,给大家做个演示。下图是演示结果,输入图像
大小为1920x1080,在我的上网本上面(显卡为HD 3200)能够达到10多帧每秒。
源代码如下:

<graph name = "SobelEdgeDetection" xmlns="http://ns.adobe.com/PixelBenderGraph/1.0">
<metadata name = "namespace" value = "Image Processing"/>
<metadata name = "vendor" value = "Terry" />
<metadata name = "version" type = "int" value = "1" />
<!-- Image inputs and outputs of the graph -->
<inputImage type = "image4" name = "src" />
<outputImage type = "image4" name = "dst" />
<!-- Embedded kernel -->
<kernel>
<![CDATA[
<languageVersion : 1.0;>
kernel Gray
<
namespace:"Image Processing";
vendor:"Terry";
version:1;
>
{
input image4 src;
output float4 dst;
void evaluatePixel()
{
float4 color = sample(src, outCoord());
float gray = color.x*0.212671 + 0.715160*color.y + 0.072169*color.z;
dst.xyz = float3(gray, gray, gray);
dst.w = 1.0;
}
}
]]>
</kernel>
<!-- Embedded kernel -->
<kernel>
<![CDATA[
<languageVersion : 1.0;>
kernel Dx
<
namespace:"Image Processing";
vendor:"Terry";
version:1;
>
{
input image4 src;
output float4 dst;
void evaluatePixel()
{
float3 dx = sampleNearest(src, outCoord() + float2(1.0, 0.0)).rgb - sampleNearest(src, outCoord() + float2(-1.0,0.0)).rgb;
dst.xyz = dx;
dst.w = 1.0;
}
}
]]>
</kernel>
<kernel>
<![CDATA[
<languageVersion : 1.0;>
kernel Dy
<
namespace:"Image Processing";
vendor:"Terry";
version:1;
>
{
input image4 src;
output float4 dst;
void evaluatePixel()
{
float3 dy = sampleNearest(src, outCoord() + float2(0.0, 1.0)).rgb - sampleNearest(src, outCoord() + float2(0.0,-1.0)).rgb;
dst.xyz = dy;
dst.w = 1.0;
}
}
]]>
</kernel>
<kernel>
<![CDATA[
<languageVersion : 1.0;>
kernel SquareRoot
<
namespace:"Image Processing";
vendor:"Terry";
version:1;
>
{
input image4 src1;
input image4 src2;
output float4 dst;
void evaluatePixel()
{
float squareRoot = sqrt(sampleNearest(src1, outCoord()).r*sampleNearest(src1, outCoord()).r +
sampleNearest(src2, outCoord()).r*sampleNearest(src2, outCoord()).r);
dst.xyz = float3(squareRoot, squareRoot, squareRoot);
dst.w = 1.0;
}
}
]]>
</kernel>
<!-- Instances of the nodes -->
<node id = "gray1" name ="Gray" namespace = "Image Processing" vendor = "Terry" version ="1" >
</node>
<node id = "dx1" name ="Dx" namespace = "Image Processing" vendor = "Terry" version ="1" >
</node>
<node id = "dy1" name ="Dy" namespace = "Image Processing" vendor = "Terry" version ="1" >
</node>
<node id = "squareRoot1" name ="SquareRoot" namespace = "Image Processing" vendor = "Terry" version ="1" >
</node>
<!-- Connect the graph -->
<connect fromImage = "src" toNode = "gray1" toInput = "src" />
<connect fromNode="gray1" fromOutput="dst" toNode="dx1" toInput="src"/>
<connect fromNode="gray1" fromOutput="dst" toNode="dy1" toInput="src"/>
<connect fromNode="dx1" fromOutput="dst" toNode="squareRoot1" toInput="src1"/>
<connect fromNode="dy1" fromOutput="dst" toNode="squareRoot1" toInput="src2"/>
<connect fromNode = "squareRoot1" fromOutput = "dst" toImage = "dst" />
</graph>
相关阅读 更多 +
排行榜 更多 +