|
| 1 | +# importing libraries |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +# initialize camera settings |
| 6 | +frameWidth = 640 |
| 7 | +frameHeight = 480 |
| 8 | +cap = cv2.VideoCapture(0) |
| 9 | +cap.set(3, frameWidth) |
| 10 | +cap.set(4, frameHeight) |
| 11 | + |
| 12 | +def empty(a): |
| 13 | + pass |
| 14 | + |
| 15 | +# name of the window |
| 16 | +cv2.namedWindow("Parameters") |
| 17 | + |
| 18 | +# resize the window |
| 19 | +cv2.resizeWindow("Parameters",640,240) |
| 20 | + |
| 21 | +#creating trackbars |
| 22 | +cv2.createTrackbar("Threshold1","Parameters",23,255,empty) |
| 23 | +cv2.createTrackbar("Threshold2","Parameters",20,255,empty) |
| 24 | +cv2.createTrackbar("Area","Parameters",5000,30000,empty) |
| 25 | + |
| 26 | +# function for stacking images together |
| 27 | +def stackImages(scale,imgArray): |
| 28 | + rows = len(imgArray) |
| 29 | + cols = len(imgArray[0]) |
| 30 | + rowsAvailable = isinstance(imgArray[0], list) |
| 31 | + width = imgArray[0][0].shape[1] |
| 32 | + height = imgArray[0][0].shape[0] |
| 33 | + if rowsAvailable: |
| 34 | + for x in range ( 0, rows): |
| 35 | + for y in range(0, cols): |
| 36 | + if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]: |
| 37 | + imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) |
| 38 | + else: |
| 39 | + imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) |
| 40 | + if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR) |
| 41 | + imageBlank = np.zeros((height, width, 3), np.uint8) |
| 42 | + hor = [imageBlank]*rows |
| 43 | + hor_con = [imageBlank]*rows |
| 44 | + for x in range(0, rows): |
| 45 | + hor[x] = np.hstack(imgArray[x]) |
| 46 | + ver = np.vstack(hor) |
| 47 | + else: |
| 48 | + for x in range(0, rows): |
| 49 | + if imgArray[x].shape[:2] == imgArray[0].shape[:2]: |
| 50 | + imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) |
| 51 | + else: |
| 52 | + imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale) |
| 53 | + if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) |
| 54 | + hor= np.hstack(imgArray) |
| 55 | + ver = hor |
| 56 | + return ver |
| 57 | + |
| 58 | +def getContours(img,imgContour): |
| 59 | + contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) |
| 60 | + |
| 61 | + # detect the area of each contour and based on the area we can remove all the contours that we are not interested in |
| 62 | + # so in order to do that we will need a for loop |
| 63 | + for cnt in contours: |
| 64 | + area = cv2.contourArea(cnt) |
| 65 | + areaMin = cv2.getTrackbarPos("Area", "Parameters") |
| 66 | + if area > areaMin: |
| 67 | + cv2.drawContours(imgContour, cnt, -1, (255, 0, 255), 7) |
| 68 | + |
| 69 | + # find the corner points, so in order to do that we need to first find the length of contours |
| 70 | + # true basically means that the contour is closed |
| 71 | + peri = cv2.arcLength(cnt, True) |
| 72 | + |
| 73 | + # to approximate what type of shape this is, we will use the approximation of poly method |
| 74 | + # we will input the contour, will give it a resolution and then we will define again that this is a closed contour |
| 75 | + approx = cv2.approxPolyDP(cnt, 0.02 * peri, True) |
| 76 | + print(len(approx)) |
| 77 | + |
| 78 | + # create a bounding box because we need to highlight the area of where the object is |
| 79 | + x , y , w, h = cv2.boundingRect(approx) |
| 80 | + cv2.rectangle(imgContour, (x , y ), (x + w , y + h ), (0, 255, 0), 5) |
| 81 | + |
| 82 | + # to display values so that we can easily see the number of points and the area detected |
| 83 | + cv2.putText(imgContour, "Points: " + str(len(approx)), (x + w + 20, y + 20), cv2.FONT_HERSHEY_COMPLEX, .7, (0, 255, 0), 2) |
| 84 | + cv2.putText(imgContour, "Area: " + str(int(area)), (x + w + 20, y + 45), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 255, 0), 2) |
| 85 | + |
| 86 | +# reading each frame |
| 87 | +while True: |
| 88 | + success, img = cap.read() |
| 89 | + imgContour = img.copy() |
| 90 | + |
| 91 | + # converting img into blur version |
| 92 | + imgBlur = cv2.GaussianBlur(img, (7, 7), 1) |
| 93 | + |
| 94 | + # converting it into gray scale |
| 95 | + imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY) |
| 96 | + |
| 97 | + # define trackbar positions |
| 98 | + threshold1 = cv2.getTrackbarPos("Threshold1", "Parameters") |
| 99 | + threshold2 = cv2.getTrackbarPos("Threshold2", "Parameters") |
| 100 | + |
| 101 | + # canny edge detector |
| 102 | + imgCanny = cv2.Canny(imgGray,threshold1,threshold2) |
| 103 | + |
| 104 | + # define kernel for dilation |
| 105 | + kernel = np.ones((5, 5)) |
| 106 | + |
| 107 | + # to overcome the overlaps and noise, we use dilation function |
| 108 | + imgDil = cv2.dilate(imgCanny, kernel, iterations=1) |
| 109 | + getContours(imgDil,imgContour) |
| 110 | + |
| 111 | + # stacking images together as we want images side by side instead of different window |
| 112 | + imgStack = stackImages(0.8,([img,imgCanny], |
| 113 | + [imgDil,imgContour])) |
| 114 | + |
| 115 | + # displaying it |
| 116 | + cv2.imshow("Result", imgStack) |
| 117 | + if cv2.waitKey(1) & 0xFF == ord('q'): |
| 118 | + break |
0 commit comments