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
i
I think that you will find that the problem goes away if you work from the other side of the image, as you are checking the first column (col[0] - it's black so you delete it and the black col[1] becomes col[0] then you check the col[1] - skipping the new col[0]....
If you start from the max it would work or if you stay on any given col once you have deleted it. Alternatively you can make a list of which to delete, reverse it and then do the deletions.
using opencv and numpy as in your attempt how about something like this:
im = cv2.imread(filename)
h,w,d = im.shape
#left limit
for i in range(w):
if np.sum(im[:,i,:]) > 0:
break
#right limit
for j in xrange(w-1,0,-1):
if np.sum(im[:,j,:]) > 0:
break
cropped = im[:,i:j+1,:].copy() # deep copy to get byte-aligned array needed for opencv
Why not calculate the frame and use PIL
from PIL import Image
img = Image.open('myImage.jpeg')
box = (50, 50, 100, 100)
area = img.crop(box)
This problem was already solved in this answer.
In [1]: from PIL import Image, ImageChops
In [3]: im = Image.open('iI3ZE.jpg')
In [4]: def trim(im):
...: bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
...: diff = ImageChops.difference(im, bg)
...: diff = ImageChops.add(diff, diff, 2.0, -100)
...: bbox = diff.getbbox()
...: if bbox:
...: return im.crop(bbox)
...:
In [5]: trim(im).show()
I used Pillow instead of PIL:
pip install pillow
Results in: