Практична робота з «Комп’ютерний зір»
Автор: Alisa2001____ • Декабрь 15, 2022 • Практическая работа • 1,452 Слов (6 Страниц) • 173 Просмотры
МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ ХАРКІВСЬКИЙ
ХАРКІВСЬКИЙ НАЦІОНАЛЬНИЙ УНІВЕРСИТЕТ РАДІОЕЛЕКТРОНІКИ
Кафедра ІУС
Практична робота №1
з дисципліни «Комп’ютерний зір»
Виконала:
Студентка групи ІУСТ-зм-22-1
Зобова А.М.
Перевірив:
Викладач: В.П. Машталір
Харків, 2022
Код:
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_ubyte, data
from skimage.draw import line, circle_perimeter
from skimage.color import rgb2gray
from skimage.feature import canny
from skimage.transform import hough_line, hough_line_peaks, hough_circle, hough_circle_peaks, \
probabilistic_hough_line
from skimage.util import random_noise
class ImageTransform:
def __init__(self, image=None, param_1: float = 1, param_2: float = 1, param_3: float = 1, image_cols=6):
self.image = self.create_image(image)
self.canny_image = self.to_canny(param_1, param_2, param_3)
self.noise_image = self.add_noise(self.image)
self.noise_image_2d = self.add_noise(rgb2gray(self.image))
self.canny_after_noise = self.to_canny(param_1, param_2, param_3, self.noise_image)
fig, axes = plt.subplots(1, image_cols, figsize=(22, 10))
self.fig = fig
self.ax = axes.ravel()
@staticmethod
def create_image(image):
pass
def to_canny(self, param_1: float, param_2: float, param_3: float, img=None):
if img is None:
img = self.image
if len(img.shape) >= 3:
if img.shape[2] == 3:
img = rgb2gray(img)
return canny(img, param_1, param_2, param_3)
def highlight(self, image=None):
pass
def add_noise(self, image):
if image is None:
image = self.image
return random_noise(image)
def compare(self):
pass
class ImageLinesTransform(ImageTransform):
def __init__(self, image=None, param_1: float = 1, param_2: float = 1, param_3: float = 1, image_cols=8):
super().__init__(image, param_1, param_2, param_3, image_cols)
def create_image(self, image):
if image is None:
image = np.zeros((200, 200, 3))
idx = np.arange(25, 175)
image[idx, idx] = 255
image[line(45, 25, 25, 175)] = 255
image[line(25, 135, 175, 155)] = 255
else:
image = plt.imread(image)
return image
def highlight(self, image=None):
if image is None:
image = rgb2gray(self.image)
nums = (2, 3)
params = (2, 0.55, 0.8)
else:
nums = (6, 7)
params = (1, 1, 1)
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 360, endpoint=False)
hspace, theta, dist = hough_line(self.to_canny(*params, image), theta=tested_angles)
img = np.zeros(self.image.shape, dtype=np.uint8)
self.ax[nums[0]].imshow(img, cmap=plt.cm.gray)
self.ax[nums[0]].set_ylim((img.shape[0], 0))
self.ax[nums[0]].set_axis_off()
self.ax[nums[0]].set_title('Detected lines')
for _, angle, dist in zip(*hough_line_peaks(hspace, theta, dist)):
(x0, y0) = dist * np.array([np.cos(angle), np.sin(angle)])
self.ax[nums[0]].axline((x0, y0), slope=np.tan(angle + np.pi / 2))
lines = probabilistic_hough_line(self.to_canny(1, 1, 1, image), threshold=10, line_length=5,
line_gap=3)
self.ax[nums[1]].imshow(self.to_canny(1, 1, 1, image) * 0)
for line in lines:
p0, p1 = line
self.ax[nums[1]].plot((p0[0], p1[0]), (p0[1], p1[1]))
self.ax[nums[1]].set_xlim((0, image.shape[1]))
self.ax[nums[1]].set_ylim((image.shape[0], 0))
self.ax[nums[1]].set_title('Probabilistic Hough')
for a in self.ax:
a.set_axis_off()
def compare(self):
self.ax[0].imshow(self.image / 255)
self.ax[0].set_title("Original")
self.ax[1].imshow(self.canny_image, cmap=plt.cm.gray)
self.ax[1].set_title("Canny")
self.highlight()
self.ax[4].imshow(self.noise_image, cmap=plt.cm.gray)
self.ax[4].set_title("With noise")
self.ax[5].imshow(self.canny_after_noise, cmap=plt.cm.gray)
self.ax[5].set_title("Canny with noise")
self.highlight(self.noise_image_2d)
self.fig.tight_layout()
plt.show()
class ImageCircleTransform(ImageTransform):
def __init__(self, image=None, param_1: float = 1, param_2: float = 1,
param_3: float = 1, image_cols=6, circles_num=1):
super().__init__(image, param_1, param_2, param_3, image_cols)
self.circles_num = circles_num
def create_image(self, image):
if image is None:
image = np.zeros((250, 250, 3), dtype=np.uint8)
image[circle_perimeter(60, 60, 50)] = 255
image[circle_perimeter(150, 150, 60)] = 255
else:
image = plt.imread(image)
return image
def highlight(self, image=None):
if image is None:
image = rgb2gray(self.image)
nums = (2,)
else:
nums = (5,)
# Detect two radii
hough_radii = np.arange(50, 80, 5)
image = img_as_ubyte(image)
edges = canny(image, sigma=2.0, low_threshold=0.55, high_threshold=0.8)
hough_res = hough_circle(edges, hough_radii)
# Select the most prominent circles
accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii, total_num_peaks=self.circles_num,
min_xdistance=50, min_ydistance=50)
image = np.zeros((self.image.shape[0], self.image.shape[1], 3), dtype=np.uint8)
# Draw them
for center_y, center_x, radius in zip(cy, cx, radii):
circy, circx = circle_perimeter(center_y, center_x, radius)
image[circy, circx] = (220, 20, 20)
self.ax[nums[0]].imshow(image, cmap=plt.cm.gray)
def compare(self):
self.ax[0].imshow(self.image / 255)
self.ax[0].set_title("Original")
self.ax[1].imshow(self.canny_image, cmap=plt.cm.gray)
self.ax[1].set_title("Canny")
self.highlight()
self.ax[3].imshow(self.noise_image, cmap=plt.cm.gray)
self.ax[3].set_title("With noise")
self.ax[4].imshow(self.canny_after_noise, cmap=plt.cm.gray)
self.ax[4].set_title("Canny with noise")
self.highlight(self.noise_image_2d)
self.fig.tight_layout()
plt.show()
line_image = ImageLinesTransform(image_cols=8)
line_image.compare()
circle_image = ImageCircleTransform(circles_num=2)
circle_image.compare()
col_image = ImageLinesTransform(image='img.jpg')
col_image.compare()
col_image2 = ImageCircleTransform(image='boll.jpg', circles_num=4)
col_image2.compare()
...