2022-10-12 16:43:28 -07:00
|
|
|
# MONOCHROME EDGE DETECTION
|
|
|
|
|
|
|
|
import cv2 as cv
|
|
|
|
import numpy as np
|
2022-11-13 17:57:41 -08:00
|
|
|
from skimage import measure, morphology, color, segmentation
|
|
|
|
import matplotlib.pyplot as plt
|
2022-10-12 16:43:28 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
file = 'streaktest2.png'
|
|
|
|
img = cv.imread(file)
|
2022-10-27 10:31:42 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
# blurred = cv.GaussianBlur(img, (8, 8), 0)
|
2022-10-27 10:31:42 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
retval, thresh_gray = cv.threshold(img, 120, 255, cv.THRESH_BINARY)
|
2022-10-12 16:43:28 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
kernel = np.ones((7, 7), np.uint8)
|
|
|
|
image = cv.morphologyEx(thresh_gray, cv.MORPH_CLOSE, kernel, iterations=1)
|
|
|
|
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
retval, gray = cv.threshold(gray, 0, 255, cv.THRESH_BINARY)
|
|
|
|
|
|
|
|
gray = cv.copyMakeBorder(
|
|
|
|
gray,
|
|
|
|
20,
|
|
|
|
20,
|
|
|
|
20,
|
|
|
|
20,
|
|
|
|
cv.BORDER_CONSTANT,
|
|
|
|
value=0
|
|
|
|
)
|
|
|
|
|
|
|
|
contours = measure.find_contours(array=gray, level=100)
|
|
|
|
|
|
|
|
fig, ax = plt.subplots()
|
|
|
|
ax.imshow(gray, cmap=plt.cm.gray, alpha=1)
|
2022-10-12 16:43:28 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
def calculate_area(countour):
|
|
|
|
c = np.expand_dims(countour.astype(np.float32), 1)
|
|
|
|
c = cv.UMat(c)
|
|
|
|
|
|
|
|
return cv.contourArea(c)
|
2022-10-12 16:43:28 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
for contour in contours:
|
|
|
|
area = calculate_area(contour)
|
|
|
|
|
|
|
|
if calculate_area(contour) > 250:
|
|
|
|
ax.plot(contour[:, 1], contour[:, 0], linewidth=0.5, color='orangered')
|
2022-10-12 16:43:28 -07:00
|
|
|
|
2022-11-13 17:57:41 -08:00
|
|
|
ax.axis('image')
|
|
|
|
ax.set_xticks([])
|
|
|
|
ax.set_yticks([])
|
|
|
|
plt.savefig("edge_detection_figure.png", dpi=500)
|