Professional Documents
Culture Documents
Codingan Pertemuan 9 Dan 10
Codingan Pertemuan 9 Dan 10
import cv2
from matplotlib import pyplot as plt
image2 = cv2.imread("penguins.jpg")
image2 = cv2.resize(image2, (400, 250)) # ubah ukuran
# Gabung
hasil = cv2.addWeighted(image1, 0.3, image2, 0.7, 0)
hasil2= cv2.addWeighted(image1, 0.7, image2, 0.3,0)
plt.subplot(221).set_title("Citra Pertama")
plt.axis("off")
plt.imshow( cv2.cvtColor(image1, cv2.COLOR_BGR2RGB) )
plt.subplot(222).set_title("Citra Kedua")
plt.axis("off")
plt.imshow( cv2.cvtColor(image2, cv2.COLOR_BGR2RGB) )
plt.subplot(223).set_title("Hasil")
plt.axis("off")
plt.imshow( cv2.cvtColor(hasil, cv2.COLOR_BGR2RGB) )
plt.subplot(224).set_title("Hasil 2")
plt.axis("off")
plt.imshow( cv2.cvtColor(hasil2, cv2.COLOR_BGR2RGB) )
plt.tight_layout()
plt.show()
Operasi Logika
import cv2
from matplotlib import pyplot as plt
image1 = cv2.imread("koala.jpg")
image1 = cv2.resize(image1, (400, 250))
image2 = cv2.imread("penguins.jpg")
image2 = cv2.resize(image2, (400, 250))
# tampilkan
plt.subplot(221).set_title("AND")
plt.axis("off")
plt.imshow( cv2.cvtColor(image_and, cv2.COLOR_BGR2RGB) )
plt.subplot(222).set_title("OR")
plt.axis("off")
plt.imshow( cv2.cvtColor(image_or, cv2.COLOR_BGR2RGB) )
plt.subplot(223).set_title("XOR")
plt.axis("off")
plt.imshow( cv2.cvtColor(image_xor, cv2.COLOR_BGR2RGB) )
plt.subplot(224).set_title("NOT")
plt.axis("off")
plt.imshow( cv2.cvtColor(image_not, cv2.COLOR_BGR2RGB) )
plt.tight_layout()
plt.show()
Deteksi gerakan
import cv2
from matplotlib import pyplot as plt
# hitung perbedaan
difference = cv2.subtract(image1, image2)
# mask
image1[mask != 255] = [0, 0, 255]
image2[mask != 255] = [0, 0, 255]
# tampilkan
plt.subplot(221).set_title("Mask Citra 1")
plt.axis("off")
plt.imshow( cv2.cvtColor(image1, cv2.COLOR_BGR2RGB) )
plt.subplot(223).set_title("Perbedaan keduanya")
plt.axis("off")
plt.imshow( cv2.cvtColor(difference, cv2.COLOR_BGR2RGB) )
plt.tight_layout()
plt.show()
Robert
import cv2
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
def roberts_cross(img_path):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# atur threshold
threshold = 5
edges = magnitude > threshold
return edges
image_path = 'penguins.jpg'
edge_image = roberts_cross(image_path)
plt.subplot(221).set_title("Citra")
plt.axis("off")
plt.imshow(original_image_rgb)
plt.subplot(222).set_title('Robert')
plt.axis("off")
plt.imshow(edge_image, cmap='gray')
plt.tight_layout()
plt.show()
Prewitt
import cv2
import numpy as np
import matplotlib.pyplot as plt
# persiapan
image = cv2.imread('penguins.jpg', cv2.IMREAD_COLOR)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(image_gray,(3,3),0)
#prewitt
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]])
prewittx = cv2.filter2D(blur_img, -1, kernelx)
prewitty = cv2.filter2D(blur_img, -1, kernely)
plt.subplot(221).set_title("Citra")
plt.axis("off")
plt.imshow(image)
plt.subplot(222).set_title('Prewitt X')
plt.axis("off")
plt.imshow(prewittx)
plt.subplot(223).set_title('Prewitt Y')
plt.axis("off")
plt.imshow(prewitty)
plt.tight_layout()
plt.show()
Sobel
import cv2
import numpy as np
import matplotlib.pyplot as plt
# persiapan
image = cv2.imread('penguins.jpg', cv2.IMREAD_COLOR)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
plt.subplot(221).set_title("Citra")
plt.axis("off")
plt.imshow(image)
plt.subplot(222).set_title('Sobel X')
plt.axis("off")
plt.imshow(filtered_image_x)
plt.subplot(223).set_title('Sobel Y')
plt.axis("off")
plt.imshow(filtered_image_y)
plt.subplot(224).set_title('Sobel XY')
plt.axis("off")
plt.imshow(filtered_image_xy)
plt.tight_layout()
plt.show()
Laplacian
import cv2
import numpy as np
import matplotlib.pyplot as plt
# persiapan
image = cv2.imread('penguins.jpg', cv2.IMREAD_COLOR)
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(image_gray,(3,3),0)
plt.subplot(221).set_title("Citra")
plt.axis("off")
plt.imshow(image)
plt.subplot(222).set_title('Laplacian')
plt.axis("off")
plt.imshow(filtered_image)
plt.tight_layout()
plt.show()