Professional Documents
Culture Documents
By – Gulshan Kumar(22PGMCA36)
(1) A11_hit_or_miss
import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.imshow(b2, cmap='gray')
plt.title('B2')
plt.show()
plt.imshow(tb2, cmap='gray')
plt.title('Tb2')
plt.show()
# Display the hit-or-miss result and the original image with detected lines
plt.imshow(hit_or_miss, cmap='gray')
plt.title('Object to detect')
plt.show()
# Display the original image with horizontal lines of minimum length 6 pixels
tb1 = cv2.erode(t, b1, iterations=1)
plt.imshow(tb1, cmap='gray')
plt.title('Horizontal lines with min. 6 pixels length')
plt.show()
(2) A12_Region_filling
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Image boundary
nb = np.bitwise_and(n, ~cv2.erode(n, sq, iterations=1))
import cv2
import numpy as np
from matplotlib import pyplot as plt
import cv2
import numpy as np
from matplotlib import pyplot as plt
if b is None:
print("Error: Could not load the bacteria image.")
else:
b = cv2.cvtColor(b, cv2.COLOR_BGR2GRAY)
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Generate x values
x = np.tile(np.arange(1, 271), (270, 1))
# Divide the image into four parts and perform adaptive thresholding
num_splits = 4
split_width = c3.shape[1] // num_splits
c4 = np.zeros_like(c3)
for i in range(num_splits):
start_col = i * split_width
end_col = (i + 1) * split_width
c4[:, start_col:end_col] = adaptive_thresholding(c3[:, start_col:end_col])
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Roberts edge
edge_r = cv2.filter2D(ic, -1, np.array([[1, 0], [0, -1]], dtype=float))
plt.imshow(edge_r, cmap='gray')
plt.title('Roberts edge')
plt.show()
# Sobel edge
edge_s = cv2.Sobel(ic, -1, 1, 0, ksize=3)
plt.imshow(edge_s, cmap='gray')
plt.title('Sobel edge')
plt.show()
# Canny edge
edge_c = cv2.Canny(ic, 50, 150)
plt.imshow(edge_c, cmap='gray')
plt.title('Canny edge')
plt.show()
(7) A17_Second_order
import cv2
import numpy as np
from matplotlib import pyplot as plt
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Sobel filter
h = cv2.getDerivKernels(1, 0, 3, normalize=True)
g_x = cv2.filter2D(f, cv2.CV_64F, h[0] @ h[1].T)
g_y = cv2.filter2D(f, cv2.CV_64F, h[1] @ h[0].T)
g = np.sqrt(g_x**2 + g_y**2)
# Watershed segmentation
ret, markers = cv2.threshold(g_uint8, 0, 255, cv2.THRESH_BINARY +
cv2.THRESH_OTSU)
markers = cv2.connectedComponents(markers)[1]
import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.show()
(10) A20_Templete
import cv2
import numpy as np
from matplotlib import pyplot as plt
plt.imshow(a, cmap='gray')
plt.title('ROI (a)')
plt.show()
# Perform cross-correlation
C = cv2.filter2D(bw.astype(float), -1, rot90(a, 2).astype(float),
borderType=cv2.BORDER_CONSTANT)
# Thresholding
thresh = 60
binary_result = (C > thresh).astype(np.uint8)