Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from scipy import misc
- import cv2
- carsample=cv2.imread('P1010001.jpg')
- #cv2.imshow('original image',carsample)
- gray_carsample=cv2.cvtColor(carsample, cv2.COLOR_BGR2GRAY)
- #blur the image
- blur=cv2.GaussianBlur(gray_carsample,(5,5),0)
- #find the sobel gradient
- sobelx=cv2.Sobel(blur, cv2.CV_8U, 1, 0, ksize=3)
- #Otsu thresholding
- ret2,th2=cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
- #cv2.imshow('otsu image', th2)
- se=cv2.getStructuringElement(cv2.MORPH_RECT,(23,2))
- closing=cv2.morphologyEx(th2, cv2.MORPH_CLOSE, se)
- cv2.imshow("closed images",closing)
- contours, hierarchy=cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- print len(contours)
- '''
- cv2.drawContours(carsample, contours, -1, (0,255,0), 3)
- cv2.imshow("contoured image", carsample)
- '''
- '''
- for i in contours:
- rect=cv2.minAreaRect(i)
- box=cv2.boxPoints(rect)
- box=np.int0(box)
- cv2.drawContours(carsample, [box], 0, (0,0,255),2)
- '''
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- plt.show()
- ###############################
- the output:
- ##############################
- 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
- Traceback (most recent call last):
- File "try.py", line 25, in <module>
- contours, hierarchy=cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
- 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