Skip to main content

Applying some image kernel(s) on a grayscale or RGB color-scale image

Project description

Pyimkernel

License: MIT GitHub repo size GitHub forks GitHub User's stars GitHub pull requests GitHub issues

With this package, You can apply various image kernels such as Blur, Sobel, Scharr, and so forth (The comprehensive list of image kernels is mentioned below) on a grayscale or RGB color-scale image, and then show them. These effects and enhancements in digital images can be achieved using the "ApplyKernels" class, allowing for a wide range of transformations.

This package helps you:

  • to understand what happens when applying kernels to images.
  • to address the issue of limited data by increasing image quantities.
  • to develop end-to-end applications.

If you are interested in learning more about the package, I encourage you to check out main.py.

About Dependencies

To develop this package, I used the following libraries:

  • numpy : for working with arrays and Kernels.
  • matplotlib : for visualizing images.
  • opencv-python : for working with images and applying kernels to images.

Image kernels

You can use built-in image kernels which have the shape of (3, 3) or (5, 5) or use your desired kernel value. To see how you can apply kernels to images, You can check out the usage section or the file Image Kernels.ipynb.

The supported Image kernels are listed below:

  • guassian blur or blur : The blur kernel applies a smoothing effect, averaging the pixel values in the neighborhood.
  • bottom sobel : The bottom sobel kernel emphasizes edges in the bottom directions.
  • emboss : The emboss kernel creates a 3D embossed effect in the image.
  • identity : The identity kernel does not modify the image and leaves it unchanged.
  • left sobel : The left sobel kernel emphasizes edges in the left directions.
  • outline : The outline kernel detects edges and boundaries by emphasizing the differences in intensity between neighboring pixels.
  • right sobel : The right sobel kernel emphasizes edges in the right directions.
  • sharpen : The sharpen kernel enhances edges and details in an image.
  • top sobel : The top sobel kernel emphasizes edges in the top directions.
  • horizontal edge : The horizontal edge kernel highlights horizontal edges.
  • vertical edge : The vertical edge kernel highlights vertical edges.
  • box blur : The box blur kernel is similar to the blur kernel. It applies a simple averaging operation to create a blur effect, but with equal weights for all neighboring pixels.
  • laplacian : The Laplacian kernel is used for edge detection and image sharpening.
  • prewitt horizontal edge : The prewitt horizontal edge kernel is similar to the bottom sobel kernel, emphasizing edges in the horizontal directions.
  • prewitt vertical edge : The prewitt vertical edge kernel is similar to the right sobel kernel, emphasizing edges in the horizontal directions.
  • high-pass filter : The high-pass filter kernel enhances the details and edges in an image while reducing the low-frequency components.
  • unsharp masking : The unsharp masking kernel is used for image sharpening. It enhances the edges and details in an image by subtracting a blurred version of the image from the original.
  • dilation : The dilation kernel expands regions of bright pixels in an image.
  • soften : The soften kernel is used to reduce image noise and create a smoother appearance while preserving overall image details.
  • scharr horizontal edge : The scharr horizontal edge kernel is used for edge detection and gradient estimation along the horizontal direction. It provides more weight to the central pixel and its immediate neighbors.
  • scharr vertical edge : The scharr vertical edge kernel is used for edge detection and gradient estimation along the vertical direction. It provides more weight to the central pixel and its immediate neighbors.
  • motion blur : The motion blur kernel is used to simulate the effect of motion in an image. It achieves this by applying a linear blur in a specific direction. The kernel consists of non-zero values along a line in the direction of motion, with zeros elsewhere.
  • robert horizontal edge : A simple and efficient edge detection operator to detect horizontal edges. The kernels consist of positive and negative values that highlight the change in intensity along the respective directions.
  • robert vertical edge : A simple and efficient edge detection operator to detect vertical edges. The kernels consist of positive and negative values that highlight the change in intensity along the respective directions.
  • ridge detection1 or edge detection1: In this kernel, the weights are increased towards the center to emphasize the ridge-like structures in the image.
  • ridge detection2 or edge detection2: This kernel is designed to enhance ridge-like structures in the image. The central value is higher (8) to emphasize the ridge, while the surrounding values are negative (-1) to suppress the surrounding areas.

Pyimkernel Installation

pip install pyimkernel

Usage

from pyimkernel import ApplyKernels
import mnist # pip install mnist
import cv2
import os

# Load data
X_train, X_test, y_train, y_test = mnist.train_images(), mnist.test_images(), mnist.train_labels(), mnist.test_labels()

# Create an instance
imkernel = ApplyKernels(random_seed=0)

# Grayscale
# Show image 9 
imkernel.imshow(
  X_train[19],
  cmap=plt.cm.gray
)

# Apply the 3x3 blur kernel to a grayscale image 9
filtered_image = imkernel.apply_filter_on_gray_img(
  X_train[19],
  kernel_name='blur',
  kernel_size=(3, 3)
)

# Show the filtered image 9
imkernel.imshow(
  image=filtered_image,
  cmap='gray'
)

# 5x5 Prewitt Horizontal Edge Kernel
kernel_value = np.array([[-1, -1, -1, -1, -1],
                         [-1, -1, -1, -1, -1],
                         [0,   0,  0,  0,  0],
                         [1,   1,  1,  1,  1],
                         [1,   1,  1,  1,  1]])

# First apply the 5x5 Prewitt kernel to image 9 and then show the filtered image
imkernel.imshow(
    image=imkernel.apply_filter_on_gray_img(
        X_train[19],
        kernel_name='custom',
        kernel_value=kernel_value
    ),
    cmap=plt.cm.gray
)

# the Color-Scale image
# Read the flower image
image1 = cv2.imread(
    os.path.join('Images', '1.jpg')
)

# Show the flower image
imkernel.imshow(
    cv2.cvtColor(image1, cv2.COLOR_BGR2RGB),
    figsize=(5, 5)
)

# Show the filtered flower image
# 5x5 box blur
kernel_value = (1 / 25.0) * np.array([[1, 1, 1, 1, 1],
                                      [1, 1, 1, 1, 1],
                                      [1, 1, 1, 1, 1],
                                      [1, 1, 1, 1, 1],
                                      [1, 1, 1, 1, 1]])
# Apply the 5x5 box blur kernel to image1
blurred_image = imkernel.apply_filter_on_color_img(
    image1,
    kernel_name='custom',
    kernel_value=kernel_value,
    with_resize=True,
    dsize=(100, 100)
)
# Show the blurred image
imkernel.imshow(
    image=cv2.cvtColor(
        blurred_image,
        cv2.COLOR_BGR2RGB
    ))

# Apply the 3x3 sharpen kernel to image1
sharpened_image = imkernel.apply_filter_on_color_img(
    image1,
    kernel_name='sharpen',
    kernel_size=(3, 3),
    with_resize=True
)
# Show the sharpened image
imkernel.imshow(
    image=cv2.cvtColor(
        sharpened_image,
        cv2.COLOR_BGR2RGB
    ))

# First Apply the 5x5 soften kernel to a sharpened image and then show the filtered image
imkernel.imshow(
    image=cv2.cvtColor(
        imkernel.apply_filter_on_color_img(
            sharpened_image, 
            kernel_name='soften',
            kernel_size=(5, 5)
        ),
    cv2.COLOR_BGR2RGB
    ))

# First Apply the 3x3 unsharp masking kernel on a sharpened image and then show the filtered image
imkernel.imshow(
    image=cv2.cvtColor(
        imkernel.apply_filter_on_color_img(
            sharpened_image,
            kernel_name='unsharp masking',
            kernel_size=(3, 3)
        ),
    cv2.COLOR_BGR2RGB
    ))

The Grayscale Output

Before Applying Kernels on a grayscale image 9



After Applying the 3x3 Blur Kernel on a grayscale image 9

After Applying the 5x5 Prewitt Horizontal Edge Kernel on a grayscale image 9

The Color-Scale Output

Before Applying Kernels on a color-scale flower image



After Applying the 5x5 Box Blur Kernel on a color-scale flower image and assigning True to the with_resize parameter and (100, 100) to the dsize parameter



After Applying the 3x3 sharpen kernel on a color-scale flower image and assigning True to the with_resize parameter and 'auto' to the dsize parameter



After Applying the 5x5 soften kernel on a color-scale image, which was filtered using the Sharpen Kernel before, and assigning False to the with_resize parameter



After Applying the 3x3 unsharp masking kernel on a color-scale image, which was filtered using the Sharpen Kernel before, and assigning False to the with_resize parameter

Package Status

All Tests Passed Successfully.

About References

To develop the package, I used the Setosa, Wikipedia, and ChatGPT websites to gather kernel values and utilized other websites to ensure that the information I collected was accurate and properly attributed.

Appreciation

I want to thank @sugizo for providing valuable and insightful ideas that helped me improve the package.

Maintainer Contact

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyimkernel-1.2.0.tar.gz (2.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyimkernel-1.2.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file pyimkernel-1.2.0.tar.gz.

File metadata

  • Download URL: pyimkernel-1.2.0.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for pyimkernel-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a7d57aa534eac8c828ec27e02f554efb4dc9cd8e04de87f63c4aca5b9f8d6334
MD5 ff30adae370cd6bc5373be4b1394602e
BLAKE2b-256 f2a6172101abd4d017a829dceb767d470ac8bb246ad5d25d151f6ffd3b9b06dd

See more details on using hashes here.

File details

Details for the file pyimkernel-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyimkernel-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for pyimkernel-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de3fe8437fc8a28f781a8199f2132083c015ed984db2f102fb19a3c3699e63de
MD5 a4519ea317c55d39267cbe3ca672fb22
BLAKE2b-256 63bf995f9bc0ffddd0af3b4f600eacb98de38b383f1bbe5c024aa56203751488

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page