Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "image_gradients.h"
133 changes: 133 additions & 0 deletions src/DlibDotNet.Native/dlib/image_transforms/image_gradients.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#ifndef _CPP_IMAGE_GRADIENTS_H_
#define _CPP_IMAGE_GRADIENTS_H_

#include "../export.h"
#include <dlib/image_transforms/spatial_filtering.h>
#include <dlib/image_transforms/edge_detector.h>
#include "../template.h"
#include "../shared.h"

using namespace dlib;
using namespace std;

#pragma region template

#define image_gradients_gradient_x_template(__TYPE__, error, type, ...) \
*((dlib::rectangle*)valid_area) = gradients->gradient_x(*((array2d<__TYPE__>*)in_image), *((array2d<float>*)out_image));

#define image_gradients_gradient_y_template(__TYPE__, error, type, ...) \
*((dlib::rectangle*)valid_area) = gradients->gradient_y(*((array2d<__TYPE__>*)in_image), *((array2d<float>*)out_image));

#define image_gradients_gradient_xx_template(__TYPE__, error, type, ...) \
*((dlib::rectangle*)valid_area) = gradients->gradient_xx(*((array2d<__TYPE__>*)in_image), *((array2d<float>*)out_image));

#define image_gradients_gradient_xy_template(__TYPE__, error, type, ...) \
*((dlib::rectangle*)valid_area) = gradients->gradient_xy(*((array2d<__TYPE__>*)in_image), *((array2d<float>*)out_image));

#define image_gradients_gradient_yy_template(__TYPE__, error, type, ...) \
*((dlib::rectangle*)valid_area) = gradients->gradient_yy(*((array2d<__TYPE__>*)in_image), *((array2d<float>*)out_image));

#pragma endregion template

DLLEXPORT dlib::image_gradients* get_image_gradients(long scale)
{
return new dlib::image_gradients(scale);
}

DLLEXPORT int image_gradients_gradient_x(dlib::image_gradients* gradients,
array2d_type type,
void* in_image,
void* out_image,
void* valid_area)
{
int error = ERR_OK;

array2d_nonalpha_template(type,
error,
image_gradients_gradient_x_template,
in_image,
out_image,
valid_area);

return error;
}

DLLEXPORT int image_gradients_gradient_y(dlib::image_gradients* gradients,
array2d_type type,
void* in_image,
void* out_image,
void* valid_area)
{
int error = ERR_OK;

array2d_nonalpha_template(type,
error,
image_gradients_gradient_y_template,
in_image,
out_image,
valid_area);

return error;
}

DLLEXPORT int image_gradients_gradient_xx(dlib::image_gradients* gradients,
array2d_type type,
void* in_image,
void* out_image,
void* valid_area)
{
int error = ERR_OK;

array2d_nonalpha_template(type,
error,
image_gradients_gradient_xx_template,
in_image,
out_image,
valid_area);

return error;
}

DLLEXPORT int image_gradients_gradient_xy(dlib::image_gradients* gradients,
array2d_type type,
void* in_image,
void* out_image,
void* valid_area)
{
int error = ERR_OK;

array2d_nonalpha_template(type,
error,
image_gradients_gradient_xy_template,
in_image,
out_image,
valid_area);

return error;
}


DLLEXPORT int image_gradients_gradient_yy(dlib::image_gradients* gradients,
array2d_type type,
void* in_image,
void* out_image,
void* valid_area)
{
int error = ERR_OK;

array2d_nonalpha_template(type,
error,
image_gradients_gradient_yy_template,
in_image,
out_image,
valid_area);

return error;
}

DLLEXPORT void image_gradients_delete(dlib::image_gradients* obj)
{
delete obj;
}

#endif
98 changes: 98 additions & 0 deletions src/DlibDotNet/ImageTransforms/ImageGradients.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using DlibDotNet.Extensions;

// ReSharper disable once CheckNamespace
namespace DlibDotNet
{

public sealed class ImageGradients : DlibObject
{

#region Constructors

public ImageGradients(long scale = 1)
{
this.NativePtr = NativeMethods.get_image_gradients(scale);
}

#endregion

#region Methods

public Rectangle GetGradientX(Array2DBase image, Array2D<float> gradient)
{
return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_x);
}
public Rectangle GetGradientY(Array2DBase image, Array2D<float> gradient)
{
return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_y);
}

public Rectangle GetGradientXY(Array2DBase image, Array2D<float> gradient)
{
return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_xy);
}

public Rectangle GetGradientXX(Array2DBase image, Array2D<float> gradient)
{
return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_xx);
}

public Rectangle GetGradientYY(Array2DBase image, Array2D<float> gradient)
{
return InvokeGetGradient(image, gradient, NativeMethods.image_gradients_gradient_yy);
}


private Rectangle InvokeGetGradient(Array2DBase image, Array2D<float> gradient, Func<IntPtr, NativeMethods.Array2DType, IntPtr, IntPtr, IntPtr, NativeMethods.ErrorType> gradientMethod)
{
this.ThrowIfDisposed();

if (image == null)
throw new ArgumentNullException(nameof(image));

if (gradient == null)
throw new ArgumentNullException(nameof(gradient));

image.ThrowIfDisposed();
gradient.ThrowIfDisposed();

using(var rect = new Rectangle.NativeRectangle())
{
var inType = image.ImageType.ToNativeArray2DType();

var ret = gradientMethod(this.NativePtr, inType, image.NativePtr, gradient.NativePtr, rect.NativePtr);

switch (ret)
{
case NativeMethods.ErrorType.Array2DTypeTypeNotSupport:
throw new ArgumentException($"Input {inType} is not supported.");
}

return rect.ToManaged();
}
}

#region Overrides

/// <summary>
/// Releases all unmanaged resources.
/// </summary>
protected override void DisposeUnmanaged()
{
base.DisposeUnmanaged();

if (this.NativePtr == IntPtr.Zero)
return;

NativeMethods.image_gradients_delete(this.NativePtr);
}

#endregion

#endregion

}

}
55 changes: 55 additions & 0 deletions src/DlibDotNet/PInvoke/ImageTransforms/ImageGradients.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Runtime.InteropServices;

// ReSharper disable once CheckNamespace
namespace DlibDotNet
{

internal sealed partial class NativeMethods
{

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern IntPtr get_image_gradients(long scale);

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern ErrorType image_gradients_gradient_x(IntPtr gradients,
Array2DType imgType,
IntPtr in_img,
IntPtr out_img,
IntPtr valid_rectangle);

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern ErrorType image_gradients_gradient_y(IntPtr gradients,
Array2DType imgType,
IntPtr in_img,
IntPtr out_img,
IntPtr valid_rectangle);

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern ErrorType image_gradients_gradient_xx(IntPtr gradients,
Array2DType imgType,
IntPtr in_img,
IntPtr out_img,
IntPtr valid_rectangle);


[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern ErrorType image_gradients_gradient_xy(IntPtr gradients,
Array2DType imgType,
IntPtr in_img,
IntPtr out_img,
IntPtr valid_rectangle);

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern ErrorType image_gradients_gradient_yy(IntPtr gradients,
Array2DType imgType,
IntPtr in_img,
IntPtr out_img,
IntPtr valid_rectangle);

[DllImport(NativeLibrary, CallingConvention = CallingConvention)]
public static extern void image_gradients_delete(IntPtr gradients);

}

}