Avatar ·

Search for a rectangle in the image

📁 python

Task : Find a rigid disk in the photo, determine its angle and contours

n

Problem: It is not always possible to find the correct disk contour.

n

in the code, I make images gray, Blue, I find the difference between them and determine the contours, but the solution is not accurate. I would like your help.

n
  Firstframe = CV2.imread (bg_im) # Background image  nframe_img = CV2.imread (Frame) # image with a disc  ngray = cv2.cvtcolor (frame_img, cv2.color_bgr2gray) ngray = cv2.gaussianblur (gran Class = ""> 21 ,  21 ),  0 ) nfirstframe = cv2.cvtcolor (firstframe, cv2.color_bgr2grastframe = cv2.gaussianblur (firstframe, ( 21 ,  21 ),  0 ) nframedelta = cv2.absdif (feirstframe, gray) nthresh = cv2.threshold (framedelta,  25 ,  255 , cv2.thresh_binary) [ 1 ] nthresh = nthresh = " cv2.dilate (Thresh,  none , itrations =  2 ) n (cnts, _) = cv2.findContours (Thresh.copy (), CV2.retr_EXTEAL, CV2.Chain_Approx_simple) nc =  sorted  (cNTS, key = cv2.contourarea, reverse =  true ) [ 0 ] nrect = cv2.minarearect (c) nbox = cv2.cv.boxpoints (rect) nx1 = block = "" "> 0 ] [ 0 ] nx2 = Box [ 1 ] [ 0 ] nx4 = box [ 3 ] [ 0 ] ny1 = ny1 = ny1 = ny1 = ny1 = Box [] [] ny2 = box [ 1 ] [ 1 ] ny4 = box [ 3 ] [ 1 ] nperimeter_1_4 = sqrt ((x4 - x1) **  2  + (y1 - y4) **  2 ) nperimeter_1_2 = sqrt ((x2 - x1) **  2  + (y1 - y2) **  2 ) nbox = np.int0 (box) nconer =  list  (rect) [ 2 ] n  if> perimeter_1_4 & gt; perimeter_1_2: n coer = - ( 90  - coer) ncv2.drawContours (frame_img, [box],  0 , ( 0 ,  0 ,  255 ),  2 ) n