Object Detection in front of a Similar Colored Background

asked 2018-05-15 09:37:10 -0500

KaanCidar gravatar image

updated 2018-12-10 09:27:53 -0500

I'm trying to detect board in the image but the background color is so close and the corners of the board is circular like so i cant detect it with rectangle detection. Here is the code I used for this case. Would appreciate any help. Thanks image description


def angle(pt1,pt2,pt0):

dx1 = pt1[0][0] - pt0[0][0]

dy1 = pt1[0][1] - pt0[0][1]
dx2 = pt2[0][0] - pt0[0][0]
dy2 = pt2[0][1] - pt0[0][1]
return float((dx1*dx2 + dy1*dy2))/math.sqrt(float((dx1*dx1 + dy1*dy1))*(dx2*dx2 + dy2*dy2) + 1e-10)

while(cap.isOpened()): #Capture frame-by-frame ret, frame = cap.read() if ret==True: #grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Canny canny = cv2.Canny(frame,15,60) #contours canny2, contours, hierarchy = cv2.findContours(canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cv2.imshow('Contours', canny2) for i in range(0,len(contours)): #approximate the contour with accuracy proportional to #the contour perimeter approx = cv2.approxPolyDP(contours[i], cv2.arcLength(contours[i], True)*0.02, True)

        #Skip small or non-convex objects
        if(abs(cv2.contourArea(contours[i]))<200 or not(cv2.isContourConvex(approx))):

        elif(len(approx)>=4 and len(approx)<=6):
            #nb vertices of a polygonal curve
            vtc = len(approx)
            #get cos of all corners
            cos = []
            for j in range(2,vtc+1):
            #sort ascending cos
            #get lowest and highest
            mincos = cos[0]
            maxcos = cos[-1]

            #Use the degrees obtained above and the number of vertices
            #to determine the shape of the contour
            x,y,w,h = cv2.boundingRect(contours[i])
                cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

    #Display the resulting frame
    cv2.imwrite('/home/kaan/Downloads/Board.jpg', frame)
    cv2.imwrite('/home/kaan/Downloads/Edge.jpg', canny)
    if cv2.waitKey(1) == 1048689: #if q is pressed

When everything done, release the capture

cap.release() cv2.destroyAllWindows()

edit retag flag offensive close merge delete