Esempio n. 1
0
def stack_different_imgs(imgs, cnt):
    stack = list()
    for img in imgs:
        img_tmp, bb = ia.get_fragment(img, cnt)
        stack.append(img_tmp)
    img_stacked = np.dstack(stack)
    return img_stacked, bb
Esempio n. 2
0
 #read current frame
 ret,img = cap.read()
 #if current frame does not exist break
 if not ret:
     break
 
 #make a first prediction for the whole current frame
 img_whole = nh.evaluate_plus_resizing(net_whole,img,dim_whole,device=device,normalize=nh.pytorch_normalize_3) 
     
 #find contour of the shape of the first prediction
 cnts = ia.filtered_contours(img_whole)
 #make a second more accurate prediciton for each object of the first prediction
 #and combine them into a new image "img_whole_plus_fragment"
 img_whole_plus_fragment = np.zeros(img_whole.shape,dtype=np.uint8)
 for cnt in cnts:
     img_tmp, bb = ia.get_fragment(img,cnt)
     pos = nh.evaluate_plus_resizing(net_fragment,img_tmp,dim_fragment,device=device,normalize=nh.pytorch_normalize_3)
     neg = nh.evaluate_plus_resizing(net_fragment_negative,img_tmp,dim_fragment,device=device,normalize=nh.pytorch_normalize_3)
     img_fragment = combine_pos_neg(pos,neg)
     img_fragment = cv2.erode(img_fragment,ia.get_kernel(8))
     img_fragment = ia.remove_filtered_contours(img_fragment)
     img_whole_plus_fragment[bb[0]:bb[1],bb[2]:bb[3]] += img_fragment
 
 #find all the object from the second prediction
 cnts = ia.filtered_contours(img_whole_plus_fragment)
 
 #if mice are seperated
 if len(cnts) == 2:
     #determine which mice is which
     centers = ia.center_of_contours(cnts)
     if get_distance(centers[0],centers_last[0]) + get_distance(centers[1],centers_last[1]) >= get_distance(centers[0],centers_last[1]) + get_distance(centers[1],centers_last[0]):
Esempio n. 3
0
print('predicting behavior')
while 1:
    ret, img = cap.read()
    if not ret:
        break
    #dm.lucid_img(img,'img')
    img_whole = nh.evaluate_plus_resizing(net_whole,
                                          img,
                                          dim_whole,
                                          device=device,
                                          normalize=nh.pytorch_normalize_3)

    cnts = ia.filtered_contours(img_whole)
    img_whole_plus_fragment = np.zeros(img_whole.shape, dtype=np.uint8)
    for cnt in cnts:
        img_tmp, bb = ia.get_fragment(img, cnt)
        pos = nh.evaluate_plus_resizing(net_fragment,
                                        img_tmp,
                                        dim_fragment,
                                        device=device,
                                        normalize=nh.pytorch_normalize_3)
        neg = nh.evaluate_plus_resizing(net_fragment_negative,
                                        img_tmp,
                                        dim_fragment,
                                        device=device,
                                        normalize=nh.pytorch_normalize_3)
        img_fragment = combine_pos_neg(pos, neg)
        img_fragment = ia.remove_filtered_contours(img_fragment)

        img_whole_plus_fragment[bb[0]:bb[1], bb[2]:bb[3]] += img_fragment
            img1 = cv2.bitwise_and(img1, img1, mask=bin1)
            img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
            img2 = cv2.bitwise_and(img2, img2, mask=bin2)
            img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

            #remove part of the mouse in the back which is overlapping with the mice in the front and add both images together
            img2 = cv2.bitwise_and(img2, img2, mask=cv2.bitwise_not(bin1))
            img_c = img1 + img2

            #apply histogramm equalization
            img_c = cv2.equalizeHist(img_c)

            #get the close up image of both overlapping mice
            cnt1 = ia.get_max_contour(bin1)
            cnt2 = ia.get_max_contour(bin2)
            img_c1, bb1 = ia.get_fragment(img_c, cnt1)
            img_c2, bb2 = ia.get_fragment(img_c, cnt2)

            #create a combined binary image
            bin_g1 = bin1
            bin_g2 = bin2
            bin_c = np.stack((bin_g1, bin_g2), axis=2)

            #get the same close up image of the binary iamge
            bin_c1, bb1 = ia.get_fragment(bin_c, cnt1)
            bin_c2, bb2 = ia.get_fragment(bin_c, cnt2)

            #reshape into consistent shape
            img_c1 = cv2.resize(img_c1, dim, interpolation=cv2.INTER_AREA)
            bin_c1 = cv2.resize(bin_c1, dim, interpolation=cv2.INTER_AREA)
            img_c2 = cv2.resize(img_c2, dim, interpolation=cv2.INTER_AREA)