|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# import system modules |
| 4 | +import cv2.cv as cv |
| 5 | +#import cv |
| 6 | +global imghsv |
| 7 | + |
| 8 | +# purpose: using HSV thresholds, detects blue, yellow and purple objects in a video stream in three new windows |
| 9 | +# 1) a black/white stream showing objects matching threshold values (window "threshold") |
| 10 | +# 2) a black/color stream tracking the locations of the objects in their respective colors (window "final") |
| 11 | +# 3) a full-color stream showing the original video and the bounding boxes of detected objects (window "real") |
| 12 | + |
| 13 | +# things that would make this script more useful for future tests: |
| 14 | +# 1) GUI HSV threshold and minimum pixel size sliders like Kevin has added to the Canny Edge Detection program |
| 15 | +# 2) Limit the number of blue/yellow/purple objects that can be detected at one time to one |
| 16 | + |
| 17 | +# source from: |
| 18 | +# http://stackoverflow.com/questions/8152504/tracking-two-different-colors-using-opencv-2-3-and-python |
| 19 | + |
| 20 | +# definitely works with Mac OSX, Python 2.7, and OpenCV library |
| 21 | + |
| 22 | +# to modify color thresholds, change the cv.Scalar values in the InRange method in the gettresholdedimg function below |
| 23 | + |
| 24 | +def getthresholdedimg(im): |
| 25 | + |
| 26 | + # this function take RGB image.Then convert it into HSV for easy colour detection |
| 27 | + # and threshold it with yellow and blue part as white and all other regions as black.Then return that image |
| 28 | + |
| 29 | + global imghsv |
| 30 | + imghsv = cv.CreateImage(cv.GetSize(im),8,3) |
| 31 | + |
| 32 | + # Convert image from RGB to HSV |
| 33 | + cv.CvtColor(im,imghsv,cv.CV_BGR2HSV) |
| 34 | + |
| 35 | + # creates images for blue |
| 36 | + imgblue = cv.CreateImage(cv.GetSize(im),8,1) |
| 37 | + |
| 38 | + # creates blank image to which color images are added |
| 39 | + imgthreshold = cv.CreateImage(cv.GetSize(im),8,1) |
| 40 | + |
| 41 | + # determine HSV color thresholds for yellow, blue, and green |
| 42 | + # cv.InRange(src, lowerbound, upperbound, dst) |
| 43 | + # for imgblue, lowerbound is 95, and upperbound is 115 |
| 44 | + cv.InRangeS(imghsv, cv.Scalar(55,100,100), cv.Scalar(155,255,255), imgblue ) |
| 45 | + |
| 46 | + # add color thresholds to blank 'threshold' image |
| 47 | + cv.Add(imgthreshold, imgblue, imgthreshold) |
| 48 | + |
| 49 | + return imgthreshold |
| 50 | + |
| 51 | +# capture from m4v |
| 52 | +#capture = cv.CaptureFromFile('/Users/glenaronson/Desktop/blimp_large.m4v') |
| 53 | +capture = cv.CaptureFromFile('C:/Users/Jeremy/Documents/GitHub/blimp_blobs/blimp.m4v') |
| 54 | +#capture = cv.CaptureFromFile("C:\Users\Jeremy\Documents\GitHub\blimp_blobs\blimp.m4v") |
| 55 | +#capture = cv.CaptureFromFile('http://10.129.20.11/snapshot/view0.jpg') |
| 56 | +#capture = cv.CaptureFromFile('rtsp://10.129.20.11/video.sav') |
| 57 | +#http://10.129.20.11/goform/stream?cmd=get&channel=0 |
| 58 | +#capture = cv.CaptureFromCAM(0) |
| 59 | +#capture = cv.CaptureFromFile("C:/Users/Jeremy/Documents/GitHub/blimp_blobs/Untitled 13.avi") |
| 60 | +#capture = cv.CaptureFromFile("Untitled 13.avi") |
| 61 | +frame = cv.QueryFrame(capture) |
| 62 | +frame_size = cv.GetSize(frame) |
| 63 | + |
| 64 | +#frame = cv.LoadImageM("C:/Users/Jeremy/Documents/GitHub/blimp_blobs/test_images/SmallBlimp/(10.129.20.12)_1_2012_08_21_04_12_07.jpg") |
| 65 | +#frame_size = cv.GetSize(frame) |
| 66 | + |
| 67 | +# blank images to which images are added later |
| 68 | +test = cv.CreateImage(cv.GetSize(frame),8,3) |
| 69 | +img2 = cv.CreateImage(cv.GetSize(frame),8,3) |
| 70 | + |
| 71 | +# three windows that will open upon execution |
| 72 | +cv.NamedWindow("Real",0) |
| 73 | + |
| 74 | +# blank lists to store coordinates of blue blob |
| 75 | +blue = [] |
| 76 | + |
| 77 | + |
| 78 | +while(1): |
| 79 | + # captures feed from video in color |
| 80 | + color_image = cv.QueryFrame(capture) |
| 81 | + #color_image = frame |
| 82 | + |
| 83 | + # ?? |
| 84 | + imdraw = cv.CreateImage(cv.GetSize(frame), 8, 3) |
| 85 | + |
| 86 | + # ?? |
| 87 | + cv.SetZero(imdraw) |
| 88 | + cv.Flip(color_image,color_image, 1) |
| 89 | + cv.Smooth(color_image, color_image, cv.CV_GAUSSIAN, 3, 0) |
| 90 | + # ?? |
| 91 | + imgbluethresh = getthresholdedimg(color_image) |
| 92 | + cv.Erode(imgbluethresh, imgbluethresh, None, 3) |
| 93 | + cv.Dilate(imgbluethresh, imgbluethresh, None, 10) |
| 94 | + # ?? |
| 95 | + img2 = cv.CloneImage(imgbluethresh) |
| 96 | + # ?? |
| 97 | + storage = cv.CreateMemStorage(0) |
| 98 | + contour = cv.FindContours(imgbluethresh, storage, cv.CV_RETR_CCOMP, cv.CV_CHAIN_APPROX_SIMPLE) |
| 99 | + |
| 100 | + # blank list into which points for bounding rectangles around blobs are appended |
| 101 | + points = [] |
| 102 | + |
| 103 | + # this is the new part here. ie use of cv.BoundingRect() |
| 104 | + while contour: |
| 105 | + |
| 106 | + # Draw bounding rectangles |
| 107 | + bound_rect = cv.BoundingRect(list(contour)) |
| 108 | + contour = contour.h_next() |
| 109 | + #print contour # not sure why print contour |
| 110 | + |
| 111 | + # for more details about cv.BoundingRect,see documentation |
| 112 | + pt1 = (bound_rect[0], bound_rect[1]) |
| 113 | + pt2 = (bound_rect[0] + bound_rect[2], bound_rect[1] + bound_rect[3]) |
| 114 | + points.append(pt1) |
| 115 | + points.append(pt2) |
| 116 | + cv.Rectangle(color_image, pt1, pt2, cv.CV_RGB(255,0,0), 1) |
| 117 | + |
| 118 | + # calculating centroids |
| 119 | + centroidx = cv.Round((pt1[0]+pt2[0])/2) |
| 120 | + centroidy = cv.Round((pt1[1]+pt2[1])/2) |
| 121 | + |
| 122 | + # identifying if blue blobs exist and adding centroids to corresponding lists. |
| 123 | + # note that the lower and upper bounds correspond to the the lower and upper bounds |
| 124 | + # in the getthresholdedimg(im): function earlier in the script. |
| 125 | + # e.g., yellow has a lowerbound of 95 and upper bound of 115 in both sections of code |
| 126 | + if (55 < cv.Get2D(imghsv,centroidy,centroidx)[0] < 155): |
| 127 | + blue.append((centroidx,centroidy)) |
| 128 | + |
| 129 | + # draw colors in windows; exception handling is used to avoid IndexError. |
| 130 | + # after drawing is over, centroid from previous part is removed from list by pop. |
| 131 | + # so in next frame, centroids in this frame become initial points of line to draw |
| 132 | + |
| 133 | + # draw blue box around blue blimp blob |
| 134 | + try: |
| 135 | + cv.Circle(imdraw, blue[1], 5, (255,0,0)) |
| 136 | + cv.Line(imdraw, blue[0], blue[1], (255,0,0), 3, 8, 60) |
| 137 | + blue.pop(0) |
| 138 | + print("centroid x:" + str(centroidx)) |
| 139 | + print("centroid y:" + str(centroidy)) |
| 140 | + print("") |
| 141 | + except IndexError: |
| 142 | + print "no blimp detected" |
| 143 | + |
| 144 | + |
| 145 | + # adds |
| 146 | + cv.Add(test,imdraw,test) |
| 147 | + |
| 148 | + # display windows previously created |
| 149 | + cv.ShowImage("Real", color_image) |
| 150 | + |
| 151 | + if cv.WaitKey(33) == 1048603: |
| 152 | + cv.DestroyWindow("Real") |
| 153 | + break |
| 154 | +###################################################### |
0 commit comments