Lompat ke konten Lompat ke sidebar Lompat ke footer

Kumpulan Tugas Praktikum Python

-Buat kode program python OpenCV untuk membaca data citra RGB

Kode:

Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

>>> import cv2

>>> image = cv2. imread('D:/Helmy kurniawan.jpg')

>>> print(image)

>>> cv2.imshow('Gambar Helmy RGB', image)

>>> cv2.waitKey(0)

>>> cv2.destroyAllWindows()

Hasilnya:

membaca data citra RGB

-Konversi citra menjadi Grayscale

Kode:

>>> import cv2

>>> image = cv2. imread('D:/Helmy kurniawan.jpg')

>>> gray= cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

>>> print(gray)

>>> cv2.imshow('Konversi GrayScale', gray)

>>> cv2.waitKey(0)

>>> cv2.destroyAllWindows()

Hasilnya:

Konversi citra menjadi Grayscale

-Konversi citra menjadi HSV

Kode:

>>> import cv2

>>> original = cv2. imread('D:/Helmy kurniawan.jpg')

>>> h, w = original.shape[:2]

>>> new_h, new_w= int(h/2), int(w/2)

>>> image= cv2.resize(original,(new_w, new_h))

>>> hsv= cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

>>> print(hsv)

>>> cv2.imshow('Gambar HSV', hsv)

>>> cv2.imshow('H Channel', hsv[: ,: ,0])

>>> cv2.imshow('S Channel', hsv[: ,: ,1])

>>> cv2.imshow('V Channel', hsv[: ,: ,2])

>>> cv2.waitKey(0)

>>> cv2.destroyAllWindows()

Hasilnya;

Konversi citra menjadi HSV

-Buat kode program python untuk melakukan low pass filter pada gambar

Kode:

import numpy as np

import cv2

from matplotlib import pyplot as plt

from PIL import Image, ImageFilter

image = cv2.imread('D:/FT 3x4.jpg')

image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

figure_size=3

new_image= cv2.blur(image,(figure_size, figure_size))

plt.figure(figsize=(11,6))

plt.subplot(121),plt.imshow(cv2.cvtColor(image, cv2.COLOR_HSV2RGB)),plt.title('Original')

plt.xticks([]),plt.yticks([])

plt.subplot(122),plt.imshow(cv2.cvtColor(new_image, cv2.COLOR_HSV2RGB)), plt.title('Mean Filter')

plt.xticks([]),plt.yticks([])

plt.show()

Kode 2:

import cv2

import numpy as np

from matplotlib import pyplot as plt

img = cv2.imread('D:/FT 3x4.jpg', 0)

height, width = img.shape

plt.figure("Input")

plt.subplot(221),plt.imshow(img, cmap = 'gray')

plt.title('Image'), plt.xticks([]), plt.yticks([])

fft = np.log(np.abs(np.fft.fftshift(np.fft.fft2(img))))

plt.subplot(222),plt.imshow(fft, cmap = 'gray')

plt.title('Fourier Transform'), plt.xticks([]), plt.yticks([])

r = 20

h_Filter_Low_Pass = np.zeros(img.size, img.dtype).reshape(img.shape)

for icounter in range(1, height):

    for jcounter in range(1, width):

        if ((icounter - height/2)**2 + (jcounter - width/2)**2) < r**2:

            h_Filter_Low_Pass[icounter, jcounter] = 1

plt.subplot(223),plt.imshow(h_Filter_Low_Pass, cmap = 'gray')

plt.title('Filter'), plt.xticks([]), plt.yticks([])

h_fft = np.log(np.abs(np.fft.fftshift(np.fft.fft2(h_Filter_Low_Pass))))

plt.subplot(224),plt.imshow(h_fft, cmap = 'gray')

plt.title('Fourier Transform (Filter)'), plt.xticks([]), plt.yticks([])

plt.figure("Output")

g = fft * h_fft

g_ifft = np.abs(np.fft.ifftshift(np.fft.ifft2(g)).real)

plt.subplot(),plt.imshow(g_ifft, cmap = 'gray')

plt.title(''), plt.xticks([]), plt.yticks([])

plt.show()

Hasilnya;

low pass filter

-Buat kode program python untuk melakukan high pass filter pada gambar

Kode:

import numpy as np

import cv2

from matplotlib import pyplot as plt

img = cv2.imread('D:/FT 3x4.jpg')

kernel= np.ones((5,5), np.float32)/25

dst= cv2.filter2D(img, -1, kernel)

plt.subplot(121), plt.imshow(img), plt.title('Original')

plt.xticks([]), plt.yticks([])

plt.subplot(122), plt.imshow(dst),plt.title('Averaging')

plt.xticks([]), plt.yticks([])                                           

plt.show()

Kode 2 :

import cv2

import numpy as np

from scipy import ndimage

kernel_3x3 = np.array([[-1, -1, -1],

                       [-1, 8, -1],

                       [-1, -1, -1]])

kernel_5x5 = np.array([[-1, -1, -1, -1, -1],

                       [-1, 1, 2, 2, -1],

                       [-1, 2, 4, 2, -1],

                       [-1, 1, 2, 1, -1],

                       [-1, -1, -1, -1, -1]])

img = cv2.imread("D:/FT 3x4.jpg", cv2.IMREAD_GRAYSCALE)

k3 = ndimage.convolve(img, kernel_3x3)

k5 = ndimage.convolve(img, kernel_5x5)

blurred = cv2.GaussianBlur(img, (11, 11), 0)

g_hpf = img - blurred

cv2.imshow("3x3", k3)

cv2.imshow("5x5", k5)

cv2.imshow("g_hpf", g_hpf)

cv2.waitKey()

cv2.destroyAllWindows()

Kode 3:

import numpy as np

import cv2

from scipy import ndimage

class HPF(object):

    def __init__(self, kernel, image):

        self.kernel = np.array(kernel)

        self.image = image


    def process(self):

        return ndimage.convolve(self.image, self.kernel)

if __name__ == "__main__":

    #enter ur image location

    image = cv2.imread("D:/FT 3x4.jpg", 0)

    kernel3x3 = [[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]

    kernel5x5 = [[-1, -1, -1, -1, -1],

    [-1, 1, 2, 1, -1],

    [-1, 2, 4, 2, -1],

    [-1, 1, 2, 1, -1],

    [-1, -1, -1, -1, -1]]

    hpf1 = HPF(kernel3x3, image)

    hpfimage1 = hpf1.process()

    hpf2 = HPF(kernel5x5, image)

    hpfimage2 = hpf2.process()

    cv2.imshow("3x3",hpfimage1)

    cv2.imshow("5x5",hpfimage2)

    cv2.waitKey()

    cv2.destroyAllWindows()

Hasilnya;

high pass filter

 -Buat kode program python OpenCV untuk menampilkan Histogram Citra

Kode:

>>> import cv2

>>> import numpy as np

>>> from matplotlib import pyplot as plt

>>> img = cv2.imread('D:/FT 3x4.jpg',0)

>>> plt.hist(img.ravel(),256,[0,256]);

>>> plt.show()

Hasilnya;

menampilkan Histogram Citra

-Buat kode program python untuk melakukan perbaikan citra dengan metode Intensity Adjusment

Kode:

from skimage.filters import threshold_yen

from skimage.exposure import rescale_intensity

from skimage.io import imread, imsave

img = imread('D:/FT 3x4.jpg')

img_transf = cv2.cvtColor(img3, cv2.COLOR_BGR2YUV)

img_transf[:,:,0] = cv2.equalizeHist(img_transf[:,:,0])

img4 = cv2.cvtColor(img_transf, cv2.COLOR_YUV2BGR)

cv2.imwrite('test.jpg', img4)

Kode 2:

from skimage.filters import threshold_yen
from skimage.exposure import rescale_intensity
from skimage.io import imread, imsave
import cv2

img4 = imread('D:/FT 3x4.jpg')
img_transf = cv2.cvtColor(img4, cv2.COLOR_BGR2YUV)
img_transf[:,:,0] = cv2.equalizeHist(img_transf[:,:,0])
img4 = cv2.cvtColor(img_transf, cv2.COLOR_YUV2BGR)
cv2.imshow('test ', img4)

Hasilnya; 

metode Intensity Adjusment

-Buat kode program python untuk melakukan perbaikan citra dengan metode Histogram Equalization

Kode:

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

def perataanHistogram(A):    
    nilai_bin = 255 
    A = A+1  
    [frekuensi,value] = np.histogram(A,bins=nilai_bin)  
    cumulatif_histogram = frekuensi.cumsum() 
    [baris,kolom] = A.shape
    probabilty_frekuensi = np.round((cumulatif_histogram/float(A.size))*nilai_bin) #langkah 4 dan 5
    B = np.empty(A.shape)    
    for i in range(0,baris):
        for j in range(0,kolom):
            B[i,j] = probabilty_frekuensi[A[i,j]-1]     return B

I = cv2.imread('D:/FT 3x4.jpg')
gray = cv2.cvtColor(I,cv2.COLOR_BGR2GRAY) 
hasil = perataanHistogram(gray)
plt.figure('Histogram Equalization')
plt.subplot(1,2,1),plt.imshow(gray,cmap='gray'),plt.title('gray')
plt.subplot(1,2,2),plt.imshow(hasil,cmap='gray'),plt.title('perataan histogram')
plt.show()

Hasilnya; 

metode Histogram Equalization

www,helmykediricom

Helmy Kediri
Helmy Kediri Sebaik-baik manusia adalah yang paling bermanfaat bagi orang lain. Ketika seseorang telah meninggal dunia, maka terputuslah amalnya kecuali 3 (perkara) : shadaqah jariyah, ilmu yang bermanfaat, dan anak saleh yang mendokan orang tuanya

Posting Komentar untuk "Kumpulan Tugas Praktikum Python"

Berlangganan via Email