|
31 | 31 | }, |
32 | 32 | "outputs": [], |
33 | 33 | "source": [ |
34 | | - "def process_image(im):\n", |
35 | | - " image, contours, hier = cv2.findContours(im, cv2.RETR_TREE,\n", |
36 | | - " cv2.CHAIN_APPROX_SIMPLE)\n", |
37 | | - "\n", |
38 | | - " # with each contour, draw boundingRect in green\n", |
39 | | - " # a minAreaRect in red and\n", |
40 | | - " # a minEnclosingCircle in blue\n", |
41 | | - " for c in contours:\n", |
| 34 | + "greenLower = (29, 86, 6)\n", |
| 35 | + "greenUpper = (64, 255, 255)\n", |
| 36 | + " \n", |
| 37 | + "# initialize the list of tracked points, the frame counter,\n", |
| 38 | + "# and the coordinate deltas\n", |
| 39 | + "pts = deque(maxlen=20)\n", |
| 40 | + "counter = 0\n", |
| 41 | + "(dX, dY) = (0, 0)\n", |
| 42 | + "direction = \"\"\n", |
| 43 | + "filename = 'test_sample2.avi'\n", |
| 44 | + "vid = imageio.get_reader(filename, 'ffmpeg')\n", |
| 45 | + "count = 0\n", |
| 46 | + "for num in range(vid.get_length()):\n", |
| 47 | + " try:\n", |
| 48 | + " img = vid.get_data(num)\n", |
42 | 49 | " \n", |
43 | | - " # get the bounding rect\n", |
44 | | - " x, y, w, h = cv2.boundingRect(c)\n", |
| 50 | + " img = cv2.resize(img,(1000,600))\n", |
| 51 | + " gray_image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n", |
| 52 | + " _,im = cv2.threshold(gray_image,220,1,cv2.THRESH_BINARY+cv2.THRESH_OTSU)\n", |
45 | 53 | " \n", |
46 | | - " # draw a green rectangle to visualize the bounding rect\n", |
47 | | - " cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", |
| 54 | + " image, cnts, hier = cv2.findContours(im,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", |
| 55 | + " center = None\n", |
| 56 | + " for c in cnts:\n", |
| 57 | + " x, y, w, h = cv2.boundingRect(c)\n", |
48 | 58 | "\n", |
49 | | - " # get the min area rect\n", |
50 | | - " rect = cv2.minAreaRect(c)\n", |
51 | | - " box = cv2.boxPoints(rect)\n", |
52 | | - " \n", |
53 | | - " # convert all coordinates floating point values to int\n", |
54 | | - " box = np.int0(box)\n", |
55 | | - " \n", |
56 | | - " # draw a red 'nghien' rectangle\n", |
57 | | - " cv2.drawContours(img, [box], 0, (0, 0, 255))\n", |
| 59 | + " # draw a green rectangle to visualize the bounding rect\n", |
| 60 | + " cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)\n", |
| 61 | + "\n", |
| 62 | + " # get the min area rect\n", |
| 63 | + " rect = cv2.minAreaRect(c)\n", |
| 64 | + " box = cv2.boxPoints(rect)\n", |
| 65 | + "\n", |
| 66 | + " # convert all coordinates floating point values to int\n", |
| 67 | + " box = np.int0(box)\n", |
58 | 68 | "\n", |
59 | | - " cv2.drawContours(img, contours, -1, (255, 255, 0), 1)\n", |
60 | | - " return img\n" |
| 69 | + " # draw a red 'nghien' rectangle\n", |
| 70 | + " cv2.drawContours(img, [box], 0, (0, 0, 255))\n", |
| 71 | + " # only proceed if at least one contour was found\n", |
| 72 | + "\n" |
61 | 73 | ] |
62 | 74 | }, |
63 | 75 | { |
|
0 commit comments