Derivatives not holding their definition when generated with a function

I'm trying to create a method to generate the function and the partial derivatives of an image. The problem is that, when executed, the derivatives won't hold their definitions.

As I see it, dI1x, dI1y, dI2x, dI2y aren't functions. I1 and I2 have no problem at being evaluated with any value of x and y whereas, in this example, dI1x when evaluated appears this way dI1x[1, 2].

Thanks for your time, I'm really lost. Any hint would be appreciated.

(* imgen input: ibigsize, isize, scale *)
(* imgen output: {{Functions}, {images}} *)

imgen[ibigsize_, isize_, scale_, a_] := Block[
{ibighalf, ihalf, i1big0, r1, r2, i1big, i2big, i1dim, i2dim, i1,
i2, I1, I2, dI1x, dI1y, dI2x, dI2y}, (

ibighalf = Ceiling[N[ibigsize/2]];
ihalf = Ceiling[N[isize/2]];

i1big0 = Blur[RandomImage[{0 - 0.75, 1 + 0.75}, ibigsize], 3];

(* Rotation Transform, center *)
r1 = N[RotationTransform[0 \[Degree]]];
r2 = N[
TranslationTransform[{ibighalf[[1]], ibighalf[[2]]}] .
RotationTransform[a \[Degree]] .
TranslationTransform[-{ibighalf[[1]], ibighalf[[2]]}]];

i1big =
ImageTransformation[i1big0, r1, DataRange -> Full,
Resampling -> "Cubic", Padding -> "Reflected"];
i2big =
ImageTransformation[i1big0, r2, DataRange -> Full,
Resampling -> "Cubic", Padding -> "Reflected"];

i1 = ImageTake[i1big,
{ibighalf[[1]] - ihalf[[1]],
ibighalf[[1]] + ihalf[[1]]}, {ibighalf[[2]] - ihalf[[2]],
ibighalf[[2]] + ihalf[[2]]}];
i2 = ImageTake[i2big,
{ibighalf[[1]] - ihalf[[1]],
ibighalf[[1]] + ihalf[[1]]}, {ibighalf[[2]] - ihalf[[2]],
ibighalf[[2]] + ihalf[[2]]}];

i1dim = ImageDimensions[i1];
i2dim = ImageDimensions[i2];

i1 = ImageResize[i1, scale*{i1dim[[1]], i1dim[[2]]}];
i2 = ImageResize[i2, scale*{i2dim[[1]], i2dim[[2]]}];

I1 = ListInterpolation[Transpose[ImageData[i1]],
Method -> "Spline", InterpolationOrder -> 3];
I2 = ListInterpolation[Transpose[ImageData[i2]],
Method -> "Spline", InterpolationOrder -> 3];

(* First Derivatives *)

dI1x[x_, y_] := D[I1[x, y], x];
dI1y[x_, y_] := D[I1[x, y], y];
dI2x[x_, y_] := D[I2[x, y], x];
dI2y[x_, y_] := D[I2[x, y], y];

{{I1, I2, dI1x, dI1y, dI2x, dI2y}, {i1, i2}}

)];


This are the results i get when i run the function imgen:

ibigsize = {99, 99};
isize = {20, 20};

Idata = imgen[ibigsize, isize, 1, 1];
Ifunctions = Idata[[1]]; (*{I1, I2, dI1x, dI1y, dI2x, dI2y}*)
im = Idata[[2]]; (*{i1, i2}*)

Ifunctions
Ifunctions[[1]][1, 2]
Ifunctions[[3]][1, 2]
idim

(* out *)

{Interpolatingfunction[], Interpolatingfunction[], dI1x, dI1y, dI2x, dI2y}
0.452859
dI1x[1, 2]
{21, 21}


• Have you already seen DerivativeFilter[]? Commented Feb 12, 2021 at 6:56
• I hadn't, but will take a look! The issue is that I need a continuous function to work within the next steps. Maybe I could get this, and then interpolate a function for it? Commented Feb 12, 2021 at 7:42

Define the derivatives as pure functions, like:

dI1x := Derivative[1, 0][I1];
dI1y = Derivative[0, 1][I1];
dI2x := Derivative[1, 0][I2];
dI2y := Derivative[0, 1][I2];


Then e.g.:

SeedRandom[1];
ibigsize = {99, 99};
isize = {20, 20};
Idata = imgen[ibigsize, isize, 1, 1];

Idata[[1, 3]][1, 1]

(*-0.123381*)

• Thanks! this worked :) Commented Feb 12, 2021 at 15:07