Загрузка данных


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

img = cv2.imread('borsok.jpg')

if img is None:
    print("Файл не найден")
    exit()

print(f"Размер: {img.shape}")
print(f"Тип: {img.dtype}")
print(f"Пиксели: {img.size}")
print(f"Min: {img.min()}, Max: {img.max()}")

resized = cv2.resize(img, (200, 200))
h, w = img.shape[:2]
cropped = img[50:min(250, h), 100:min(300, w)]
flipped = cv2.flip(img, 1)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([5, 20, 100])
upper = np.array([25, 200, 255])
mask = cv2.inRange(hsv, lower, upper)

kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

if contours:
    biggest = max(contours, key=cv2.contourArea)
    area = cv2.contourArea(biggest)
    M = cv2.moments(biggest)
    cx = int(M['m10'] / M['m00'])
    cy = int(M['m01'] / M['m00'])
    print(f"Площадь: {area}")
    print(f"Центр: ({cx}, {cy})")
    result = img.copy()
    cv2.drawContours(result, [biggest], -1, (0, 255, 0), 2)
    cv2.circle(result, (cx, cy), 5, (0, 0, 255), -1)
else:
    result = img
    print("Объект не найден")

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = gray / 255.0

gauss = np.random.normal(0, 0.05, gray.shape)
img_gauss = gray + gauss
img_gauss = np.clip(img_gauss, 0, 1)

sp = np.random.random(gray.shape)
img_sp = gray.copy()
img_sp[sp < 0.025] = 0
img_sp[sp > 0.975] = 1

gauss_uint8 = (img_gauss * 255).astype(np.uint8)
sp_uint8 = (img_sp * 255).astype(np.uint8)

gauss_filtered = cv2.GaussianBlur(gauss_uint8, (5, 5), 0)
median_filtered = cv2.medianBlur(sp_uint8, 5)

plt.figure(figsize=(18, 10))

plt.subplot(3, 4, 1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('1. Оригинал'), plt.axis('off')

plt.subplot(3, 4, 2)
plt.imshow(cv2.cvtColor(resized, cv2.COLOR_BGR2RGB))
plt.title('2. Resize'), plt.axis('off')

plt.subplot(3, 4, 3)
plt.imshow(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB))
plt.title('3. Crop'), plt.axis('off')

plt.subplot(3, 4, 4)
plt.imshow(cv2.cvtColor(flipped, cv2.COLOR_BGR2RGB))
plt.title('4. Flip'), plt.axis('off')

plt.subplot(3, 4, 5)
plt.imshow(hsv[:, :, 0], cmap='gray')
plt.title('5. Hue'), plt.axis('off')

plt.subplot(3, 4, 6)
plt.imshow(mask, cmap='gray')
plt.title('6. Маска'), plt.axis('off')

plt.subplot(3, 4, 7)
plt.imshow(cv2.cvtColor(result, cv2.COLOR_BGR2RGB))
plt.title('7. Контур + Центр'), plt.axis('off')

plt.subplot(3, 4, 8)
plt.imshow(img_gauss, cmap='gray')
plt.title('8. Гауссовский шум'), plt.axis('off')

plt.subplot(3, 4, 9)
plt.imshow(img_sp, cmap='gray')
plt.title('9. Соль и перец'), plt.axis('off')

plt.subplot(3, 4, 10)
plt.imshow(gauss_filtered, cmap='gray')
plt.title('10. Гаусс фильтр'), plt.axis('off')

plt.subplot(3, 4, 11)
plt.imshow(median_filtered, cmap='gray')
plt.title('11. Медианный фильтр'), plt.axis('off')

plt.tight_layout()
plt.show()