Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from matplotlib import pyplot as plt
- def blur_image(image, amount=3):
- '''Blurs the image
- Does not affect the original image'''
- kernel = np.ones((amount, amount), np.float32) / (amount**2)
- return cv2.filter2D(image, -1, kernel)
- def get_contours():
- def progowanie(x):
- print("Progowanie: " + str(x))
- def rozmycie(x):
- print("rozmycie: " + str(x))
- img = cv2.imread('pies.jpg', 0)
- img_c = cv2.imread('pies.jpg')
- cv2.namedWindow('image')
- cv2.createTrackbar('progowanie1', 'image', 39, 255, progowanie)
- cv2.createTrackbar('progowanie2', 'image', 38, 255, progowanie)
- cv2.createTrackbar('rozmycie', 'image', 1, 7, rozmycie)
- imgshow = img
- while (1):
- cv2.imshow('image', imgshow)
- k = cv2.waitKey(1) & 0xFF
- # escape to end program
- if k == 27:
- break
- # get current positions of four trackbars
- progowanie1 = cv2.getTrackbarPos('progowanie1', 'image')
- progowanie2 = cv2.getTrackbarPos('progowanie2', 'image')
- blur = cv2.getTrackbarPos('rozmycie', 'image')
- if blur % 2 == 0:
- blur = blur + 1
- bluredImg = cv2.GaussianBlur(img, (blur, blur), 1.5)
- imgshow = cv2.Canny(bluredImg, progowanie1, progowanie2)
- im2, contours, hierarchy = cv2.findContours(blur_image(imgshow), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- cv2.drawContours(img_c, contours, -1, (0, 255, 0), 3)
- cv2.imshow('dst', img_c)
- if cv2.waitKey(0) & 0xff == 27:
- cv2.destroyAllWindows()
- cv2.destroyAllWindows()
- for con in contours:
- if cv2.contourArea(con)>3000:
- mask_image(con)
- def mask_image(contour):
- image = cv2.imread('pies.jpg', -1)
- mask = np.zeros(image.shape, dtype=np.uint8)
- roi_corners = np.array(contour, dtype=np.int32)
- channel_count = image.shape[2]
- ignore_mask_color = (255,) * channel_count
- cv2.fillConvexPoly(mask, roi_corners, ignore_mask_color)
- masked_image = cv2.bitwise_and(image, mask)
- epsilon = 0.1 * cv2.arcLength(contour, True)
- print(epsilon)
- cv2.imshow('dst', masked_image)
- if cv2.waitKey(0) & 0xff == 27:
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement