├── README.md ├── images ├── cards │ ├── main_image │ │ └── cards.JPG │ └── sample │ │ ├── king_club.JPG │ │ ├── queen_diamond.JPG │ │ ├── queen_spade.JPG │ │ └── ten_club.JPG ├── girl.jpg └── rectangles.png ├── part_1.py ├── part_2.py ├── part_3.py └── part_4.py /README.md: -------------------------------------------------------------------------------- 1 | # Opencv Tutorial 2 | 3 | I highly recommend to see my articles for better understanding: 4 | 5 | Part_1: https://habr.com/ru/post/519454/ 6 | 7 | Part_2: https://habr.com/ru/post/528144/ 8 | 9 | Part_3: https://habr.com/ru/post/539228/ 10 | 11 | Part_4: https://habr.com/ru/post/547218/ 12 | -------------------------------------------------------------------------------- /images/cards/main_image/cards.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/cards/main_image/cards.JPG -------------------------------------------------------------------------------- /images/cards/sample/king_club.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/cards/sample/king_club.JPG -------------------------------------------------------------------------------- /images/cards/sample/queen_diamond.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/cards/sample/queen_diamond.JPG -------------------------------------------------------------------------------- /images/cards/sample/queen_spade.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/cards/sample/queen_spade.JPG -------------------------------------------------------------------------------- /images/cards/sample/ten_club.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/cards/sample/ten_club.JPG -------------------------------------------------------------------------------- /images/girl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/girl.jpg -------------------------------------------------------------------------------- /images/rectangles.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wb-08/opencv-tutorial/e27b320b3a2f06c36c9a868e1f0de2c70aa4583e/images/rectangles.png -------------------------------------------------------------------------------- /part_1.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | 4 | def loading_displaying_saving(): 5 | img = cv2.imread('images/girl.jpg', cv2.IMREAD_GRAYSCALE) 6 | cv2.imshow('girl', img) 7 | cv2.waitKey(0) 8 | cv2.imwrite('graygirl.jpg', img) 9 | 10 | 11 | 12 | def accessing_and_manipulating(): 13 | img = cv2.imread('images/girl.jpg') 14 | print("Высота:"+str(img.shape[0])) 15 | print("Ширина:" + str(img.shape[1])) 16 | print("Количество каналов:" + str(img.shape[2])) 17 | (b, g, r) = img[0, 0] 18 | print("Красный: {}, Зелёный: {}, Синий: {}".format(r, g, b)) 19 | img[0, 0] = (255, 0, 0) 20 | (b, g, r) = img[0, 0] 21 | print("Красный: {}, Зелёный: {}, Синий: {}".format(r, g, b)) 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /part_2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('images/girl.jpg') 5 | 6 | 7 | def resizing(new_width=None, new_height=None, interp=cv2.INTER_LINEAR): 8 | h, w = img.shape[:2] 9 | 10 | if new_width is None and new_height is None: 11 | return img 12 | 13 | if new_width is None: 14 | ratio = new_height / h 15 | dimension = (int(w * ratio), new_height) 16 | 17 | else: 18 | ratio = new_width / w 19 | dimension = (new_width, int(h * ratio)) 20 | 21 | res_img = cv2.resize(img, dimension, interpolation=interp) 22 | cv2.imwrite('new_girl_resize.jpg', res_img) 23 | 24 | # res_img = cv2.resize(img, (500, 900), cv2.INTER_NEAREST) 25 | # cv2.imwrite('res_girl.jpg', res_img) 26 | res_img_nearest = cv2.resize(img, (int(w / 1.4), int(h / 1.4)), cv2.INTER_NEAREST) 27 | res_img_linear = cv2.resize(img, (int(w / 1.4), int(h / 1.4)), cv2.INTER_LINEAR) 28 | # cv2.imwrite('girl_nearest.jpg', res_img_nearest) 29 | # cv2.imwrite('girl_linear.jpg', res_img_linear) 30 | 31 | 32 | def shifting(): 33 | h, w = img.shape[:2] 34 | translation_matrix = np.float32([[1, 0, 200], [0, 1, 300]]) 35 | dst = cv2.warpAffine(img, translation_matrix, (w, h)) 36 | cv2.imwrite('girl_right_and_down_1.jpg', dst) 37 | 38 | 39 | def cropping(): 40 | crop_img = img[10:450, 300:750] 41 | cv2.imwrite('crop_face.png', crop_img) 42 | 43 | 44 | def rotation(): 45 | (h, w) = img.shape[:2] 46 | center = (int(w / 2), int(h / 2)) 47 | rotation_matrix = cv2.getRotationMatrix2D(center, 45, 0.6) 48 | rotated = cv2.warpAffine(img, rotation_matrix, (w, h)) 49 | cv2.imwrite('rotated_girl.jpg', rotated) 50 | -------------------------------------------------------------------------------- /part_3.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | 4 | 5 | def image_arithmetic(): 6 | print("opencv addition: {}".format(cv2.add(np.uint8([250]), np.uint8([30])))) 7 | print("opencv subtract: {}".format(cv2.subtract(np.uint8([70]), np.uint8([100])))) 8 | print("numpy addition: {}".format(np.uint8([250]) + np.uint8([30]))) 9 | print("numpy subtract: {}".format(np.uint8([70]) - np.uint8([71]))) 10 | 11 | 12 | def splitting_and_merging(): 13 | image = cv2.imread('images/rectangles.png') 14 | b, g, r = cv2.split(image) 15 | cv2.imshow('blue', b) 16 | cv2.imshow('green', g) 17 | cv2.imshow('red', r) 18 | 19 | merge_image = cv2.merge([g,b,r]) 20 | cv2.imshow('merge_image', merge_image) 21 | cv2.imshow('original', image) 22 | cv2.waitKey(0) 23 | 24 | 25 | def averaging_blurring(): 26 | image = cv2.imread('images/girl.jpg') 27 | img_blur_3 = cv2.blur(image, (3, 3)) 28 | img_blur_7 = cv2.blur(image, (7, 7)) 29 | img_blur_11 = cv2.blur(image, (11, 11)) 30 | cv2.imshow('3x3', img_blur_3) 31 | cv2.imshow('7x7', img_blur_7) 32 | cv2.imshow('11x11', img_blur_11) 33 | cv2.waitKey(0) 34 | 35 | 36 | def gaussian_blurring(): 37 | image = cv2.imread('images/girl.jpg') 38 | img_blur_3 = cv2.GaussianBlur(image, (3, 3), 0) 39 | img_blur_7 = cv2.GaussianBlur(image, (7, 7), 0) 40 | img_blur_11 = cv2.GaussianBlur(image, (11, 11), 0) 41 | cv2.imshow('3x3', img_blur_3) 42 | cv2.imshow('7x7', img_blur_7) 43 | cv2.imshow('11x11', img_blur_11) 44 | cv2.waitKey(0) 45 | 46 | 47 | def median_blurring(): 48 | image = cv2.imread('images/girl.jpg') 49 | img_blur_3 = cv2.medianBlur(image, 3) 50 | img_blur_7 = cv2.medianBlur(image, 7) 51 | img_blur_11 = cv2.medianBlur(image, 11) 52 | cv2.imshow('3', img_blur_3) 53 | cv2.imshow('7', img_blur_7) 54 | cv2.imshow('11', img_blur_11) 55 | cv2.waitKey(0) 56 | -------------------------------------------------------------------------------- /part_4.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | 4 | 5 | def find_features(img1): 6 | correct_matches_dct = {} 7 | directory = 'images/cards/sample/' 8 | for image in os.listdir(directory): 9 | img2 = cv2.imread(directory+image, 0) 10 | orb = cv2.ORB_create() 11 | kp1, des1 = orb.detectAndCompute(img1, None) 12 | kp2, des2 = orb.detectAndCompute(img2, None) 13 | bf = cv2.BFMatcher() 14 | matches = bf.knnMatch(des1, des2, k=2) 15 | correct_matches = [] 16 | for m, n in matches: 17 | if m.distance < 0.75*n.distance: 18 | correct_matches.append([m]) 19 | correct_matches_dct[image.split('.')[0]] = len(correct_matches) 20 | correct_matches_dct = dict(sorted(correct_matches_dct.items(), 21 | key=lambda item: item[1], reverse=True)) 22 | return list(correct_matches_dct.keys())[0] 23 | 24 | 25 | def find_contours_of_cards(image): 26 | blurred = cv2.GaussianBlur(image, (3, 3), 0) 27 | T, thresh_img = cv2.threshold(blurred, 215, 255, cv2.THRESH_BINARY) 28 | (_, cnts, _) = cv2.findContours(thresh_img, cv2.RETR_EXTERNAL, 29 | cv2.CHAIN_APPROX_SIMPLE) 30 | return cnts 31 | 32 | 33 | def find_coordinates_of_cards(cnts, image): 34 | cards_coordinates = {} 35 | for i in range(0, len(cnts)): 36 | x, y, w, h = cv2.boundingRect(cnts[i]) 37 | if w > 20 and h > 30: 38 | img_crop = image[y - 15:y + h + 15, x - 15:x + w + 15] 39 | cards_name = find_features(img_crop) 40 | cards_coordinates[cards_name] = (x - 15, y - 15, x + w + 15, y + h + 15) 41 | return cards_coordinates 42 | 43 | 44 | def draw_rectangle_aroud_cards(cards_coordinates, image): 45 | for key, value in cards_coordinates.items(): 46 | rec = cv2.rectangle(image, (value[0], value[1]), (value[2], value[3]), (255, 255, 0), 2) 47 | cv2.putText(rec, key, (value[0], value[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (36, 255, 12), 1) 48 | cv2.imshow('Image', image) 49 | cv2.waitKey(0) 50 | 51 | 52 | if __name__ == '__main__': 53 | main_image = cv2.imread('images/cards/main_image/cards.JPG') 54 | gray_main_image = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY) 55 | contours = find_contours_of_cards(gray_main_image) 56 | cards_location = find_coordinates_of_cards(contours, gray_main_image) 57 | draw_rectangle_aroud_cards(cards_location, main_image) --------------------------------------------------------------------------------