Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2 as cv
- import numpy as np
- from matplotlib import pyplot as plt
- def take_max(A):
- A = [X]
- max = A[0]
- for i in range (len(A)):
- if A[i]>max:
- max = A[i]
- return max
- img = cv.imread('image.png')
- imgray = np.average(img,axis=2)
- ret,thresh = cv.threshold(imgray,100,255,cv.THRESH_BINARY_INV)
- #thresh.reshape((imgray.shape[0],imgray.shape[1],1))*np.array([1.,1.,1.]))
- thresh = thresh.astype(np.uint8)
- im2,contours, hierarchy = cv.findContours(thresh,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)
- cv.drawContours(img,contours,-1,(0,255,0),3)
- cv.fillPoly(img, contours, color=(0,0,0))
- X = []
- Y = []
- W = []
- H = []
- for c in contours:
- x,y,w,h = cv.boundingRect(c)
- cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
- X.append(x)
- Y.append(y)
- W.append(w)
- Y.append(h)
- x_r = take_max(X)
- y_r = take_max(Y)
- w_r =take_max(W)
- h_r = take_max(H)
- max_picture = img[y_r:y_r+h, x_r:x_r+h]
- print("-------------------------------------------------------------------------------------------------------------------------------------------------")
- plt.imshow(max_picture)
- #print(w*h)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement