# Create Derivative Kernel#

## Synopsis#

Create a derivative kernel.

## Results#

Output:

```Size: [3, 3]
Neighborhood:
Size:[3, 3]
DataBuffer:NeighborhoodAllocator { this = 0x7ffeec8f19e8, begin = 0x7f822c2a8f00, size=9 }
[-1, -1] 0
[0, -1] 0
[1, -1] 0
[-1, 0] 0.5
[0, 0] 0
[1, 0] -0.5
[-1, 1] 0
[0, 1] 0
[1, 1] 0
```

## Code#

### Python#

```#!/usr/bin/env python

import itk

derivativeOperator = itk.DerivativeOperator[itk.F, 2]()
derivativeOperator.SetDirection(0)  # Create the operator for the X axis derivative

print("Size: " + str(derivativeOperator.GetSize()))

print(derivativeOperator)

for i in range(9):
print(
str(derivativeOperator.GetOffset(i))
+ " "
+ str(derivativeOperator.GetElement(i))
)
```

### C++#

```#include <itkDerivativeOperator.h>

int
main()
{
using DerivativeOperatorType = itk::DerivativeOperator<float, 2>;
DerivativeOperatorType derivativeOperator;
derivativeOperator.SetDirection(0); // Create the operator for the X axis derivative

std::cout << "Size: " << derivativeOperator.GetSize() << std::endl;

std::cout << derivativeOperator << std::endl;

for (unsigned int i = 0; i < 9; ++i)
{
std::cout << derivativeOperator.GetOffset(i) << " " << derivativeOperator.GetElement(i) << std::endl;
}
return EXIT_SUCCESS;
}
```

## Classes demonstrated#

template<typename TPixel, unsigned int VDimension = 2, typename TAllocator = NeighborhoodAllocator<TPixel>>
class DerivativeOperator : public itk::NeighborhoodOperator<TPixel, VDimension, TAllocator>

A NeighborhoodOperator for taking an n-th order derivative at a pixel.

DerivativeOperator’s coefficients are a tightest-fitting convolution kernel for calculating the n-th order directional derivative at a pixel. DerivativeOperator is a directional NeighborhoodOperator that should be applied to a Neighborhood or NeighborhoodPointer using the inner product method.

An example operator to compute X derivatives of a 2D image can be created with:

```using DerivativeOperatorType = itk::DerivativeOperator<float, 2>;
DerivativeOperatorType derivativeOperator;
derivativeOperator.SetDirection(0); // X dimension
radius.Fill(1); // A radius of 1 in both dimensions is a 3x3 operator
```
and creates a kernel that looks like:
```0        0 0
0.5  0   -0.5
0    0   0
```

Note

DerivativeOperator does not have any user-declared “special member function”, following the C++ Rule of Zero: the compiler will generate them if necessary.

See

NeighborhoodOperator

See

Neighborhood

See

ForwardDifferenceOperator

See

BackwardDifferenceOperator

ITK Sphinx Examples: