def applyFilter(self):
        imycrcb = cv2.cvtColor(self.img,cv.CV_BGR2YCrCb)
        lowx = min(self.pt1[0],self.pt2[0])
        highx = max(self.pt1[0],self.pt2[0])
        lowy = min(self.pt1[1],self.pt2[1])
        highy = max(self.pt1[1],self.pt2[1])
        y = []
        cr = []
        cb = []

        for i in range(lowx,highx+1):
            for j in range(lowy,highy+1):
                y.append(imycrcb[i,j,0])
                cr.append(imycrcb[i,j,1])
                cb.append(imycrcb[i,j,2])

        ymean = np.mean(y)
        crmean = np.mean(cr)
        cbmean = np.mean(cb)
        ystd = np.std(y)
        crstd = np.std(cr)
        cbstd = np.std(cb)

        print (ymean,crmean,cbmean)
        print (ystd,crstd,cbstd)

        self.filter = fe.colourFilter((ymean-ystd*2*delta,crmean-crstd*delta,cbmean-cbstd*delta),(ymean+ystd*2*delta,crmean+crstd*delta,cbmean+cbstd*delta))
        hull = self.filter.getColourHull(self.img)
        print self.filter.low
        print self.filter.high

        imgray = cv2.cvtColor(self.img,cv.CV_BGR2GRAY)
        cv2.drawContours(imgray,[hull],-1,(255,0,0),2)
        self.displayImage = imgray
Beispiel #2
0
    def applyFilter(self):
        imycrcb = cv2.cvtColor(self.img,cv.CV_BGR2YCrCb)
        lowx = min(self.pt1[0],self.pt2[0])
        highx = max(self.pt1[0],self.pt2[0])
        lowy = min(self.pt1[1],self.pt2[1])
        highy = max(self.pt1[1],self.pt2[1])
        y = []
        cr = []
        cb = []

        for i in range(lowx,highx+1):
            for j in range(lowy,highy+1):
                y.append(imycrcb[i,j,0])
                cr.append(imycrcb[i,j,1])
                cb.append(imycrcb[i,j,2])

        ymean = np.mean(y)
        crmean = np.mean(cr)
        cbmean = np.mean(cb)
        ystd = np.std(y)
        crstd = np.std(cr)
        cbstd = np.std(cb)

        print (ymean,crmean,cbmean)
        print (ystd,crstd,cbstd)

        self.filter = fe.colourFilter((ymean-ystd*2*delta,crmean-crstd*delta,cbmean-cbstd*delta),(ymean+ystd*2*delta,crmean+crstd*delta,cbmean+cbstd*delta))
        hull = self.filter.getColourHull(self.img)
        print self.filter.low
        print self.filter.high

        imgray = cv2.cvtColor(self.img,cv.CV_BGR2GRAY)
        cv2.drawContours(imgray,[hull],-1,(255,0,0),2)
        self.displayImage = imgray
Beispiel #3
0
def getFeatures(folder):
    features = []
    with open(os.path.join(folder,"calibration.csv")) as csvfile:
        reader = csv.reader(csvfile)
        low = [ float(x) for x in reader.next()]
        high = [ float(x) for x in reader.next()]
        green = fe.colourFilter(tuple(low),tuple(high))
        low = [ float(x) for x in reader.next()]
        high = [ float(x) for x in reader.next()]
        blue = fe.colourFilter(tuple(low),tuple(high))

    for f in os.listdir(folder):
        if os.path.splitext(f)[1] == ".ppm":
            imbgr = cv2.imread(os.path.join(folder,f))
            hull = green.getColourHull(imbgr)

            features.append(fe.getFeatureVector(hull,['central']))
            hull = blue.getColourHull(imbgr)
            features.append(fe.getFeatureVector(hull,['central']))
    return features
Beispiel #4
0
    def get_error(self, imbgr,low,high,ideal,blue=False):
        filt = fe.colourFilter(low,high)
        if blue:
            cnt = filt.getColourContours(imbgr)
            feature = self.blue_contours(cnt)
        else:
            if len(filt.getColourContours(imbgr)) != 5:
                return 20
            hull = filt.getColourHull(imbgr)
            if len(hull):
                m = cv2.moments(hull)
                feature = np.array([m['nu20'],m['nu11'],m['nu02'],m['nu30'],m['nu21'],m['nu12'],m['nu03']])
            else:
                feature = np.array([0,0,0,0,0,0,0])

        dist = np.sqrt(np.sum(np.square(ideal-feature)))
        return dist
Beispiel #5
0

if __name__ == "__main__":

    modelfile = open(sys.argv[2])

    model = pickle.load(modelfile)
    labels = pickle.load(modelfile)
    running = deque([])

    with open(sys.argv[1]) as csvfile:
        reader = csv.reader(csvfile)
        low = [ float(x) for x in reader.next()]
        high = [ float(x) for x in reader.next()]

    green = fe.colourFilter(tuple(low),tuple(high))

    while 1:
        try:
            imbgr = fe.get_video()
            hull = green.getColourHull(imbgr)
            if len(hull):
                features = fe.getFeatureVector(hull,['central'])

                imbgrclass = model.predict([features])
                imbgrclass = imbgrclass[0]
                
                if len(running) < 10:
                    sign = imbgrclass
                    running.append(sign)
                else:
Beispiel #6
0
    def autocalibration(self, calibration_file, calibration_folder):
        g_m,r_m,b_m,c = self.get_features(calibration_folder)

        got_green = False
        got_blue = False
        got_red = False

        gcount = 0
        bcount = 0
        rcount = 0
        frame_count = 0

        glow,ghigh = self.get_start(c,'glow','ghigh')
        blow,bhigh = self.get_start(c,'blow','bhigh')
        rlow,rhigh = self.get_start(c,'rlow','rhigh')

        while 1:
            try:
                imbgr = np.array(fe.get_video())

                #print "green"
                got_green,gcount,glow,ghigh = self.optimize(imbgr,got_green,gcount,glow,ghigh,g_m,c,"glow","ghigh",0.05)
                #print "red"
                got_red,rcount,rlow,rhigh = self.optimize(imbgr,got_red,rcount,rlow,rhigh,r_m,c,"rlow","rhigh",0.05)
                #print "blue"
                got_blue,bcount,blow,bhigh = self.optimize(imbgr,got_blue,bcount,blow,bhigh,b_m,c,"blow","bhigh",0.05,blue=True)
                
                green = fe.colourFilter(glow,ghigh)   
                hull = green.getColourHull(imbgr)   
                if len(hull):
                    cv2.drawContours(imbgr,[hull],-1,(0,255,0),2) 

                red = fe.colourFilter(rlow,rhigh)   
                hull = red.getColourHull(imbgr)   
                if len(hull):
                    cv2.drawContours(imbgr,[hull],-1,(0,0,255),2) 

                blue = fe.colourFilter(blow,bhigh)   
                cnt = blue.getColourContours(imbgr)
                if len(cnt) >= 2:
                    cnt_sorted = sorted(cnt,key=cv2.contourArea)
                    cv2.drawContours(imbgr,[cnt_sorted[-1]],-1,(255,0,0),2)  
                    cv2.drawContours(imbgr,[cnt_sorted[-2]],-1,(255,0,0),2)  

                cv2.imshow("Demo",imbgr)

                if got_green and got_red and got_blue:
                    frame_count += 1

            except KeyboardInterrupt:
                break
            if cv.WaitKey(10) == 32:
                break
            if frame_count >= 30:
                break

        with open(calibration_file,'w') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(glow)
            writer.writerow(ghigh)
            writer.writerow(blow)
            writer.writerow(bhigh)
            writer.writerow(rlow)
            writer.writerow(rhigh)

        return glow,ghigh,blow,bhigh,rlow,rhigh