Advertisement
Guest User

Untitled

a guest
May 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. import cv2 as cv
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5. def take_max(A):
  6.     A = [X]
  7.     max = A[0]
  8.     for i in range (len(A)):
  9.         if A[i]>max:
  10.             max = A[i]
  11.     return max
  12.  
  13. img = cv.imread('image.png')
  14.  
  15. imgray = np.average(img,axis=2)
  16.  
  17. ret,thresh = cv.threshold(imgray,100,255,cv.THRESH_BINARY_INV)
  18.  
  19. #thresh.reshape((imgray.shape[0],imgray.shape[1],1))*np.array([1.,1.,1.]))
  20.  
  21.  
  22. thresh = thresh.astype(np.uint8)
  23.  
  24.  
  25. im2,contours, hierarchy = cv.findContours(thresh,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)
  26. cv.drawContours(img,contours,-1,(0,255,0),3)
  27. cv.fillPoly(img, contours, color=(0,0,0))
  28. X = []
  29. Y = []
  30. W = []
  31. H = []
  32. for c in contours:
  33.       x,y,w,h = cv.boundingRect(c)
  34.       cv.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
  35.       X.append(x)
  36.       Y.append(y)
  37.       W.append(w)
  38.       Y.append(h)
  39. x_r = take_max(X)
  40. y_r = take_max(Y)
  41. w_r =take_max(W)
  42. h_r = take_max(H)
  43. max_picture = img[y_r:y_r+h, x_r:x_r+h]
  44. print("-------------------------------------------------------------------------------------------------------------------------------------------------")
  45. plt.imshow(max_picture)
  46. #print(w*h)
  47. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement