Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4.  
  5. img = cv2.imread('cebra.jpg', 0)
  6. im = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  7. #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  8. #img = cv2.medianBlur(img,5)
  9.  
  10. th2 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C,\
  11.  cv2.THRESH_BINARY,11,2)
  12. #cv2.imshow('Detector de Lineas', th2)
  13. th3 = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
  14.  cv2.THRESH_BINARY,11,2)
  15. #cv2.imshow('Detector de Lineas', th3)
  16.  
  17.  
  18.  
  19. ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  20. #cv2.imshow('Detector de Lineas', thresh1)
  21. ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
  22. #cv2.imshow('Detector de Lineas', thresh2)
  23. ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC+cv2.THRESH_OTSU)
  24. #cv2.imshow('Detector de Lineas', thresh3)
  25. ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
  26. #cv2.imshow('Detector de Lineas', thresh4)
  27. ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)
  28. #cv2.imshow('Detector de Lineas', thresh5)
  29.  
  30.  
  31.  
  32.  
  33.  
  34. _, contours, h = cv2.findContours(thresh2, 1, 2)
  35.  
  36. cv2.drawContours(im, contours, -1, (0, 0, 255), -1 )
  37. cv2.imshow('Detector de Lineas', im)
  38. print(len(contours))
  39.  
  40. """cnt  = contours
  41. big_contour = []
  42. max = 0
  43. cont=0
  44.  
  45. for i in cnt:
  46.    #if ((cv2.contourArea(i)) > 10):
  47.    print("Contorno identificado")
  48.    cont += 1
  49.    #if(cont == 1):
  50.    cv2.drawContours(im, i, -1, (0, 0, 255), -1 )
  51.  
  52. cv2.imshow('imagen', im)
  53. #cv2.waitKey()"""
  54.  
  55. """edges = cv2.Canny(thresh2, 150, 150, apertureSize = 3)
  56.  
  57.  
  58. lines = cv2.HoughLines(edges, 1, np.pi/180, 80)
  59. print(lines)
  60.  
  61. for line in lines:
  62.    rho, theta = line[0]
  63.    a = np.cos(theta)
  64.    b = np.sin(theta)
  65.    x0 = a*rho
  66.    y0 = b*rho
  67.    x1 = int(x0 + 1000*(-b))
  68.    y1 = int(y0 + 1000*(a))
  69.    x2 = int(x0 - 1000*(-b))
  70.    y2 = int(y0 - 1000*(a))
  71.  
  72.    cv2.line(img, (x1,y1), (x2,y2), (0, 0, 255), 1, cv2.LINE_AA)"""
  73.  
  74. #cv2.imshow('Bordes de Imagen', edges)
  75. #cv2.imshow('Detector de Lineas', img)
  76. cv2.waitKey()
  77. cv2.imwrite("cebra_11.jpg", img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement