/// <summary> /// Any pixel values below threshold will be changed to 0. /// Any pixel values above (or equal to) threshold will be changed to 1. /// </summary> /// <typeparam name="T">Image type to process and return</typeparam> /// <param name="image">Image to process</param> /// <param name="threshold">Binary threshold</param> /// <returns>New image as binary</returns> public static T AsImage <T>(T image, byte threshold) where T : Image { Bitmap binaryBitmap = AsBitmap(image, threshold); // Convert to original format return((T)ImageFormatting.ToFormat(binaryBitmap, image.RawFormat)); }
/// <summary> /// Applies multiple convolution kernels to source image, then combines the results. /// </summary> /// <typeparam name="T">Image type to process and return</typeparam> /// <param name="image">Image to apply kernel to</param> /// <param name="convolutionTypes">Convolutions to apply</param> /// <returns>Image with all kernels combined</returns> public static T ApplyKernelsThenCombine <T>(T image, params ConvolutionType[] convolutionTypes) where T : Image { if (image is Bitmap bmp) { return((T)(Image)ApplyKernelsThenCombine(bmp, convolutionTypes)); } else { // Convert to bitmap for image processing using (Bitmap bitmap = ImageFormatting.ToBitmap(image)) { using (Bitmap combinedBitmap = ApplyKernelsThenCombine(bitmap, convolutionTypes)) { // Return processed image to original format return((T)ImageFormatting.ToFormat(combinedBitmap, image.RawFormat)); } } } }
/// <summary> /// Applies convolution matrix/kernel to image. /// </summary> /// <typeparam name="T">Image type to process and return</typeparam> /// <param name="image">Image to apply kernel to</param> /// <param name="kernelMatrix">Kernel matrix</param> /// <returns>Image with kernel applied</returns> public static T ApplyKernel <T>(T image, int[,] kernelMatrix) where T : Image { // Skip conversion if (image is Bitmap bmp) { return((T)(Image)ApplyKernel(bmp, kernelMatrix)); } else { // Convert to bitmap for image processing using (Bitmap bitmap = ImageFormatting.ToBitmap(image)) { // Apply filter to bitmap using (Bitmap bitmapWithFilter = ApplyKernel(bitmap, kernelMatrix)) { // Covert processed image to original format return((T)ImageFormatting.ToFormat(bitmapWithFilter, image.RawFormat)); } } } }
/// <summary> /// Crops an image based on the crop region. /// </summary> /// <typeparam name="T">Image type to process and return</typeparam> /// <param name="image">Image to crop</param> /// <param name="cropRegion">Region within image to crop</param> /// <returns>Cropped image</returns> public static T ByRegion <T>(T image, Rectangle cropRegion) where T : Image { // Check if already a bitmap if (image is Bitmap bmp) { return((T)(Image)CropBitmap(bmp, cropRegion)); } else { // Convert to bitmap using (Bitmap bitmap = ImageFormatting.ToBitmap(image)) { // Crop as bitmap using (Image croppedBitmap = CropBitmap(bitmap, cropRegion)) { // Restore original image format return((T)ImageFormatting.ToFormat(croppedBitmap, image.RawFormat)); } } } }