Giter VIP home page Giter VIP logo

imagekernels's Introduction

import torch
import cv2
import numpy as np
from matplotlib import pyplot
img = cv2.imread('lenna.png')
img = cv2.imread('pycharm.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

pyplot.imshow(img)

png

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
pyplot.imshow(img, cmap="gray")

png

img = torch.from_numpy(img.reshape(1, 1, *img.shape) / 255.)
img = img.type(torch.FloatTensor)
print(img.shape)
torch.Size([1, 1, 32, 32])
def convolve(img, kernel):
    kernel = torch.tensor(kernel)
    kernel = kernel.type(torch.FloatTensor)
    kernel = kernel.view(1, 1, *kernel.shape).repeat(1, 1, 1, 1)
    return torch.conv2d(img, kernel, padding=0)
# Identity
convolved = convolve(img,
                     [
                         [0., 0., 0.],
                         [0., 1., 0.],
                         [0., 0., 0.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Edge detection 2 (Laplace 8)
convolved = convolve(img,
                     [
                         [-1., -1., -1.],
                         [-1., 8., -1.],
                         [-1., -1., -1.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Edge detection 2 (Prewitt vertical)
prewitt_v = convolve(img,
                     [
                         [1., 0., -1.],
                         [1., 0., -1.],
                         [1., 0., -1.]
                     ]
                    )
pyplot.imshow(prewitt_v[0, 0], cmap="gray")

png

# Edge detection 2 (Prewitt hotizontal)
prewitt_h = convolve(img,
                     [
                         [1., 1., 1.],
                         [0., 0., 0.],
                         [-1., -1., -1.]
                     ]
                    )
pyplot.imshow(prewitt_h[0, 0], cmap="gray")

png

prewitt_combined = pow(pow(prewitt_h, 2) + pow(prewitt_v, 2), 0.5)
pyplot.imshow(prewitt_combined[0, 0], cmap="gray")

png

# Edge detection 2 (Sobel vertical)
sobel_v = convolve(img,
                     [
                         [1., 0., -1.],
                         [2., 0., -2.],
                         [1., 0., -1.]
                     ]
                    )
pyplot.imshow(sobel_v[0, 0], cmap="gray")

png

# Edge detection 2 (Sobel hotizontal)
sobel_h = convolve(img,
                     [
                         [1., 2., 1.],
                         [0., 0., 0.],
                         [-1., -2., -1.]
                     ]
                    )
pyplot.imshow(sobel_h[0, 0], cmap="gray")

png

sobel_combined = pow(pow(sobel_h, 2) + pow(sobel_v, 2), 0.5)
pyplot.imshow(sobel_combined[0, 0], cmap="gray")

png

# Sharpen 1
convolved = convolve(img,
                     [
                         [-1., -1., -1.],
                         [-1., 9., -1.],
                         [-1., -1., -1.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Sharpen 2
convolved = convolve(img,
                     [
                         [0., -1., 0.],
                         [-1., 5., -1.],
                         [0., -1., 0.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Emboss
convolved = convolve(img,
                     [
                         [-2., -1., -1.],
                         [-1., 1., 1.],
                         [0., 1., 2.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Box blur
convolved = convolve(img,
                     [
                         [1., 1., 1.],
                         [1., 1., 1.],
                         [1., 1., 1.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# Gaussian blur
convolved = convolve(img,
                     [
                         [1., 2., 1.],
                         [2., 4., 2.],
                         [1., 2., 1.]
                     ]
                    )
pyplot.imshow(convolved[0, 0], cmap="gray")

png

# a little more complex kernel cropped from the image
kernel = cv2.imread("C.png")
kernel = cv2.cvtColor(kernel, cv2.COLOR_BGR2GRAY)
pyplot.imshow(kernel, cmap="gray")

png

convolved = convolve(img, kernel)
# convolved = torch.max_pool2d(convolved, 3)
pyplot.imshow(convolved[0, 0], cmap="gray")

png

kernel = torch.tensor(kernel)
kernel = kernel.view(1, 1, *kernel.shape).repeat(1, 1, 1, 1)
kernel = kernel.type(torch.FloatTensor)
transpose = torch.conv_transpose2d(convolved, kernel)

pyplot.imshow(transpose[0, 0], cmap="gray")

png

pyplot.imshow(img[0, 0], cmap="gray")

png

imagekernels's People

Contributors

ffabi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.