# Numpy: Crop Object Of Interest By Removing X And Y Axis That Are Black

## 18 October 2019 - 1 answer

Problem: I have some images (in numpy) that have a black background. In the middle, I have an object that I am interested in. I would like to crop the object of interest, in a numpy way.

Lets say we have an image that looks like this: I need a function `crop_region_of_interest`, that detects and removes any X and Y axis if the entire axis is black or [0,0,0], in order to get this: Some codes used in this demo:

``````# just a function to add colors to generate test image
img[pixel_x][pixel_y] = rgb
img[pixel_x][pixel_y] = rgb
img[pixel_x][pixel_y] = rgb

def generate_fake_image_for_stackoverflow():
# a black background image
base_img = np.zeros((16,16,3), dtype=int)

# lets add some colors, these are the region we want
for x in range(4,10):
for y in range(6,12):
if(x==y):
continue
if(x+y<12):
continue
if(x+y>16):
continue

return base_img

# a hardcoded cropped to generate expected result
def crop_region_of_interest(img):
# crop first axis
cropped = img[4:10]

# transpose to second axis, so can crop
cropped = cropped.transpose((1,0,2))
cropped = cropped[6:12]

# transpose back
cropped = cropped.transpose((1,0,2))
cropped = cropped.transpose((1,0,2))
cropped = cropped.transpose((1,0,2))

return cropped

img = generate_fake_image_for_stackoverflow() # to generate a test image
plt.imshow(img)
plt.show()

cropped = crop_region_of_interest(img) # a hardcoded cropped to generate expected result, this to be replaced
plt.imshow(cropped)
plt.show()
``````

just slight edit on the solution provided by [https://codereview.stackexchange.com/questions/132914/crop-black-border-of-image-using-numpy]

``````def crop_image(img,tol=0):
# img is 2D or 3D image data
# tol  is tolerance
if img.ndim==3: