Ad
Numpy: Crop Object Of Interest By Removing X And Y Axis That Are Black
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
def add_color(img, pixel_x, pixel_y, rgb):
img[pixel_x][pixel_y][0] = rgb[0]
img[pixel_x][pixel_y][1] = rgb[1]
img[pixel_x][pixel_y][2] = rgb[2]
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
add_color(base_img, x,y, [255,60,90])
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()
Ad
Answer
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
mask = img>tol
if img.ndim==3:
mask = mask.all(2)
m,n = mask.shape
mask0,mask1 = mask.any(0),mask.any(1)
col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
return img[row_start:row_end,col_start:col_end]
Thanks @jamal and @divakar
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad