Advertisement
Guest User

Untitled

a guest
Jul 9th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import misc
  4. import cv2
  5.  
  6. carsample=cv2.imread('P1010001.jpg')
  7. #cv2.imshow('original image',carsample)
  8. gray_carsample=cv2.cvtColor(carsample, cv2.COLOR_BGR2GRAY)
  9.  
  10. #blur the image
  11. blur=cv2.GaussianBlur(gray_carsample,(5,5),0)
  12.  
  13. #find the sobel gradient
  14. sobelx=cv2.Sobel(blur, cv2.CV_8U, 1, 0, ksize=3)
  15.  
  16. #Otsu thresholding
  17. ret2,th2=cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  18. #cv2.imshow('otsu image', th2)
  19.  
  20. se=cv2.getStructuringElement(cv2.MORPH_RECT,(23,2))
  21. closing=cv2.morphologyEx(th2, cv2.MORPH_CLOSE, se)
  22. cv2.imshow("closed images",closing)
  23.  
  24.  
  25. contours, hierarchy=cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  26. print len(contours)
  27. '''
  28. cv2.drawContours(carsample, contours, -1, (0,255,0), 3)
  29. cv2.imshow("contoured image", carsample)
  30. '''
  31.  
  32.  
  33. '''
  34. for i in contours:
  35.    rect=cv2.minAreaRect(i)
  36.    box=cv2.boxPoints(rect)
  37.    box=np.int0(box)
  38.    cv2.drawContours(carsample, [box], 0, (0,0,255),2)
  39. '''
  40. cv2.waitKey(0)
  41. cv2.destroyAllWindows()
  42. plt.show()
  43.  
  44.  
  45.  
  46. ###############################
  47. the output:
  48. ##############################
  49. OpenCV Error: Assertion failed (_contours.kind() == _InputArray::STD_VECTOR_VECTOR && _contours.channels() == 2 && _contours.depth() == CV_32S) in findContours, file /home/kislay/Documents/Opop/opencv/modules/imgproc/src/contours.cpp, line 1708
  50. Traceback (most recent call last):
  51.   File "try.py", line 25, in <module>
  52.     contours, hierarchy=cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  53. cv2.error: /home/kislay/Documents/Opop/opencv/modules/imgproc/src/contours.cpp:1708: error: (-215) _contours.kind() == _InputArray::STD_VECTOR_VECTOR && _contours.channels() == 2 && _contours.depth() == CV_32S in function findContours
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement