Initial commit
This commit is contained in:
162
lab2_1/lab2_1.py
Normal file
162
lab2_1/lab2_1.py
Normal file
@@ -0,0 +1,162 @@
|
||||
import os
|
||||
import sys
|
||||
import cv2
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
|
||||
def menu():
|
||||
print("Выберите пункт меню:\n1.Linear filter\n2.Blur\n3.Median blur\n4.GaussianBlur\n5.Erode")
|
||||
print("6.Dilate\n7.MORPH_OPERATIONS\n8.Sobel\n9.Laplacian\n10.Canny\n11.CalcHist\n12.EqualizeHist")
|
||||
print("13.Save\n14.Add Figure\n15.Del Figure\n16.Exit")
|
||||
|
||||
|
||||
def main():
|
||||
image1 = cv2.imread("D:\\MACH\\LAB_2\\image0.jpg")
|
||||
image2 = image1.copy()
|
||||
im = image1.copy()
|
||||
while True:
|
||||
menu()
|
||||
try:
|
||||
s = int(input())
|
||||
if s == 1:
|
||||
image2 = image1.copy()
|
||||
kernel = np.array([[-0.1, 0.2, -0.1], [0.2, 3.0, 0.2], [-0.1, 0.2, -0.1]])
|
||||
image2 = cv2.filter2D(image2, -1, kernel)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 2:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.blur(image2, (5, 5))
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 3:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.medianBlur(image2, 5)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 4:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.GaussianBlur(image2, (9, 9), cv2.BORDER_DEFAULT)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 5:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.erode(image2, np.ones((11, 11)))
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 6:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.dilate(image2, np.ones((11, 11)))
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 7:
|
||||
image2 = image1.copy()
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
cv2.imshow("Image_load", image1)
|
||||
kernel = np.ones((6, 6), np.uint8)
|
||||
image3 = cv2.morphologyEx(image3, cv2.MORPH_OPEN, kernel, iterations=1)
|
||||
cv2.imshow("MORPH_OPEN", image3)
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
image3 = cv2.morphologyEx(image3, cv2.MORPH_CLOSE, kernel, iterations=1)
|
||||
cv2.imshow("MORPH_CLOSE", image3)
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
image3 = cv2.morphologyEx(image3, cv2.MORPH_GRADIENT, kernel, iterations=1)
|
||||
cv2.imshow("MORPH_GRADIENT", image3)
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
image3 = cv2.morphologyEx(image3, cv2.MORPH_TOPHAT, kernel, iterations=1)
|
||||
cv2.imshow("MORPH_TOPHAT", image3)
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
image3 = cv2.morphologyEx(image3, cv2.MORPH_BLACKHAT, kernel, iterations=1)
|
||||
cv2.imshow("MORPH_BLACKHAT", image3)
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
cv2.waitKey(0)
|
||||
if s == 8:
|
||||
image2 = image1.copy()
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
||||
image3 = cv2.GaussianBlur(image3, (3, 3), 0)
|
||||
im3 = cv2.Sobel(image3, cv2.CV_64F, 1, 0, ksize=5)
|
||||
im4 = cv2.Sobel(image3, cv2.CV_64F, 0, 1, ksize=5)
|
||||
im5 = cv2.Sobel(image3, cv2.CV_8UC1, 0, 1, ksize=5)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image_X", im3) # x
|
||||
cv2.imshow("Result_Image_Y", im4) # y
|
||||
cv2.imshow("Result_Gradient", im5) # gradient
|
||||
cv2.waitKey(0)
|
||||
if s == 9:
|
||||
image2 = image1.copy()
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_RGB2GRAY)
|
||||
image3 = cv2.GaussianBlur(image3, (3, 3), 0)
|
||||
im3 = cv2.Laplacian(image3, cv2.CV_64F)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", im3) # x
|
||||
cv2.waitKey(0)
|
||||
if s == 10:
|
||||
image2 = image1.copy()
|
||||
image3 = cv2.cvtColor(image2, cv2.COLOR_RGB2GRAY)
|
||||
image3 = cv2.blur(image2, (5, 5))
|
||||
image3 = cv2.Canny(image3, 100, 100)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image3) # x
|
||||
cv2.waitKey(0)
|
||||
if s == 11:
|
||||
image2 = image1.copy()
|
||||
color = ('b', 'g', 'r')
|
||||
cv2.imshow("Image_load", image1)
|
||||
for i, col in enumerate(color):
|
||||
histr = cv2.calcHist([image2], [i], None, [256], [0, 256])
|
||||
plt.plot(histr, color=col)
|
||||
plt.xlim([0, 256])
|
||||
plt.show()
|
||||
plt.close()
|
||||
cv2.waitKey(0)
|
||||
if s == 12:
|
||||
image2 = image1.copy()
|
||||
image2 = cv2.cvtColor(image2, cv2.COLOR_RGB2GRAY)
|
||||
image2 = cv2.equalizeHist(image2)
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 13:
|
||||
cv2.imshow("Image_load", image1)
|
||||
cv2.imshow("Result_Image", image2)
|
||||
cv2.imwrite("D:\\im1.jpg", image1)
|
||||
cv2.imwrite("D:\\im2.jpg", image2)
|
||||
cv2.waitKey(0)
|
||||
if s == 14:
|
||||
image2 = image1.copy()
|
||||
cv2.imshow("Image_load", image1)
|
||||
image2 = cv2.line(image2, (15, 15), (270, 270), (76, 187, 23), 10) # линия
|
||||
image2 = cv2.rectangle(image2, (20, 20), (100, 100), (0, 0, 255), 10) # нарисуем четырехугольник
|
||||
image2 = cv2.circle(image2, (100, 100), 40, (0, 255, 0), 10) # нарисуем круг
|
||||
cv2.imshow("Result_Image", image2)
|
||||
image1 = image2
|
||||
cv2.waitKey(0)
|
||||
if s == 15:
|
||||
cv2.imwrite("1.jpg", im)
|
||||
cv2.imwrite("2.jpg", image1)
|
||||
img = cv2.imread("1.jpg")
|
||||
mask = cv2.imread("2.jpg", 0)
|
||||
res = cv2.bitwise_and(img, img, mask=mask)
|
||||
image2 = res.copy()
|
||||
image1 = res.copy()
|
||||
cv2.imshow("Result_Image", res)
|
||||
|
||||
cv2.waitKey(0)
|
||||
os.remove("1.jpg")
|
||||
os.remove("2.jpg")
|
||||
if s == 16:
|
||||
cv2.destroyAllWindows()
|
||||
sys.exit(0)
|
||||
except ValueError:
|
||||
print("Неверный пункт меню!!!! Выберите другое!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user