Removing Borders/Margins from Video Frames

Prithwish Jana

I am working with videos, that have borders (margins) around them. Some have it along all 4 sides, some along left&right only and some along top&bottom only. Length of these margins is also not fixed. I am extracting frames from these videos, as for example,

enter image description here

and

enter image description here

Both of these contain borders on the top and bottom.

Can anyone please suggest some methods to remove these borders from these images (in Python, preferably). I came across some methods, like this on Stackoverflow, but this deals with an ideal situation where borders are perfectly black (0,0,0). But in my case, they may not be pitch black, and also may contain jittery noises too. Any help/suggestions would be highly appreciated.

fmw42

Here is one way to do that in Python/OpenCV.

  • Read the image
  • Convert to grayscale and invert
  • Threshold
  • Apply morphology to remove small black or white regions then invert again
  • Get the contour of the one region
  • Get the bounding box of that contour
  • Use numpy slicing to crop that area of the image to form the resulting image
  • Save the resulting image


import cv2
import numpy as np

# read image
img = cv2.imread('gymnast.png')

# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# invert gray image
gray = 255 - gray

# gaussian blur
blur = cv2.GaussianBlur(gray, (3,3), 0)

# threshold
thresh = cv2.threshold(blur,236,255,cv2.THRESH_BINARY)[1]

# apply close and open morphology to fill tiny black and white holes
kernel = np.ones((5,5), np.uint8)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

# invert thresh
thresh = 255 -thresh

# get contours (presumably just one around the nonzero pixels) 
# then crop it to bounding rectangle
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
cntr = contours[0]
x,y,w,h = cv2.boundingRect(cntr)
crop = img[y:y+h, x:x+w]

cv2.imshow("IMAGE", img)
cv2.imshow("THRESH", thresh)
cv2.imshow("CROP", crop)
cv2.waitKey(0)
cv2.destroyAllWindows()

# save cropped image
cv2.imwrite('gymnast_crop.png',crop)
cv2.imwrite('gymnast_crop.png',crop)


Input:

enter image description here


Thresholded and cleaned image:

enter image description here

Cropped Result:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Import frames from a video

From Dev

Removing gradient from video

From

Create video from camera frames

From Dev

Creating a video from individual frames

From Dev

Extract frames from video angular

From Dev

Removing background of previous frames from matlab gif

From Dev

removing rows on conditions from nested data frames

From Dev

FFMpeg - Trimming out the video after removing duplicate frames

From Dev

Removing video from embed youtube video

From Dev

how to get frames with timestamp where frames are converted from video in python

From Dev

FFMPEG not extracting all the frames from a video

From Dev

Extract frames from video into specific folder

From Dev

Extract Frames from Video C#

From Dev

WebGL textures from YouTube video frames

From Dev

How to read frames from a video as bitmaps in UWP

From Dev

How to read frames from a video MATLAB?

From Dev

How to render video from ARGB Frames

From Java

TensorFlow - Read video frames from TFRecords file

From Dev

How to extract clear frames from video file?

From Java

Getting frames from Video Image in Android

From Dev

Grab frames from video using Swift

From Dev

Swift - get all frames from video

From Dev

Extract frames from video with ffmpeg - header problem?

From Dev

Getting a specific sequence of frames from video in python

From Dev

Streaming frames from the screen, generating a video

From Dev

how to select specific frames from a video in MATLAB?

From Dev

Extract frames from a video using OpenImaj in Java

From Dev

How to extract frames from a video java?

From Dev

Trying to create video from multiple frames