parameters: (image, tile)
return_statement: return im_rotate[y_min:y_max, x_min:x_max, :], horiz
docstring: Crop the image giving a tile.
      Args:
          image: Image to be crop, [h, w, c].
          tile: [p_0, p_1, p_2, p_3] (clockwise).
  
      Returns:
          cropped: Patch corresponding to the tile.
  
      Raises:
          ZeroDivisionError: x[1] == x[0] or x[2] == x[3].
docstring_summary: Crop the image giving a tile.
      Args:
          image: Image to be crop, [h, w, c].
          tile: [p_0, p_1, p_2, p_3] (clockwise).
docstring_tokens: Crop the image giving a tile . Args : image : Image to be crop [ h w c ] . tile : [ p_0 p_1 p_2 p_3 ] ( clockwise ) .
function: def general_crop(image, tile):
      """Crop the image giving a tile.
      Args:
          image: Image to be crop, [h, w, c].
          tile: [p_0, p_1, p_2, p_3] (clockwise).
  
      Returns:
          cropped: Patch corresponding to the tile.
  
      Raises:
          ZeroDivisionError: x[1] == x[0] or x[2] == x[3].
      """
      x = [p[0] for p in tile]
      y = [p[1] for p in tile]
      # phase1:shift the center of patch to image center
      x_center = int(round(sum(x) / 4))
      y_center = int(round(sum(y) / 4))
      im_center = [int(round(coord / 2)) for coord in image.shape[:2]]
      shift = [im_center[0] - y_center, im_center[1] - x_center]
      M = np.float32([[1, 0, shift[1]], [0, 1, shift[0]]])
      height, width = image.shape[:2]
      im_shift = cv2.warpAffine(image, M, (width, height))
  
      # phase2:imrote the im_shift to regular the box
      bb_width = (math.sqrt((y[1] - y[0]) ** 2 + (x[1] - x[0]) ** 2) +
                  math.sqrt((y[3] - y[2]) ** 2 + (x[3] - x[2]) ** 2)) / 2
      bb_height = (math.sqrt((y[3] - y[0]) ** 2 + (x[3] - x[0]) ** 2) +
                   math.sqrt((y[2] - y[1]) ** 2 + (x[2] - x[1]) ** 2)) / 2
      horiz = True
      if bb_width > bb_height:  # main direction is horizental
          tan = ((y[1] - y[0]) / float(x[1] - x[0] + 1e-8) +
                 (y[2] - y[3]) / float(x[2] - x[3] + 1e-8)) / 2
          degree = math.atan(tan) / math.pi * 180
      else:  # main direction is vertical
          tan = ((y[1] - y[2]) / float(x[1] - x[2] + 1e-8) +
                 (y[0] - y[3]) / float(x[0] - x[3] + 1e-8)) / 2
          degree = math.atan(tan) / math.pi * 180 - np.sign(tan) * 90
          horiz = False
      rotation_matrix = cv2.getRotationMatrix2D(
          (width / 2, height / 2), degree, 1)
      cos = np.abs(rotation_matrix[0, 0])
      sin = np.abs(rotation_matrix[0, 1])
      # compute the new bounding dimensions of the image
      nW = int((height * sin) + (width * cos))
      nH = int((height * cos) + (width * sin))
      # adjust the rotation matrix to take into account translation
      rotation_matrix[0, 2] += (nW / 2) - width//2
      rotation_matrix[1, 2] += (nH / 2) - height//2
  
      im_rotate = cv2.warpAffine(im_shift, rotation_matrix, (nW, nH))
      # phase3:crop the box out.
      (newCX, newCY) = (nW // 2, nH // 2)
      x_min = max(newCX - int(round(bb_width / 2)), 0)
      x_max = min(newCX + int(round(bb_width / 2)), nW)
      y_min = max(newCY - int(round(bb_height / 2)), 0)
      y_max = min(newCY + int(round(bb_height / 2)), nH)
      print([x_min,y_min,x_max,y_max])
      print([nW,nH])
      return im_rotate[y_min:y_max, x_min:x_max, :], horiz
function_tokens: def general_crop ( image , tile ) : x = [ p [ 0 ] for p in tile ] y = [ p [ 1 ] for p in tile ] # phase1:shift the center of patch to image center x_center = int ( round ( sum ( x ) / 4 ) ) y_center = int ( round ( sum ( y ) / 4 ) ) im_center = [ int ( round ( coord / 2 ) ) for coord in image . shape [ : 2 ] ] shift = [ im_center [ 0 ] - y_center , im_center [ 1 ] - x_center ] M = np . float32 ( [ [ 1 , 0 , shift [ 1 ] ] , [ 0 , 1 , shift [ 0 ] ] ] ) height , width = image . shape [ : 2 ] im_shift = cv2 . warpAffine ( image , M , ( width , height ) ) # phase2:imrote the im_shift to regular the box bb_width = ( math . sqrt ( ( y [ 1 ] - y [ 0 ] ) ** 2 + ( x [ 1 ] - x [ 0 ] ) ** 2 ) + math . sqrt ( ( y [ 3 ] - y [ 2 ] ) ** 2 + ( x [ 3 ] - x [ 2 ] ) ** 2 ) ) / 2 bb_height = ( math . sqrt ( ( y [ 3 ] - y [ 0 ] ) ** 2 + ( x [ 3 ] - x [ 0 ] ) ** 2 ) + math . sqrt ( ( y [ 2 ] - y [ 1 ] ) ** 2 + ( x [ 2 ] - x [ 1 ] ) ** 2 ) ) / 2 horiz = True if bb_width > bb_height : # main direction is horizental tan = ( ( y [ 1 ] - y [ 0 ] ) / float ( x [ 1 ] - x [ 0 ] + 1e-8 ) + ( y [ 2 ] - y [ 3 ] ) / float ( x [ 2 ] - x [ 3 ] + 1e-8 ) ) / 2 degree = math . atan ( tan ) / math . pi * 180 else : # main direction is vertical tan = ( ( y [ 1 ] - y [ 2 ] ) / float ( x [ 1 ] - x [ 2 ] + 1e-8 ) + ( y [ 0 ] - y [ 3 ] ) / float ( x [ 0 ] - x [ 3 ] + 1e-8 ) ) / 2 degree = math . atan ( tan ) / math . pi * 180 - np . sign ( tan ) * 90 horiz = False rotation_matrix = cv2 . getRotationMatrix2D ( ( width / 2 , height / 2 ) , degree , 1 ) cos = np . abs ( rotation_matrix [ 0 , 0 ] ) sin = np . abs ( rotation_matrix [ 0 , 1 ] ) # compute the new bounding dimensions of the image nW = int ( ( height * sin ) + ( width * cos ) ) nH = int ( ( height * cos ) + ( width * sin ) ) # adjust the rotation matrix to take into account translation rotation_matrix [ 0 , 2 ] += ( nW / 2 ) - width // 2 rotation_matrix [ 1 , 2 ] += ( nH / 2 ) - height // 2 im_rotate = cv2 . warpAffine ( im_shift , rotation_matrix , ( nW , nH ) ) # phase3:crop the box out. ( newCX , newCY ) = ( nW // 2 , nH // 2 ) x_min = max ( newCX - int ( round ( bb_width / 2 ) ) , 0 ) x_max = min ( newCX + int ( round ( bb_width / 2 ) ) , nW ) y_min = max ( newCY - int ( round ( bb_height / 2 ) ) , 0 ) y_max = min ( newCY + int ( round ( bb_height / 2 ) ) , nH ) print ( [ x_min , y_min , x_max , y_max ] ) print ( [ nW , nH ] ) return im_rotate [ y_min : y_max , x_min : x_max , : ] , horiz