You are on page 1of 10

LAPORAN FINAL PROJECT

Image Interpretation and Processing

Disusun Oleh :

Agusti Rahmat Triandana

NIM. 180411100081

PROGRAM STUDI TEKNIK INFORMATIKA

FAKULTAS TEKNIK

UNIVERSITAS TRUNOJOYO MADURA

TAHUN 2021
1. Negative Images

Program :
#read image
img_src = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png
')
cv2_imshow(img)

# Histogram plotting of the image


color = ('b', 'g', 'r')

for i, col in enumerate(color):

histr = cv2.calcHist([img],
[i], None,
[256],
[0, 256])

plt.plot(histr, color = col)

# Limit X - axis to 256


plt.xlim([0, 256])

plt.show()

# get height and width of the image


height, width, _ = img.shape

for i in range(0, height - 1):


for j in range(0, width - 1):

# Get the pixel value


pixel = img[i, j]

# Negate each channel by


# subtracting it from 255

# 1st index contains red pixel


pixel[0] = 255 - pixel[0]

# 2nd index contains green pixel


pixel[1] = 255 - pixel[1]

# 3rd index contains blue pixel


pixel[2] = 255 - pixel[2]

# Store new values in the pixel


img[i, j] = pixel

# Display the negative transformed image

cv2_imshow(img)

# Histogram plotting of the


# negative transformed image
color = ('b', 'g', 'r')
for i, col in enumerate(color):

histr = cv2.calcHist([img],
[i], None,
[256],
[0, 256])

plt.plot(histr, color = col)


plt.xlim([0, 256])

plt.show()

2. Log Transformation

Program :

#read image
image = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png')
cv2_imshow(image)
# Apply log transformation method
c = 255 / np.log(1 + np.max(image))
log_image = c * (np.log(image + 1))

# Specify the data type so that


# float value will be converted to int
log_image = np.array(log_image, dtype = np.uint8)
#cv2_imshow(image)
cv2_imshow(log_image)

3. Histogram Equalization

Program :

#read image
img_src = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png
')
cv2_imshow(img)
hist,bins = np.histogram(img.flatten(),256,[0,256])
cdf = hist.cumsum()
cdf_normalized = cdf * float(hist.max()) / cdf.max()
plt.plot(cdf_normalized, color = 'b')
plt.hist(img.flatten(),256,[0,256], color = 'r')
plt.xlim([0,256])
plt.legend(('cdf','histogram'), loc = 'upper left')
plt.show()
4. Lowpass filtering

Program :

#read image
img_src = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png
')
cv2_imshow(img)

#prepare the 5x5 shaped filter


kernel = 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]])
kernel = kernel/sum(kernel)

#filter the source image


img_rst = cv2.filter2D(img_src,-1,kernel)
cv2_imshow(img_rst)

5. Highpass filtering / Edge Detection

Program :

#read image
img = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png')
cv2_imshow(img)

# Convert to graycsale
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Blur the image for better edge detection
img_blur = cv2.GaussianBlur(img_gray, (3,3), 0)
cv2_imshow(img_blur)
# Sobel Edge Detection
sobelx = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=0, ksize
=5) # Sobel Edge Detection on the X axis
sobely = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=0, dy=1, ksize
=5) # Sobel Edge Detection on the Y axis
sobelxy = cv2.Sobel(src=img_blur, ddepth=cv2.CV_64F, dx=1, dy=1, ksiz
e=5) # Combined X and Y Sobel Edge Detection
cv2_imshow(sobelx)
cv2_imshow(sobely)
cv2_imshow(sobelxy)
# Canny Edge Detection
edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # C
anny Edge Detection
# Display Canny Edge Detection Image
cv2_imshow(edges)
6. Thressholding

Program :

#read image
img = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png', c
v2.IMREAD_GRAYSCALE)
cv2_imshow(img)

# applying different thresholding


# techniques on the input image
# all pixels value above 120 will
# be set to 255
ret, thresh1 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img, 120, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img, 120, 255, cv2.THRESH_TOZERO_INV)

cv2_imshow(thresh1)
cv2_imshow(thresh2)
cv2_imshow(thresh3)
cv2_imshow(thresh4)
cv2_imshow(thresh5)
7. Boundary Detection – Morphological Image

Program :

#read image
img = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png', c
v2.IMREAD_GRAYSCALE)
cv2_imshow(img)

# Morphological Edge Detection


# External Boundary Extraction
structure_element2 = np.ones((5,5),np.uint8)
dilate = cv2.dilate(img,structure_element2,iterations = 1)
ebe = np.subtract(dilate, img)
cv2_imshow(ebe)

# Internal Boundary Extraction


erode = cv2.erode(img,structure_element2,iterations = 1)
ibe = np.subtract(img,erode)
cv2_imshow(ibe)

# Morphological Gradient
erode = cv2.erode(img,structure_element2,iterations = 1)
dilate = cv2.dilate(img,structure_element2,iterations = 1)
mg = np.subtract(dilate,erode)
cv2_imshow(mg)
8. Connected Component Labelling

Program :

#read image
img = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png', 0
)
cv2_imshow(img)

_, mask = cv2.threshold(img, 220, 255, cv2.THRESH_BINARY_INV)

kernal = np.ones((5,5), np.uint8)

dilation = cv2.dilate(mask, kernal, iterations=2)


erosion = cv2.erode(mask, kernal, iterations=1)
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernal)
closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernal)
mg = cv2.morphologyEx(mask, cv2.MORPH_GRADIENT, kernal)
th = cv2.morphologyEx(mask, cv2.MORPH_TOPHAT, kernal)

titles = ['image', 'mask', 'dilation', 'erosion', 'opening', 'closing


', 'mg', 'th']
images = [img, mask, dilation, erosion, opening, closing, mg, th]

for i in range(8):
plt.subplot(2, 4, i+1), plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])

plt.show()

9. Image Skeletonization
Program :

#read image
img = cv2.imread('/content/drives/MyDrive/Final Project/lenna.png', 0
)
cv2_imshow(img)

# Threshold the image


ret,img = cv2.threshold(img, 127, 255, 0)

# Step 1: Create an empty skeleton


size = np.size(img)
skel = np.zeros(img.shape, np.uint8)

# Get a Cross Shaped Kernel


element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3,3))
#Step 2: Open the image
open = cv2.morphologyEx(img, cv2.MORPH_OPEN, element)
#Step 3: Substract open from the original image
temp = cv2.subtract(img, open)
#Step 4: Erode the original image and refine the skeleton
eroded = cv2.erode(img, element)
skel = cv2.bitwise_or(skel,temp)
img = eroded.copy()

cv2_imshow(skel)

You might also like