Esempio n. 1
0
def thresh_callback(val, src_gray):
    threshold = val
    rng.seed(12345)
    canny_output = cv2.Canny(src_gray, threshold, threshold * 2)

    contours, hierarchy = cv2.findContours(canny_output, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)[-2:]

    contours_poly = [None] * len(contours)
    boundRect = [None] * len(contours)
    centers = [None] * len(contours)
    radius = [None] * len(contours)
    for i, c in enumerate(contours):
        contours_poly[i] = cv2.approxPolyDP(c, 3, True)
        boundRect[i] = cv2.boundingRect(contours_poly[i])
        centers[i], radius[i] = cv2.minEnclosingCircle(contours_poly[i])

    drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3),
                       dtype=np.uint8)

    for i in range(len(contours)):
        color = (rng.randint(0, 256), rng.randint(0, 256), rng.randint(0, 256))
        cv2.drawContours(drawing, contours_poly, i, color)
        cv2.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
           (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
        cv2.circle(drawing, (int(centers[i][0]), int(centers[i][1])),
                   int(radius[i]), color, 2)
Esempio n. 2
0
def detect_ball(frame):
    x, y, radius = -1, -1, -1
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_frame, HSV_lower, HSV_upper)
    mask = cv2.erode(mask, None, iterations=0)
    mask = cv2.dilate(mask, None, iterations=12)
    im2, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                                cv2.CHAIN_APPROX_SIMPLE)
    center = (-1, -1)

    # only proceed if at least one contour was found
    if len(contours) > 0:
        # find the largest contour in the mask, then use
        # it to compute the minimum enclosing circle and
        # centroid
        c = max(contours, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        xList.append(x)  # x coordinates
        xPath.append((x - xStart) / pathLength)  # path traveled
        M = cv2.moments(mask)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        # check that the radius is larger than some threshold
        if radius > minRadius:
            #outline ball
            cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 0), 2)
            #show ball center
            cv2.circle(frame, center, 5, (0, 255, 0), -1)

    return center[0], center[1], radius
Esempio n. 3
0
def watershedf():
    image = threshold()

    D = ndimage.distance_transform_edt(image)
    localMax = peak_local_max(D, indices=False, min_distance=20, labels=image)

    markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
    labels = watershed((-1) * D, markers, mask=image)

    original_image = image

    for label in np.unique(labels):
        if label == 0:
            continue

        mask = np.zeros(image.shape, dtype="uint8")
        mask[labels == label] = 255
        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        c = max(cnts, key=cv2.contourArea)
        ((x, y), r) = cv2.minEnclosingCircle(c)
        cv2.circle(original_image, (int(x), int(y)), int(r), (0, 255, 0), 2)
        cv2.putText(original_image, "#{}".format(label), (int(x) - 10, int(y)),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

    image = original_image
    show_img(ImageTk.PhotoImage(Img.fromarray(image)))
Esempio n. 4
0
def center_of_mass(image):
    threshold = thresh

    # Convert image to gray and blur it
    src_gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    src_gray = cv.blur(src_gray, (3, 3))

    # Edge detection
    canny_output = cv.Canny(src_gray, threshold, threshold * 2)

    contours, _ = cv.findContours(canny_output, cv.RETR_TREE,
                                  cv.CHAIN_APPROX_SIMPLE)

    # For every found contour we now apply approximation to polygons with accuracy +-3 and stating that the curve must be closed.
    # After that we find a bounding rect for every polygon and save it to boundRect.
    # At last we find a minimum enclosing circle for every polygon and save it to center and radius vectors.
    contours_poly = [None] * len(contours)
    boundRect = [None] * len(contours)
    centers = [None] * len(contours)
    radius = [None] * len(contours)

    for i, c in enumerate(contours):
        i = 0
        contours_poly[i] = cv.approxPolyDP(c, 3, True)
        boundRect[i] = cv.boundingRect(contours_poly[i])
        centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i])
        return centers[0]
Esempio n. 5
0
def maxThreshCalc(img):

    if len(img) > 0:
        c = max(img, key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)

        return radius
def circle(surfaces_body, img):
    '''
    外接圆的计算、绘制

    参数:
        surfaces_body:二维列表surfaces_body[i]中代表存储标号为i+1的堆石块体的信息,surfaces_body[i][0]以二维0-1数组(n,m)存储堆石块体的连通域,surfaces[i][1:]存储该连通域的多个轮廓顶点坐标
        img:二维np数组(n,2),存储原始图像

    返回值:
        circles:二维列表,circles[i]存储外接圆的信息,第一个元素表示圆心横坐标,第二个元素表示圆心纵坐标,第三个元素表示最小外接圆的半径
        img:二维np数组(n,2),存储绘制有所有堆石最小外接圆的图像图像
    '''
    circles = []
    for i in range(len(surfaces_body)):
        radius_max = 0
        for j in surfaces_body[i][1:]:
            #计算外接圆
            (rx, ry), radius = cv2.minEnclosingCircle(np.float32(j))

            #保留半径最大的外接圆
            if radius_max == 0:
                circles.append([rx, ry, radius])
                radius_max = radius
            if radius_max <= radius and radius_max != 0:
                circles[i] = [rx, ry, radius]
                radius_max = radius

        center = (int(circles[i][1]), int(circles[i][0]))
        radius = int(circles[i][2])

        #筛选细碎分割面的外接圆
        if radius >= (max(img.shape) / 20):
            img = cv2.circle(img, center, radius, (255, 0, 0), 2)  #绘制外接圆

    return circles, img
def ConvexHull_Cal(contour):

    IsTriangle = lambda a, b, c: a + b > c and a + c > b and b + c > a  #任意两边和必须大于第三边

    point_list = []
    convex_angle_ls = []
    concave_angle_ls = []

    epsilon = 0.003 * cv2.arcLength(contour, True)
    contour = cv2.approxPolyDP(contour, epsilon, True)  #轮廓近似,Douglas-Peucker算法
    hull = cv2.convexHull(contour, returnPoints=False)
    defects = cv2.convexityDefects(contour, hull)
    _, radius = cv2.minEnclosingCircle(contour)

    if defects is not None:
        for i in range(defects.shape[0]):
            s, e, f, _ = defects[i, 0]
            sta = tuple(contour[s][0])
            end = tuple(contour[e][0])
            far = tuple(contour[f][0])
            point_list.append([sta, far, end])

    #下面的角边标示含义见文件夹里的图片说明
    if len(point_list) >= 2:
        for it_1, it_2 in zip(point_list, point_list[1:] + point_list[:1]):
            CA = scfun.Eucledian_Distance(it_1[1], it_1[2])  #far to end
            AB = scfun.Eucledian_Distance(it_1[2], it_2[1])  #end to next far
            #凸包的角度
            if radius <= CA + AB < 2 * radius:
                BC = scfun.Eucledian_Distance(it_1[1],
                                              it_2[1])  #far to 2nd far,为底边
                if IsTriangle(CA, AB, BC):
                    angle = acos((CA**2 + AB**2 - BC**2) / (2 * CA * AB))
                    convex_angle_ls.append(angle)
            #凹陷的角度
            DC = scfun.Eucledian_Distance(it_1[0], it_1[1])  #sta to far
            if radius <= DC + CA < 2 * radius:
                DA = scfun.Eucledian_Distance(it_1[0],
                                              it_1[2])  #sta to end,为底边
                if IsTriangle(DC, CA, DA):
                    angle = acos((CA**2 + DC**2 - DA**2) / (2 * CA * DC))
                    concave_angle_ls.append(angle)

        convex_angle = [x for x in convex_angle_ls
                        if pi / 18 <= x <= pi / 6]  #凸包角度:10度至30度
        convex_len = len(convex_angle)
        concave_angle = [
            x for x in concave_angle_ls if pi / 18 <= x <= pi / 3.5
        ]
        concave_len = len(concave_angle)

        result = [convex_len, concave_len]

    else:
        result = [0, 0]

    return result
Esempio n. 8
0
def main():
    global pub, coo, cap, cX, cY
    coo.x = 0
    coo.y = 0
    cap = cv2.VideoCapture(0)
    while not rospy.is_shutdown():
        while (cap.isOpened()):
            ret, frame = cap.read()
            # cv2.imshow('frame',frame)

            hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            lower_bound = np.array([25, 50, 50])
            upper_bound = np.array([50, 255, 255])
            mask = cv2.inRange(hsv, lower_bound, upper_bound)
            res = cv2.bitwise_and(frame, frame, mask=mask)
            kernel_e = np.ones((3, 3), dtype=np.uint8)
            res2 = cv2.erode(res, kernel=kernel_e, iterations=2)
            res3 = cv2.dilate(res2, kernel=kernel_e, iterations=2)
            # blur=cv.GaussianBlur(res3,(5,5),0)
            blur_gray = cv2.cvtColor(res3, cv2.COLOR_BGR2GRAY)
            _, cnts, _ = cv2.findContours(blur_gray, cv2.RETR_EXTERNAL,
                                          cv2.CHAIN_APPROX_SIMPLE)
            x = 0
            y = 0
            radius = 0
            for i in cnts:
                if len(i) > 50:
                    #M = cv.moments(i)
                    #cX = int(M["m10"] / M["m00"])
                    #cY = int(M["m01"] / M["m00"])
                    (x, y), radius = cv2.minEnclosingCircle(i)
                    #cv2.drawContours(res3, [i], -1, (0, 255, 0), 2)
                    #cv2.circle(res3, (cX, cY), 7, (255, 255, 255), -1)
                    #cv2.putText(res3, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)

            coo.x = x
            coo.y = y
            coo.radius = radius
            pub.publish(coo)

            # cv2.drawContours(res3, [i], -1, (0, 255, 0), 2)
            # cv2.circle(res3, (cX, cY), 7, (255, 255, 255), -1)
            # cv2.putText(res3, "center", (cX - 20, cY - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
            # cv.imshow('at',at)
            # cv2.imshow('res',res)
            # cv2.imshow('res2',res2)
            cv2.imshow('res3', res3)
            # cv2.imshow('blur', blur_gray)
            # cv2.imshow('res3', res3)
            # # cv.imshow('gray', gray)
            # cv2.imshow('mask',mask)
            # cv2.imshow('hsv',hsv)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                cap.release()
                cv2.destroyAllWindows()
def selectContours(img, contours, range):
    for c in contours:
        area = cv2.contourArea(c)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        if area > range:  # If area of found contour is greater than 1000 execute
            cx, cy = contourCenter(c)  # Center of center
            cv2.circle(img, (cx, cy), int(radius), (0, 0, 255),
                       thickness=5)  # Draw a circle around found contour
    return img
Esempio n. 10
0
def Gestures_Detect(hand, sample_list, fourier_des_ls):
    ndefects = 0

    sign, large_cout = Find_Contour(hand, sample_list, fourier_des_ls)
    if sign == False:
        ndefects = 11  #返回contours为空的信息,只作调试用
        center = tuple([a // 2 for a in reversed(hand.shape)])  #返回图像的中心坐标
        return hand, ndefects, center

    black2 = np.ones(hand.shape, np.uint8)  #创建黑色幕布
    cv2.drawContours(black2, large_cout, -1, (255, 255, 255), 2)  #绘制白色轮廓
    cv2.imshow('large_cout', black2)

    hull = cv2.convexHull(large_cout, returnPoints=False)
    defects = cv2.convexityDefects(large_cout, hull)
    _, radius = cv2.minEnclosingCircle(large_cout)

    if defects is not None:
        for i in range(defects.shape[0]):
            s, e, f, _ = defects[i, 0]
            sta = tuple(large_cout[s][0])
            end = tuple(large_cout[e][0])
            far = tuple(large_cout[f][0])
            B = scfun.Eucledian_Distance(sta, far)
            C = scfun.Eucledian_Distance(end, far)
            #过滤掉角边太短的角
            if B + C > radius:
                A = scfun.Eucledian_Distance(sta, end)  #底边
                angle = acos((B**2 + C**2 - A**2) / (2 * B * C))

                if angle <= pi / 2.5:
                    ndefects += 1
    else:
        ndefects = 12
    '''
    test=scfun.Fourier_Descriptor(large_cout[:,0,:],Normalize=True)
    similar=scfun.Eucledian_Distance(test,fourier_des_ls[0])
    print('{:.5f}  {:.5f}'.format(similar,log(similar)))
    '''
    M = cv2.moments(large_cout)
    center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))  #手部的质心坐标

    x, y, w, h = cv2.boundingRect(large_cout)

    hand = cv2.cvtColor(hand, cv2.COLOR_GRAY2BGR)  #将灰度图像转换为BGR以显示绿色方框
    hand = cv2.rectangle(hand, (x, y), (x + w, y + h), (0, 255, 0), 2)

    return hand, ndefects, center
def GetCircle(img):
    #ClearBlackEdge - GaussianBlur - Binary - Erode -FindCountour
    time0_start = time.time()
    img = ClearBlackEdge(img)  #去黑边
    time0_end = time.time()
    t0 = time0_end - time0_start
    #print('black:',t0)
    time2_start = time.time()
    blur = cv2.GaussianBlur(img,(9,9),0)  #高斯平滑
    ret3, binary = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)  #双峰二值化,不过现在有一种情况有点问题,后续鲤鱼进一步改进
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (300, 300))  #定义一直贼大的矩形核
    grad = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel) #用刚才那个贼大的核,做个闭操作
    out = cv2.erode(grad, None, iterations=10) #膨胀10次
    time2_end = time.time()
    t2 = time2_end-time2_start
    #print(t2)
    #以上骚操作,都是为了把圆盘变成个实心圆

    #边缘检测得到一些外围的候选
    time1_start = time.time()
    contours, hier = cv2.findContours(out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    time1_end = time.time()
    t1 = time1_end-time1_start
    #print('edge:',t1)
    height, width = img.shape
    allCenter = []
    allRadius = []
    #这个循环是为了过滤掉一些不太外围候选,得到外围
    
    for c in contours :
        (x, y), radius = cv2.minEnclosingCircle(c)
        if width/3 < x < width/3*2 and 0.1*width < radius < 0.8*width:
            allCenter.append((int(x), int(y)))
            allRadius.append(int(radius)+100)
        else:
            pass

    

        # for i in range(len(allCenter)):
        #     img = cv2.circle(img, allCenter[i], allRadius[i], (0, 0, 255), 10)
    return img, height, width, allCenter, allRadius
#返回值是去了黑边的大图及其长宽,外围中心与半径
 def image_processing(frame):
     green_lower = (29, 86, 6)
     green_upper = (64, 255, 255)
     blurred = cv2.GaussianBlur(frame, (11, 11), 0)
     hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
     mask = cv2.inRange(hsv, green_lower, green_upper)
     mask = cv2.erode(mask, None, iterations=2)
     mask = cv2.dilate(mask, None, iterations=2)
     contour = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)
     contour = imutils.grab_contours(contour)
     if len(contour) > 0:
         c = max(contour, key=cv2.contourArea)
         ((x, y), radius) = cv2.minEnclosingCircle(c)
         m = cv2.moments(c)
         center = (int(m["m10"] / m["m00"]), int(m["m01"] / m["m00"]))
         if radius > 15:
             cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 0),
                        2)
             cv2.circle(frame, center, 5, (255, 0, 0), -1)
     return frame
Esempio n. 13
0
def get_angle_and_pos(orig_img, filtered_imgs):
    X = []
    Y = []
    for i in range(2):
        imgray = cv2.cvtColor(filtered_imgs[i], cv2.COLOR_BGR2GRAY)
        _, thresh = cv2.threshold(imgray, 255, 255, 255)
        contours, _ = cv2.findContours(thresh, cv2.RETR_TREE,
                                       cv2.CHAIN_APPROX_NONE)
        radius_lst = []
        X_lst = []
        Y_lst = []
        if len(contours) > 0:
            for ctr in contours:
                (x, y), radius = cv2.minEnclosingCircle(ctr)
                x, y = (int(x), int(y))
                radius_lst.append(int(radius))
                X_lst.append(x)
                Y_lst.append(y)

            idx = radius_lst.index(max(radius_lst))
            radius = (radius_lst[idx])
            X.append(X_lst[idx])
            Y.append(Y_lst[idx])

            cv2.circle(orig_img, (X[i], Y[i]), radius, (0, 255, 0), 2)
            cv2.circle(orig_img, (X[i], Y[i]), 4, (0, 255, 0), 3)
        else:
            return 0, 0, 0, orig_img

    if len(filtered_imgs) == 2:
        d = np.sqrt(np.power(X[0] - X[1], 2) + np.power(Y[0] - Y[1], 2))
        angle = np.arctan2(Y[0] - Y[1], X[0] - X[1])
        xs = X[0] - np.cos(angle) * d / 2
        ys = Y[0] - np.sin(angle) * d / 2
        cv2.circle(orig_img, (int(xs), int(ys)), 4, (0, 255, 0), 3)
    else:
        xs = X[0]
        ys = Y[0]
        angle = 0
    return xs, ys, angle, orig_img
Esempio n. 14
0
    def draw_circle(self, img, descirptor_in_use):
        """获取外接轮廓

        :param res: 输入图像,傅里叶描述子
        :return: 重绘图像
        """
        contour_reconstruct = np.fft.ifft(descirptor_in_use)  # 傅里叶反变换
        contour_reconstruct = np.array(
            [contour_reconstruct.real, contour_reconstruct.imag])
        contour_reconstruct = np.transpose(contour_reconstruct)  # 转换矩阵
        contour_reconstruct = np.expand_dims(contour_reconstruct,
                                             axis=1)  # 改变数组维度在axis=1轴上加1
        if contour_reconstruct.min() < 0:
            contour_reconstruct -= contour_reconstruct.min()
        contour_reconstruct *= img.shape[0] / contour_reconstruct.max()
        contour_reconstruct = contour_reconstruct.astype(np.int32, copy=False)

        x, y, w, h = cv2.boundingRect(contour_reconstruct)  # 外接矩形
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 225, 0), 3)
        rect = cv2.minAreaRect(contour_reconstruct)  # 最小外接矩形
        box = np.int0(cv2.boxPoints(rect))  # 矩形的四个角点取整
        cv2.drawContours(img, [box], 0, (0, 255, 255), 3)
        (x, y), radius = cv2.minEnclosingCircle(contour_reconstruct)  # 最小外接圆
        (x, y, radius) = np.int0((x, y, radius))  # 圆心和半径取整
        cv2.circle(img, (x, y), radius, (0, 255, 0), 2)
        ellipse = cv2.fitEllipse(contour_reconstruct)  # 拟合椭圆
        cv2.ellipse(img, ellipse, (0, 0, 255), 2)

        df = pd.DataFrame(np.random.rand(10, 4),
                          columns=[u'外接矩形', u'最小外接矩阵', u'外接圆', u'椭圆'])
        fig = df.plot(figsize=(6, 6))  # 创建图表对象,并复制给fig
        plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        plt.xlabel(u'图像轮廓', fontsize=20)

        plt.show()
        return img
Esempio n. 15
0
def readFrame():
    ret, frame = camera.read()

    if ret:
        # Process frame @ lower quality level
        frame = imutils.resize(frame, width=600)
        blurred = cv.GaussianBlur(frame, (11, 11), 0)
        hsv = cv.cvtColor(blurred, cv.COLOR_BGR2HSV)

        mask = cv.inRange(hsv, colorUpLower, colorUpUpper)
        mask = cv.erode(mask, None, iterations=2)
        mask = cv.dilate(mask, None, iterations=2)

        cv.imshow('Mask', mask)

        # Pull contours to draw
        contours = cv.findContours(
            mask.copy(), cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
        contours = imutils.grab_contours(contours)
        center = None

        if len(contours) > 0:
            maxC = max(contours, key=cv.contourArea)
            ((x, y), radius) = cv.minEnclosingCircle(maxC)
            M = cv.moments(maxC)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

            if radius > RADIUS:
                cv.circle(frame, (int(x), int(y)),
                          int(radius), (0, 255, 255), 2)
                cv.circle(frame, center, 5, (0, 0, 255), -1)
                global frame_count_up
                frame_count_up = frame_count_up + 1

        frame = cv.flip(frame, 1)
        cv.imshow('Frame', frame)
Esempio n. 16
0
    Mask = cv2.inRange(hsv, Lower_hsv, Upper_hsv)
    Mask = cv2.erode(Mask, kernel, iterations=1)
    Mask = cv2.morphologyEx(Mask, cv2.MORPH_OPEN, kernel)
    Mask = cv2.dilate(Mask, kernel, iterations=1)

    # Find contours for the pointer after idetifying it
    cnts,_ = cv2.findContours(Mask.copy(), cv2.RETR_EXTERNAL,
    	cv2.CHAIN_APPROX_SIMPLE)
    center = None

    # Ifthe contours are formed
    if len(cnts) > 0:
    	# sorting the contours to find biggest 
        cnt = sorted(cnts, key = cv2.contourArea, reverse = True)[0]
        # Get the radius of the enclosing circle around the found contour
        ((x, y), radius) = cv2.minEnclosingCircle(cnt)
        # Draw the circle around the contour
        cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
        # Calculating the center of the detected contour
        M = cv2.moments(cnt)
        center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))

        # Now checking if the user wants to click on any button above the screen 
        if center[1] <= 65:
            if 40 <= center[0] <= 140: # Clear Button
                bpoints = [deque(maxlen=512)]
                gpoints = [deque(maxlen=512)]
                rpoints = [deque(maxlen=512)]
                ypoints = [deque(maxlen=512)]

                blue_index = 0
def Identificar_objeto(c, crop_img_contour, img):
    #logica para processar os dados de cada contorno encontrado
    # id do objeto
    i = 0
    tabela = []
    for cnt in c:

        (x, y), _ = cv2.minEnclosingCircle(
            cnt)  #função que ajuda a descobrir o centro do objeto
        center = (int(x), int(y))
        cv2.putText(crop_img_contour, str(i), center, cv2.FONT_HERSHEY_SIMPLEX,
                    3, (255, 255, 255), 8,
                    cv2.LINE_AA)  #escreve o Id do objeto no centro

        box = []
        rect = cv2.minAreaRect(
            cnt)  #acha os pontos do retangulo q vai delimitar o objeto
        box_float = cv2.boxPoints(rect)
        box = np.int0(box_float)  #conversão para manipulação
        #modo como o openCV numera os vértices
        ###3###
        #2###4#
        ###1##

        cv2.drawContours(crop_img_contour, [box], 0, (225, 0, 255),
                         2)  #desenhar o retangulo

        #distancias entres os pontos pra saber qual o melhor lado pra pegar
        # distancia 0 garra anti horaria
        # distancia 1 garra horaria
        distancia_0 = math.sqrt((box[0][0] - box[1][0])**2 +
                                (box[0][1] - box[1][1])**2)
        distancia_1 = math.sqrt((box[0][0] - box[3][0])**2 +
                                (box[0][1] - box[3][1])**2)
        #delimitador de tamanho max de face
        limite = 300
        if ((distancia_0 > limite) and (distancia_1 > limite)):
            cv2.putText(crop_img_contour, "IMPOSSIVEL", center,
                        cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 255), 8,
                        cv2.LINE_AA)

        else:
            #escolher a face de menor tamanho, pq uma delas pode ser fora do limite permitido (maior que a garra)
            #lógica para converter ângulo em ãngulo_braco (a logica muda de acordo com o sentido de giro)... o braço entende de -90 a 90
            if (distancia_0 > distancia_1):

                angulo = Angulo(box[0], box[3], center, img)
                angulo_braco = math.fabs(angulo) - 90
                angulo_braco = round(angulo_braco, 2)

            else:
                angulo = Angulo(box[0], box[1], center, img)
                angulo_braco = 90 - angulo
                angulo_braco = round(angulo_braco, 2)

        #para evitar angulos grandes e o braço girar mais do que deve. Essa logica inverte a face que o braço vai pegar
            if angulo_braco > 90:
                angulo_braco = angulo_braco - 180
            if angulo_braco < -90:
                angulo_braco = angulo_braco + 180
            #converter de pixls (x e y) para dados reais (braço)
            centro_conv_X, centro_conv_Y = Conversao_centro(center)
            #correção necessário, pois o ponto de giro não é fixo... lógica desenvolvida por mapeamento e obtenção de padrão de erro
            if angulo_braco < 0:
                centro_conv_X = centro_conv_X + (0.1625 * angulo_braco)
                centro_conv_X = round(centro_conv_X, 3)

            #organizar para alimentar o dataframe
            objeto = []
            objeto.append(i)
            objeto.append(centro_conv_X)
            objeto.append(centro_conv_Y)
            objeto.append(angulo_braco)
            tabela.append(objeto)
        i += 1
    return crop_img_contour, tabela
Esempio n. 18
0
    #encontra contornos na mascara e inicializa
    #(x, y)
    contornos = cv.findContours(mascara.copy(), cv.RETR_EXTERNAL,
                                cv.CHAIN_APPROX_SIMPLE)[-2]
    centro = None

    centroC = None

    centroF = None

    #continua apenas se foi encontrado um contorno
    if len(contornos) > 0:
        #encontra maior contorno na mascara, e usa para
        #computar circundacao minima e ponto centro
        c = max(contornos, key=cv.contourArea)
        ((x, y), raio) = cv.minEnclosingCircle(c)
        M = cv.moments(c)
        centro = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        #procede apenas se raio tiver um tamanho minimo
        if raio > 10:
            #Desenha circulo e centroide no frame
            #Atualiza a lista de pontos rastreados
            cv.circle(frame, (int(x), int(y)), int(raio), (0, 255, 255), 2)

            cv.circle(frame, centro, 5, (0, 0, 255), -1)

    #atualiza fila de pontos
    pontos.appendleft(centro)

    #percorre setn de pontos e, havendo, s=desenha linha
Esempio n. 19
0
def steerPoint():
    bpoints = [deque(maxlen=1024)]
    gpoints = [deque(maxlen=1024)]
    rpoints = [deque(maxlen=1024)]
    ypoints = [deque(maxlen=1024)]
    #assigning index values
    blue_index = 0
    green_index = 0
    red_index = 0
    yellow_index = 0
    kernel = np.ones((5, 5), np.uint8)
    colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255)]
    colorIndex = 0

    #starting the painting window setup
    paintWindow = np.zeros((471, 636, 3)) + 255
    paintWindow = cv2.circle(paintWindow, (75, 45), 25, (0, 0, 0), 2)
    paintWindow = cv2.circle(paintWindow, (225, 45), 20, colors[0], -1)
    paintWindow = cv2.circle(paintWindow, (280, 45), 20, colors[1], -1)
    paintWindow = cv2.circle(paintWindow, (335, 45), 20, colors[2], -1)
    paintWindow = cv2.circle(paintWindow, (390, 45), 20, colors[3], -1)
    cv2.putText(paintWindow, "CLEAR", (55, 50), cv2.FONT_HERSHEY_DUPLEX, 0.4,
                (0, 0, 0), 1, cv2.LINE_AA)

    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        #Flipping the frame just for convenience
        frame = cv2.flip(frame, 1)
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        #HSV value for blue object
        u_hue = 153
        u_saturation = 255
        u_value = 255
        l_hue = 64
        l_saturation = 72
        l_value = 49

        Upper_hsv = np.array([u_hue, u_saturation, u_value])
        Lower_hsv = np.array([l_hue, l_saturation, l_value])

        #detect the object and remove background environment
        mask = cv2.inRange(hsv, Lower_hsv, Upper_hsv)
        mask = cv2.erode(mask, kernel, iterations=1)
        mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        mask = cv2.dilate(mask, kernel, iterations=1)
        cnts, _ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
                                   cv2.CHAIN_APPROX_SIMPLE)
        center = None
        # Ifthe contours(boundry detection of the object) are formed
        if len(cnts) > 0:
            # sorting the contours to find biggest contour
            cnt = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
            # Get the radius of the enclosing circle around the found contour
            ((x, y), radius) = cv2.minEnclosingCircle(cnt)
            # Draw the circle around the contour
            cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
            # Calculating the center of the detected contour
            M = cv2.moments(cnt)
            center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))
            #checking if any button above the screen is clicked/cursor hovered to
            if center[1] <= 80:
                if 75 <= center[0] <= 125:  # Clear Button
                    bpoints = [deque(maxlen=512)]
                    gpoints = [deque(maxlen=512)]
                    rpoints = [deque(maxlen=512)]
                    ypoints = [deque(maxlen=512)]
                    blue_index = 0
                    green_index = 0
                    red_index = 0
                    yellow_index = 0
                    paintWindow[78:, :, :] = 255
                elif 225 <= center[0] <= 265:
                    colorIndex = 0  # Blue
                elif 280 <= center[0] <= 320:
                    colorIndex = 1  # Green
                elif 335 <= center[0] <= 375:
                    colorIndex = 2  # Red
                elif 390 <= center[0] <= 430:
                    colorIndex = 3  # Yellow
            else:
                if colorIndex == 0:
                    bpoints[blue_index].appendleft(center)
                elif colorIndex == 1:
                    gpoints[green_index].appendleft(center)
                elif colorIndex == 2:
                    rpoints[red_index].appendleft(center)
                elif colorIndex == 3:
                    ypoints[yellow_index].appendleft(center)
        else:
            bpoints.append(deque(maxlen=512))
            blue_index += 1
            gpoints.append(deque(maxlen=512))
            green_index += 1
            rpoints.append(deque(maxlen=512))
            red_index += 1
            ypoints.append(deque(maxlen=512))
            yellow_index += 1
        points = [bpoints, gpoints, rpoints, ypoints]

        #draw on the whiteboard with the color selected
        for i in range(len(points)):
            for j in range(len(points[i])):
                for k in range(1, len(points[i][j])):
                    if points[i][j][k - 1] is None or points[i][j][k] is None:
                        continue
                    cv2.line(frame, points[i][j][k - 1], points[i][j][k],
                             colors[i], 2, cv2.LINE_AA)
                    cv2.line(paintWindow, points[i][j][k - 1], points[i][j][k],
                             colors[i], 2, cv2.LINE_AA)

        #convert frame into jpg format
        ret, jpeg = cv2.imencode('.jpg', paintWindow)

        #For saving whiteboard as image
        if keyboard.is_pressed('s'):
            file_name = "Trace_Image_" + time.strftime('%Y%m%d%H%M%S') + ".jpg"
            cv2.imwrite(file_name, paintWindow)

        #return frames in form of image
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n')
Esempio n. 20
0
def detectFilament(c_img):

    # contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    ret, th = cv2.threshold(c_img, 5, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

    contours_thresh, hierarchy_tresh = cv2.findContours(
        th.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    mask = np.zeros_like(c_img)
    cv2.drawContours(mask, contours_thresh, -1, 255, -1)  #black out the cell
    c_img_masked = cv2.bitwise_and(c_img, c_img, mask=mask)
    c_img_masked_not = cv2.bitwise_not(c_img_masked.copy(),
                                       c_img_masked.copy())

    ##cv2.imshow('Output', c_img) #SHOW THE RAW PICTURE
    ##cv2.waitKey(0)

    #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (4,4))
    c_img_masked_dilate = cv2.dilate(c_img_masked_not, np.ones((11, 11)), 2)
    c_img_masked_dilate_not = cv2.bitwise_not(c_img_masked_dilate.copy(),
                                              c_img_masked_dilate.copy())
    #dilate = cv2.dilate(c_img_masked, kernel, iterations=5)
    #blur_mask = cv2.GaussianBlur(c_img_masked,(5,5),0)

    edges = cv2.Canny(c_img_masked, 40, 200)
    c_img_masked = cv2.bitwise_and(edges, c_img_masked_dilate_not.copy())

    ret2, th2 = cv2.threshold(c_img_masked, 30, 255,
                              cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    contours, hierarchy = cv2.findContours(th2.copy(), cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_NONE)

    for c in contours:
        area = cv2.contourArea(c)

        # Fill very small contours with zero (erase small contours).
        if area < 10:
            cv2.fillPoly(th2, pts=[c], color=0)
            continue
    #th2 = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (10,10))); #this one actually closes the filaments individually
    th2 = cv2.morphologyEx(
        th2, cv2.MORPH_CLOSE,
        cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (20, 20)))
    ##close the filaments to get an area
    contours_isolated, hierarchy_isolated = cv2.findContours(
        th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    if (len(contours_isolated)) > 0:

        cnt = max(contours_isolated, key=cv2.contourArea)
        (x, y), radius = cv2.minEnclosingCircle(cnt)
        center = (int(x), int(y))
        radius = int(radius)
        cv2.circle(th2, center, radius, 255, 2)
        #   print("we have: ",len(contours))

        cv2.drawContours(c_img_masked, contours_isolated, -1, 255,
                         3)  #contour our actual filament
    # cv2.imshow('Output', c_img_masked)
    # cv2.waitKey(0)

    return contours_isolated  #returns the contour of the actual filament
Esempio n. 21
0
    def callback(self, data):
        lower_grey = np.array([0, 0, 46]) 
        upper_grey = np.array([180, 43, 220])
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
            print e
        # cv2.imshow("camera_raw", cv_image)
        # cv2.waitKey(3)
        
        # Guess Processing
        blurred = cv2.GaussianBlur(cv_image, (11, 11), 0)
        # cv2.imshow("blurred", blurred)

        # bgr to hsv
        hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
        
        # clear everything except the target
        mask = cv2.inRange(hsv, lower_grey, upper_grey)
        # cv2.imshow("hsv_raw", mask)

        # lvbo
        mask = cv2.erode(mask, None, iterations=2)
        mask = cv2.dilate(mask, None, iterations=2)

        # show the ball in 2 bits pic
        cv2.bitwise_not(mask, mask)
        cv2.imshow("not mask", mask)
        cv2.waitKey(3)

        cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
        self.findball = len(cnts)

        if self.findball:
            c = max(cnts, key = cv2.contourArea)
            ((x, y), radius) = cv2.minEnclosingCircle(c)
            M = cv2.moments(c)
            try:
                center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
                self.ball_center = center
                self.x_diff = self.ball_center[0] - 399
                self.y_diff = self.ball_center[1] - 500
                
                if self.y_diff > -20:
                    self.y_diff = 0
                if self.x_diff < 10 and self.x_diff > -10:
                    self.x_diff = 0
                if radius > 35:
                    radius = 0
                else :
                    radius = 1
                self.linear_speed = radius * 0.2
                self.angular_speed = - self.signal(self.x_diff) * 0.1
                # print radius
            except:
                pass


        else:
            self.linear_speed = 0
            self.angular_speed = 0.2
Esempio n. 22
0
# cv_show('gray', gray)
# 自适应阈值图
ret, res = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
# cv_show('res', res)
# 寻找轮廓
ret, contours, hie = cv.findContours(res, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# 排序轮廓
cnts = sorted(contours, key=cv.contourArea, reverse=True)[:5]
cnt = cnts[0]

# 获取矩形参数
x, y, w, h = cv.boundingRect(cnt)
# 绘制矩形
img1 = cv.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 获取最小外切圆
(x, y), radius = cv.minEnclosingCircle(cnt)
# 绘制圆形
img2 = cv.circle(img, (int(x), int(y)), int(radius), (0, 255, 0), 2)

cv_show('img', img)
cv.imwrite('result.jpg', img)
print("矩形中心点:")
print((float((x + w) / 2), float(y + h) / 2))
print("\n圆形圆心:")
print((x, y))

# 矩形四个极点
leftTop = tuple(cnt[cnt[:, :, 0].argmin()][0])
righTop = tuple(cnt[cnt[:, :, 0].argmax()][0])
upTop = tuple(cnt[cnt[:, :, 1].argmin()][0])
downTop = tuple(cnt[cnt[:, :, 1].argmax()][0])
Esempio n. 23
0
cv2.contourArea()
The function computes a contour area. Similarly to moments , the area is computed using the Green formula. 
Thus, the returned area and the number of non-zero pixels. 
Also, the function will most certainly give a wrong results for contours with self-intersections.
'''

print('Периметр контура внешней окружности: ' +
      str(cv2.arcLength(outside, True)))
print('Площадь контура внешней окружности: ' + str(cv2.contourArea(outside)))
# Получим координаты внешнего ограничивающего прямоугольника:
x, y, w, h = cv2.boundingRect(outside)
# Нарисуем внешний огр.прямоугольник:
cv2.rectangle(circle, (x, y), (x + w, y + h), (0, 255, 0), 2)
print('Площадь внешнего ограничивающего прямоугольника: ' + str(w * h))
# Получим координаты внешней описанной окружности:
(x, y), radius = cv2.minEnclosingCircle(outside)
center = (int(x), int(y))
radius = int(radius)
# Нарисуем внешнюю ограничивающую окружность:
cv2.circle(circle, center, radius, (0, 0, 255), thickness=2)
print('Площадь внешней ограничивающей окружности: ' + str(pi * (radius**2)) +
      '\n')

print('Периметр контура внутренней окружности: ' +
      str(cv2.arcLength(inside, True)))
print('Площадь контура внутренней окружности: ' + str(cv2.contourArea(inside)))
# Получим координаты внутреннего ограничивающего прямоугольника:
x, y, w, h = cv2.boundingRect(inside)
# Нарисуем внутренний огр.прямоугольник:
cv2.rectangle(circle, (x, y), (x + w, y + h), (133, 21, 199), 2)
print('Площадь внутреннего ограничивающего прямоугольника: ' + str(w * h))
Esempio n. 24
0
            cv.CHAIN_APPROX_SIMPLE)[-2]
        center = None
        
        if key == 'magenta':
            pag.click()
        
        elif key == 'anil':
            break
        
        # only proceed if at least one contour was found
        if len(cnts) > 0:
            # find the largest contour in the mask, then use
            # it to compute the minimum enclosing circle and
            # centroid
            c = max(cnts, key=cv.contourArea)
            ((x, y), radius) = cv.minEnclosingCircle(c)
            M = cv.moments(c)
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
        
            # only proceed if the radius meets a minimum size. Correct this value for your obect's size
            if radius > 0.5:
                # draw the circle and centroid on the frame,
                # then update the list of tracked points
                cv.circle(frame, (int(x), int(y)), int(radius), colors[key], 2)
                pag.moveTo(x = (center[0] - 10), y=(center[1] - 10))
                 
                #cv.putText(frame,key + " ball", (int(x-radius),int(y-radius)), cv2.FONT_HERSHEY_SIMPLEX, 0.6,colors[key],2)  


    
    #Mostra imagem na linha
Esempio n. 25
0
def main():
    cap = cv2.VideoCapture(0)

    # define range of blue color in HSV
    lower_blue = np.array([110,50,50])
    upper_blue = np.array([130,255,255])

    pts = deque(maxlen=512)
    blackboard = np.zeros((480, 640, 3), dtype=np.uint8)
    digit = np.zeros((200, 200, 3), dtype=np.uint8)
    results = ""
    while cap.isOpened():
        ret, img = cap.read()
        img = cv2.flip(img, 1)
        hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
        kernel = np.ones((5, 5), np.uint8)
        mask = cv2.inRange(hsv, lower_blue, upper_blue)
        mask = cv2.erode(mask, kernel, iterations=2)
        mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
        mask = cv2.dilate(mask, kernel, iterations=1)
        res = cv2.bitwise_and(img, img, mask=mask)
        cnts, heir = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
        center = None

        if len(cnts) >= 1:
            cnt = max(cnts, key=cv2.contourArea)
            if cv2.contourArea(cnt) > 200:
                ((x, y), radius) = cv2.minEnclosingCircle(cnt)
                cv2.circle(img, (int(x), int(y)), int(radius), (0, 255, 255), 2)
                cv2.circle(img, center, 5, (0, 0, 255), -1)
                M = cv2.moments(cnt)
                center = (int(M['m10'] / M['m00']), int(M['m01'] / M['m00']))
                pts.appendleft(center)
                for i in range(1, len(pts)):
                    if pts[i - 1] is None or pts[i] is None:
                        continue
                    cv2.line(blackboard, pts[i - 1], pts[i], (255, 255, 255), 7)
                    cv2.line(img, pts[i - 1], pts[i], (0, 0, 255), 2)
        elif len(cnts) == 0:
            if len(pts) != []:
                blackboard_gray = cv2.cvtColor(blackboard, cv2.COLOR_BGR2GRAY)
                blur1 = cv2.medianBlur(blackboard_gray, 15)
                blur1 = cv2.GaussianBlur(blur1, (5, 5), 0)
                thresh1 = cv2.threshold(blur1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
                blackboard_cnts = cv2.findContours(thresh1.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
                if len(blackboard_cnts) >= 1:
                    cnt = max(blackboard_cnts, key=cv2.contourArea)
                    print(cv2.contourArea(cnt))
                    if cv2.contourArea(cnt) > 1000:
                        x, y, w, h = cv2.boundingRect(cnt)
                        digit = blackboard_gray[y:y + h, x:x + w]
                        image_processed = process_image(digit)             
                        image_deep = model.predict(np.reshape(image_processed, (-1, 28, 28, 1)))
                        results = np.argmax(image_deep, axis = 1)
                        print(results)
            cv2.putText(img, "The Number Is : " + str(results), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255 ,0, 0), 2)
            pts = deque(maxlen=512)
            blackboard = np.zeros((480, 640, 3), dtype=np.uint8)
        cv2.imshow("Main Frame", img)
        k = cv2.waitKey(10)
        if k == 27:
            break
Esempio n. 26
0
def detect_squares(frame):
    global crop_image, mask, dilate_kernel, squares

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    black = gray.copy()
    black[:] = 0
    test = black.copy()
    # gray = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,1)
    edges = cv2.Canny(gray, 50, 220, apertureSize=3)
    edges = cv2.dilate(edges, dilate_kernel, iterations=1)

    squares = []

    centroidSquare = Square()

    contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)

    try:
        hierarchy = hierarchy[0]
    except:
        pass

    for ind in range(len(contours)):
        cnt = contours[ind]

        area = cv2.contourArea(cnt)
        perimeter = cv2.arcLength(cnt, False)

        approx = cv2.approxPolyDP(cnt, 0.1 * perimeter, True)

        if area > 150 and area < 3300 and perimeter < 200:
            if len(approx) == 4:
                # # Skip if this contour has a parent
                # if hierarchy[ind][3] == -1:
                #     cv2.drawContours(
                #         frame, [approx], -1, (255, 255, 0), thickness=-1)
                #     continue
                cv2.drawContours(test, [approx], -1, (255), thickness=-1)
                ((x, y), radius) = cv2.minEnclosingCircle(cnt)
                # # OR
                # M = cv2.moments(cnt)
                # x = int(M['m10']/M['m00'])
                # y = int(M['m01']/M['m00'])
                # radius = 20

                if (radius <= 0):
                    radius = 1

                squr = Square()
                squr.x = int(x)
                squr.y = int(y)
                squr.area = area
                squr.contour = cnt
                squr.approx = approx
                squr.radius = int(radius)
                squr.perimeter = perimeter

                squares.append(squr)

                # try:
                #     clr = frame[squr.y, squr.x]
                # # Skip squares whose color we cant get (mostly center going out of the frame)
                # except:
                #     continue
                # squr.color_name, squr.color = getNearestColor(
                #     (int(clr[2]), int(clr[1]), int(clr[0])))

                # center = (squr.x, squr.y)

                # # cv2.drawContours(black, [approx], -1, (255), thickness=-1)
                # cv2.circle(frame, center, int(radius), squr.color, -1)

                # cv2.putText(frame, squr.color_name,
                #             center,
                #             cv2.FONT_HERSHEY_SIMPLEX,
                #             1,
                #             (255, 255, 0))

                # Doing average for radius, area and x,y
                centroidSquare.x += x
                centroidSquare.y += y
                centroidSquare.area += area
                centroidSquare.radius += radius

    length = len(squares)
    if length > 0:  # calc centeroid if squares are detected
        centroidSquare.x = int(centroidSquare.x / length)
        centroidSquare.y = int(centroidSquare.y / length)
        # This should not work as its not linear
        centroidSquare.area = int(centroidSquare.area / length)
        centroidSquare.radius = int(centroidSquare.radius / length)

    squares = clean_squares(black, frame, centroidSquare, squares)

    opticalFlow(black, frame, squares)

    # Draw optical flow
    if mask is not None:
        frame = cv2.add(frame, mask)

    drawSquares(frame, squares)
    # Draw centroid
    #cv2.circle(frame, (centroidSquare.x, centroidSquare.y),
    #          centroidSquare.radius, (255, 255, 255), 1)

    # print(len(contours))
    cv2.imshow('black', black)
    cv2.imshow('Out', frame)
    cv2.imshow('test', test)
Esempio n. 27
0
def find_candidate_targets(frame):
    global queue_frames

    _frame = frame
    # Blur the frame to remove noise
    if _is_blur:
        _frame = cv2.medianBlur(_frame, _blur_kernalsize)

    # Convert frame from RGB color space to HSV color base
    frame_hsv = cv2.cvtColor(_frame, cv2.COLOR_BGR2HSV)

    # Define the boundary of target color in HSV color space
    # Infrared light appear white-ish in frame
    mask = cv2.inRange(frame_hsv, lower_boundary, upper_boundary)

    # Eroded the frmae
    if _is_erode:
        mask = cv2.erode(mask, None, iterations=_erode_iterations)

    # Dilate the frame
    if _is_dilate:
        mask = cv2.dilate(mask, None, iterations=_dilate_iterations)

    put_frame(mask)
    feedback_frame = get_feedback_frame()

    # Find contours in the mask
    # contours: 輪廓
    # cv2.RETR_EXTERNAL: only check the contours
    # cv2.CHAIN_APPROX_SIMPLE: only keep the coordinate value (x, y), ignore directional data
    contours = cv2.findContours(feedback_frame, cv2.RETR_EXTERNAL,
                                cv2.CHAIN_APPROX_SIMPLE)[-2]

    # Convert the mask from gray-scale back to bgr for better support of showing in GUI
    feedback_frame = cv2.cvtColor(feedback_frame, cv2.COLOR_GRAY2BGR)

    # If there exists contour
    if len(contours) > 0:
        targets = []

        # create Target object for each contours
        for contour in contours:
            # Form the minimum circle contain the above largest contour
            # Get x, y, and radius of largest contour
            ((x, y), radius) = cv2.minEnclosingCircle(contour)

            # only proceed if the radius meets a minimum size
            if radius > min_radius:

                # Calculate the centroid of largest contour and find its center
                m = cv2.moments(contour)

                m00 = m["m00"]
                # if m00 is 0, which will result in division by zero error later, just use original x and y will be good enough
                # e.g. if the contour shape is a balanced butterfly shape, m00 will highly likely be zero
                if m00 == 0:
                    centroid_x = int(x)
                    centroid_y = int(y)
                else:
                    centroid_x = int(m["m10"] / m00)
                    centroid_y = int(m["m01"] / m00)

                target = Target(x=centroid_x, y=centroid_y, radius=radius)
                targets.append(target)

        if len(targets) > 0:
            return {
                "result": True,
                "targets": targets,
                "pro_processing_frame": feedback_frame
            }

    return {"result": False, "pro_processing_frame": feedback_frame}
def overlay_images(overview_dl_fh, overview_ef_fh, zoom_fh, output_fh, circle=False, box=True, convex=False,
                   debug=False):
    """
    Overlay drop image in convex, circle or box shape on overview image from Rockimager
    @param overview_dl_fh: Overview drop location file path
    @param overview_ef_fh: Overview file path
    @param zoom_fh: Drop file path
    @param output_fh: Overlay output file path
    @param circle: bool, Shape circle cut out
    @param box: bool, Shape box overlay
    @param convex: bool, Shape convex cut out
    @param debug: Show images during processing
    @return: overlayed image
    """
    # This is the main function of the script

    overview_dl = cv2.imread(overview_dl_fh)
    zoom = cv2.imread(zoom_fh)
    overview_ef = cv2.imread(overview_ef_fh)

    if debug:
        cv2.imshow("dl, ef, zoom Press Any Key to Close", np.concatenate([overview_dl, overview_ef, zoom]))
        cv2.imshow("dl, ef, zoom Press Any Key to Close", np.concatenate([overview_dl, overview_ef, zoom]))
        cv2.waitKey(0)

    dark_red = np.array([0, 2, 57])
    light_red = np.array([69, 92, 255])
    b_x, b_y, b_w, b_h, img_is_normal_sized = get_drop_location_box(overview_dl, dark_red, light_red, debug=debug)

    if debug:
        print("b_x, b_y, b_w, b_h, img_is_normal_sized", b_x, b_y, b_w, b_h, img_is_normal_sized)

    if img_is_normal_sized:
        if circle or convex:
            # convert drop image to grey image
            zoom_grey = cv2.cvtColor(zoom, cv2.COLOR_RGB2GRAY)

            # invert image make dark pixels brighter (edge of drop)
            _, zoom_grey = cv2.threshold(zoom_grey, 175, 255, cv2.THRESH_BINARY_INV)

            # blur the image (fill gaps, reduce noise)
            zoom_blur_grey = cv2.GaussianBlur(zoom_grey, (5, 5), 0)

            # (make mask) high contrast dark black and bright white color
            _, zoom_sharp_grey = cv2.threshold(zoom_blur_grey, 0, 255, cv2.THRESH_BINARY_INV)

            # find edges of mask
            edges = cv2.Canny(zoom_sharp_grey, 0, 255)

            # find contour points of mask
            cnts, hierarchy, _, _, _ = find_image_features(edges, mask_color=False, percent_arc_length=0.01,
                                                           bilateral=False,
                                                           contour_method=cv2.CHAIN_APPROX_NONE,
                                                           retreival_method=cv2.RETR_TREE)

            # create blank image for masking drop image
            black_white_mask = np.zeros((zoom_grey.shape[0], zoom_grey.shape[1]), np.uint8)

            if circle:
                cnt = find_biggest_contour(image=zoom, contours=cnts, max_area=zoom.shape[0] * zoom.shape[1])
                (circle_x, circle_y), radius = cv2.minEnclosingCircle(cnt)
                (circle_x, circle_y, radius) = (int(circle_x), int(circle_y), int(radius))
                circle_mask = np.zeros((zoom_grey.shape[0], zoom_grey.shape[1]), np.uint8)
                cv2.circle(circle_mask, (circle_x, circle_y), radius, COLOR_WHITE, -1)
                overview_ef = align_drop_to_overview(b_x, b_y, b_w, b_h, zoom, overview_ef, circle_mask, debug=debug)

            elif convex:
                # make convex shapes that fit biggest contour point set
                hull = []
                for i in range(len(cnts)):
                    hull.append(cv2.convexHull(cnts[i], False))

                # draw them on a mask
                for i in range(len(hull)):
                    cv2.drawContours(black_white_mask, hull, i, COLOR_WHITE, -1, 8)

                # find contours of that mask (adds some smoothing)
                cnts_mask, hierarchy_mask, _, _, _ = find_image_features(black_white_mask, mask_color=False,
                                                                         percent_arc_length=3,
                                                                         retreival_method=cv2.RETR_TREE,
                                                                         bilateral=False, blur_image=True,
                                                                         blur_iterations=30)

                # create final convex shape mask
                black_white_mask_2 = np.zeros((zoom_grey.shape[0], zoom_grey.shape[1]), np.uint8)

                # make convex shapes that fit biggest contour point set
                hull = []
                for i in range(len(cnts_mask)):
                    hull.append(cv2.convexHull(cnts_mask[i], False))

                # draw them on a mask
                for i in range(len(hull)):
                    cv2.drawContours(black_white_mask_2, hull, i, COLOR_WHITE, -1, 8)

                overview_ef = align_drop_to_overview(b_x, b_y, b_w, b_h, zoom, overview_ef, black_white_mask_2,
                                                     debug=debug)
        elif box:
            overview_ef = align_drop_to_overview(b_x, b_y, b_w, b_h, zoom, overview_ef, debug=debug)
    elif box:
        overview_ef = align_drop_to_overview(b_x, b_y, b_w, b_h, zoom, overview_ef, debug=debug)
    else:
        overview_ef = overview_ef

    cv2.imwrite(output_fh, overview_ef)
    return overview_ef
Esempio n. 29
0
    contornosF = cv.findContours(mascaraFecha.copy(), cv.RETR_TREE,
                                 cv.CHAIN_APPROX_SIMPLE)

    #encontra contornos na mascara e inicializa
    #(x, y)
    contornos = cv.findContours(mascara.copy(), cv.RETR_EXTERNAL,
                                cv.CHAIN_APPROX_SIMPLE)[-2]
    centro = None

    #continua apenas se foi encontrado um contorno
    if len(contornos) > 0:
        #encontra maior contorno na mascara, e usa para
        #computar circundacao minima e ponto centro
        c = max(contornos, key=cv.contourArea)
        ((x, y), raio) = cv.minEnclosingCircle(c)
        M = cv.moments(c)
        centro = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        #procede apenas se raio tiver um tamanho minimo
        if raio > 10:
            #Desenha circulo e centroide no frame
            #Atualiza a lista de pontos rastreados
            cv.circle(frame, (int(x), int(y)), int(raio), (0, 255, 255), 2)

            cv.circle(frame, centro, 5, (0, 0, 255), -1)

    #atualiza fila de pontos
    pontos.appendleft(centro)

    #percorre setn de pontos e, havendo, s=desenha linha
def gameControllerFunction(topLeft, centerLeft, bottomLeft, topCenter, middle, bottomCenter, topRight, centerRight, bottomRight):

    print("Booting the Video stream,")
    vs = cv2.VideoCapture(0)  # start the video stream.
    time.sleep(2.0)  # set sleep time to 2.0 seconds

    while True:
        frame = vs.read()  # Read off the frame from the video stream
        ret, frame = frame  # Use this if you want to load in your video
        output = "None"
        key = None

        if frame is None:  # If there is no frame, save my pc from going through any stress at all
            break
        # otherwise, if we have a frame, we proceed with the following code
        # so much easier than open cv, keeping aspect ratio intact
        frame = imutils.resize(frame, width=700)
        # i want the mirror view, it's very helpful especially if i'm streaming
        frame = cv2.flip(frame, 1)

        windowDetails = cv2.getWindowImageRect('frame')
        # print(windowDetails)
        totalWidth = windowDetails[2]
        totalHeight = windowDetails[3]
        verLine1 = {
            'start': (totalWidth//3, 0),
            'end': (totalWidth//3, totalHeight)
        }
        verLine2 = {
            'start': (totalWidth//3 * 2, 0),
            'end': (totalWidth//3 * 2, totalHeight)
        }
        horLine1 = {
            'start': (0, totalHeight//3),
            'end': (totalWidth, totalHeight//3)
        }
        horLine2 = {
            'start': (0, totalHeight//3 * 2),
            'end': (totalWidth, totalHeight//3 * 2)
        }

        # processing the frame
        # blurr helps to reduce high frequency noise, definately helps model
        blurred = cv2.GaussianBlur(frame, (11, 11), 0)
        # convert my color to the HSV format
        hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)

        # Create a mask
        # mask other regions except colors in range of upper to lower (thresholding)
        mask = cv2.inRange(hsv, lower_color_boundary, upper_color_boundary)
        # Reduce noise caused by thresholding
        mask = cv2.erode(mask, None, iterations=2)
        # foreground the found object i.e futher reduce noise.
        mask = cv2.dilate(mask, None, iterations=2)

        contours = cv2.findContours(
            mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # find contours
        # Grab the contours using imutils
        contours = imutils.grab_contours(contours)
        center = None  # center is initially set to none
        if len(contours) > 0:  # if the contours list is not empty proceed
            # select contour with maximum Area, most likely our object
            contour = max(contours, key=cv2.contourArea)
            # pick up co-ordinates for drawing a circle around the object
            ((x, y), radius) = cv2.minEnclosingCircle(contour)
            M = cv2.moments(contour)  # Extract moments from the contour.
            # Obtain the centre of mass of the object.
            center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
            if radius > 10:  # if we have a reasonable radius for the proposed object detected
                # Draw a circle to bound the Object
                cv2.circle(frame, (int(x), int(y)),
                           int(radius), (0, 255, 255), 2)
                # Draw a filled in dot at the centre of the circle
                cv2.circle(frame, center, 5, (0, 0, 225), -1)

        if center:
            if center[0] <= totalWidth//3:
                if center[1] <= totalHeight//3:
                    output = "Top Left"
                    key = topLeft
                elif center[1] >= totalHeight//3*2:
                    output = "Bottom Left"
                    key = bottomLeft
                else:
                    output = "Center Left"
                    key = centerLeft
            elif center[0] >= totalWidth//3*2:
                if center[1] <= totalHeight//3:
                    output = "Top Right"
                    key = topRight
                elif center[1] >= totalHeight//3*2:
                    output = "Bottom Right"
                    key = bottomRight
                else:
                    output = "Center Right"
                    key = centerRight
            else:
                if center[1] <= totalHeight//3:
                    output = "Top Center"
                    key = topCenter
                elif center[1] >= totalHeight//3*2:
                    output = "Bottom Center"
                    key = bottomCenter
                else:
                    output = "Center"
                    key = middle

        if key:
            key_arr = key.split('+')

            # Key Presses
            for k in key_arr:
                pyautogui.keyDown(k.strip())

            time.sleep(0.08)

            for k in key_arr:
                pyautogui.keyUp(k.strip())

        # Drawing the grid
        cv2.putText(frame,  output,  (50, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 1,  (0, 0, 0),  2,  cv2.LINE_4)

        cv2.line(frame, verLine1['start'], verLine1['end'], (255, 255, 255), 5)
        cv2.line(frame, verLine2['start'], verLine2['end'], (255, 255, 255), 5)
        cv2.line(frame, horLine1['start'], horLine1['end'], (255, 255, 255), 5)
        cv2.line(frame, horLine2['start'], horLine2['end'], (255, 255, 255), 5)
        cv2.imshow("frame", frame)  # let's see the frame X frame

        # Closing a video frame
        key = cv2.waitKey(1)  # wait for the cv key
        if key == ord("q"):  # If the x button is pressed
            break  # Break from the loop

    vs.release()  # Let opencv release the video loader
    cv2.destroyAllWindows()  # Destroy all windows to close it