I Gede Andi Utama Giri - L - Tugas Deteksi Tepi

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Laporan Pratikum Deteksi Tepi

Pengolahan Citra Dan Pola

Oleh:

I Gede Andi Utama Giri (2201010101)

FAKULTAS TEKNIK INFORMATIKA


PROGRAM STUDI MANAGEMENT TEKNIK INFORMATIKA INSTITUT BISNIS
DAN TEKNOLOGI
INDONESIA
2024
Code:

import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load the images


img1_path = "gambar/One.png"
img2_path = "gambar/coins.jpg"

img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)


img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)

# Function to apply edge detection filters


def apply_edge_detection(image):
# Apply Robert, Sobel, Prewitt, and Laplacian edge detection
# Robert's Cross
roberts_cross_v = np.array([[1, 0], [0, -1]])
roberts_cross_h = np.array([[0, 1], [-1, 0]])

roberts_v = cv2.filter2D(image, -1, roberts_cross_v)


roberts_h = cv2.filter2D(image, -1, roberts_cross_h)
roberts = roberts_v + roberts_h

# Sobel
sobelx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobely = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
sobel = np.sqrt(sobelx*2 + sobely*2)

# Prewitt
kernelx = np.array([[1, 0, -1], [1, 0, -1], [1, 0, -1]])
kernely = np.array([[1, 1, 1], [0, 0, 0], [-1, -1, -1]])

prewittx = cv2.filter2D(image, -1, kernelx)


prewitty = cv2.filter2D(image, -1, kernely)
prewitt = prewittx + prewitty

# Laplacian
laplacian = cv2.Laplacian(image, cv2.CV_64F)

return roberts, sobel, prewitt, laplacian

# Apply edge detection


edges_img1 = apply_edge_detection(img1)
edges_img2 = apply_edge_detection(img2)

# Plot the results


titles = ['Original Image', 'Roberts', 'Sobel', 'Prewitt', 'Laplacian']

fig, axes = plt.subplots(2, 5, figsize=(20, 10))


axes = axes.ravel()

# Plot first image results


axes[0].imshow(img1, cmap='gray')
axes[0].set_title(titles[0])
axes[0].axis('off')

for i in range(1, 5):


axes[i].imshow(edges_img1[i-1], cmap='gray')
axes[i].set_title(titles[i])
axes[i].axis('off')

# Plot second image results


axes[5].imshow(img2, cmap='gray')
axes[5].set_title(titles[0])
axes[5].axis('off')

for i in range(1, 5):


axes[5 + i].imshow(edges_img2[i-1], cmap='gray')
axes[5 + i].set_title(titles[i])
axes[5 + i].axis('off')

plt.show()
Hasil:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy