'openCV camera boundaries are deemed as object, objects only detected when they cross path

I am trying to detect objects with my camera. However only the outer border becomes green and objects are detected once they cut the outer border.

https://imgur.com/a/Mj2Zexp

You can see how it detects images when it cuts through the outlines, but it does not detect the rectangle.

as you can tell only the borders are green and once an object cuts the border it becomes an object. If I were to place something else in the middle of the screen which doesn't touch the outline borders it is not considered an object.

My code:

import math
import numpy as np
import cv2 as cv
from threading import *

class robotVision(Thread):

    def run(self):
        self.cap = cv.VideoCapture(0)
        if not self.cap.isOpened():
            print("Cannot open camera")
            exit()
        while True:
            # Capture frame-by-frame
            self.ret, self.frame = self.cap.read()

            if not self.ret:
                print("Can't receive frame (stream end?). Exiting ...")
                break

            self.detectCookie()

            self.imshow()

            if cv.waitKey(1) == ord('q'):
                self.releaseStream()
                break

    def detectCookie(self):
        # turn scene gray and put a threshold on noise
        self.gray = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
        self.blur = cv.GaussianBlur(self.gray,(39, 39), 0)
        _, self.thresh = cv.threshold(self.blur, 75, 255, 0, cv.THRESH_BINARY)
        self.dilated = cv.dilate(self.thresh, (7, 7), iterations = 3)
        self.findContours()

        for cnt in self.contours:
            area = cv.contourArea(cnt)
            perimeter = cv.arcLength(cnt, True)

            if (perimeter != 0 and area != 0):
                #the rounding -> how round it is
                formFactor = 4 * math.pi * area / perimeter**2

            if (area > 100):
                self.drawContours(cnt)

    def releaseStream(self):
        # When everything done, release the capture
        self.cap.release()
        cv.destroyAllWindows()

    def imshow(self):
        cv.imshow("Gray capture (First cycle)", self.gray)
        cv.imshow("Blur capture (Second cycle)", self.blur)
        cv.imshow("Thresh capture (Third cycle)", self.thresh)
        cv.imshow("Dilated capture (Fourth cycle)", self.dilated)
        cv.imshow("Video capture (Final result)", self.frame)

    def findContours(self):
        self.contours, self.hierarchy = cv.findContours(self.dilated, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)

    def drawContours(self, target = "default"):
        if (target == "default"):
            cv.drawContours(self.frame, self.contours, -1, (0,255,0), 3)
        elif (isinstance(target, list)):
            cv.drawContours(self.frame, target, -1, (0,255,0), 3)
        else:
            cv.drawContours(self.frame, [target], -1, (0,255,0), 3)

My question:

How can I remove the outer lines and detect objects without having the objects have to cut the outer lines to be considered an object.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source