Some videos have frames that have black strips like borders. I have to remove them from the frames. I came up with a crude solution:
import sys, cv2, numpy
import Image, scipy
filename = "snap.jpeg"
img = cv2.imread(filename)
def checkEqual(lst):
return len(set(lst)) <= 1 ## <-- This is the maximum length of the set
def removeColumns(image):
for col in range(image.shape[1]):
for ch in range(3):
try:
checkEqual(image[:, col, ch].tolist())
except IndexError:
continue
else:
if checkEqual(image[:, col, ch].tolist()):
try:
image = numpy.delete(image, col, 1)
except IndexError:
continue
else:
pass
return image
img2 = removeColumns(img)
print img.shape, img2.shape ## (480, 856, 3) (480, 705, 3)
Here I find the columns that have the same elements and all the videos that I have have black borders. But even if I increase the maximum length in the function checkEqual()
from 1 to 20 or 40, the whole black strip is not deleted.
This is the original image:
This is the image after running the program:
Could anyone give suggestion for a better solution to this problem ? Thanks!