Ask Your Question
0

Can I use real-time detection only inside drawen polygon on the frames

asked 2018-11-18 10:37:23 -0600

Sibobb gravatar image

updated 2018-11-18 11:12:14 -0600

berak gravatar image

I just want to draw a polygon by points. And than use detect algorithms only inside this polygon area. Anyone know about that? (I dont want to use only rectangle-areas)

import numpy as np
import cv2

def mouse_drawing(event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("Left click")
        circles.append((x,y))

cap = cv2.VideoCapture(0)

circles = []

cv2.namedWindow("Frame")
if len(circles) < 4:
    cv2.setMouseCallback("Frame", mouse_drawing)


while True:
    _, frame = cap.read()

    for center_position in circles:
        cv2.circle(frame, center_position, 2, (0, 0, 255), -1)
    points = np.array(circles)
    if len(circles) >= 4:
        cv2.polylines(frame, np.int32([points]), 1, (255, 255, 255))

    cv2.imshow("Frame", frame)

    key = cv2.waitKey(1)
    if key == 27:

        break
    elif key == ord("d"):
        circles = []

cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

i don't think, this is possible, as all the algorithms in the library expect a rectangular region to work on.

(you can ofc. warp your polygon to a rectangle, but that will distort the content inside)

berak gravatar imageberak ( 2018-11-19 01:26:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-11-19 08:22:28 -0600

supra56 gravatar image

Actually, I used raspberry pi 3b+. Unfortunately, I didn't used videocapture(0). So you can modified yourself to suit your needed.

import cv2
import numpy as np

left_click = np.empty((0,2), np.uint32)
left_click_bool = False
right_click = np.empty((0,2), np.uint32)

lpnts = np.empty((0,2), np.uint32)
rpnts = np.empty((0,2), np.uint32)

# mouse callback function
def get_points(event,x,y,flags,param):
    global lpnts,rpnts
    if event == cv2.EVENT_LBUTTONDOWN:
        lpnts = np.append(lpnts, np.array([[x, y]]), axis=0)
        cv2.polylines(img, [lpnts], False, (0, 0, 255))

    if event == cv2.EVENT_RBUTTONDOWN:
        rpnts = np.append(rpnts, np.array([[x, y]]), axis=0)
        cv2.polylines(img, [rpnts], False, (255, 0, 0))

img = np.zeros((512,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image', get_points)

while True:
    cv2.imshow('image', img)
    k = cv2.waitKey(1) 
    if k == ord('m'):
        mode = not mode
    elif k == 27:
        break
edit flag offensive delete link more

Comments

@supra56 -- nice, but where is the actual improvement over the original code ?

berak gravatar imageberak ( 2018-11-19 09:16:19 -0600 )edit

no i mean:

(I dont want to use only rectangle-areas)

berak gravatar imageberak ( 2018-11-19 09:48:41 -0600 )edit

@berak. You can't do on fly.

supra56 gravatar imagesupra56 ( 2018-11-19 09:53:37 -0600 )edit
1

Here is link: polygons

supra56 gravatar imagesupra56 ( 2018-11-19 10:05:55 -0600 )edit
1

hmm, maybe i misread all of this, but creating polygons was never the problem, more like analysing the pixels inside ? (non-rectangle areas, again)

and, @supra56 -- don't get it wrong, your input is always welcome here ;)

(i'm only trying to push you (a little) to find a better fit to the original question :)

berak gravatar imageberak ( 2018-11-19 10:10:20 -0600 )edit

@berak. Look at link georgraphy map. This is what is look like.

supra56 gravatar imagesupra56 ( 2018-11-19 10:20:59 -0600 )edit

isn't that "clustering" only ? how is it related to the original problem ?

berak gravatar imageberak ( 2018-11-19 10:22:57 -0600 )edit

@berak. Hopefully you understood.. I may take a break and do some robotic project.

supra56 gravatar imagesupra56 ( 2018-11-19 10:27:21 -0600 )edit

isn't that "clustering" only ? how is it related to the original problem ? I can't help it.

supra56 gravatar imagesupra56 ( 2018-11-19 10:30:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-11-18 10:37:23 -0600

Seen: 726 times

Last updated: Nov 19 '18