/*
* Gradient computation via separable convolutions
*
* [1] Dirk-Jan Kroon, 2009, Numerical Optimization of Kernel Based Image Derivatives.
*/
#include <pjsr/ImageOp.jsh>
// Classical Sobel operator
var Derivative_Sobel = new Matrix( +1, +2, +1,
0, 0, 0,
-1, -2, -1 );
// Scharr operator (referenced in [1])
var Derivative_Scharr = new Matrix( +3, +10, +3,
0, 0, 0,
-3, -10, -3 );
// Kroon's optimized 3x3 filter [1]
var Derivative_Kroon_3x3 = new Matrix( +17, +61, +17,
0, 0, 0,
-17, -61, -17 );
// Kroon's optimized 5x5 filter [1]
var Derivative_Kroon_5x5 = new Matrix(
[+0.0007, +0.0052, +0.0370, +0.0052, +0.0007,
+0.0037, +0.1187, +0.2589, +0.1187, +0.0037,
0, 0, 0, 0, 0,
-0.0037, -0.1187, -0.2589, -0.1187, -0.0037,
-0.0007, -0.0052, -0.0370, -0.0052, -0.0007], 5, 5 );
var Derivative_Filter = Derivative_Kroon_5x5;
function main()
{
var window = ImageWindow.activeWindow;
if ( window.isNull )
throw new Error( "There is no active image." );
var view = window.currentView;
var img = view.image;
// Working images
var Ix = new Image;
img.clonePixelData( Ix );
var Iy = new Image;
img.clonePixelData( Iy );
// Derivative in the X direction
Ix.convolve( Derivative_Filter );
// Ix = Ix^2
Ix.apply( Ix, ImageOp_Mul );
// Derivative in the Y direction
Iy.convolve( Derivative_Filter.transpose() );
// Iy = Iy^2
Iy.apply( Iy, ImageOp_Mul );
// Ix = Ix^2 + Iy^2
Ix.apply( Iy, ImageOp_Add );
// Ix = (Ix^2 + Iy^2)^0.5
Ix.apply( 0.5, ImageOp_Pow );
view.beginProcess();
img.apply( Ix, ImageOp_Mov );
img.rescale();
view.endProcess();
}
main();