Np.argwhere operation equivalent for Nx - is this feature available currently? Is there an alternative that I’m not considering?

Hi all!

I’m currently using Evision and Nx in some computer vision applications. In my current scenario, I have an image which has a rectangular shape somewhere inside the image, and the background is black. I’d like to crop the image to just the rectangular shape. While I know I could use more advanced methods to detect the location of the rectangle (not the focus of this question), one simple one is just to sum along the rows and columns to find the rows/cols with non-zero values. Then, you can just crop the image to only include the specified rows and columns. The python code to do this is below

import numpy as np
import cv2

image = cv2.imread("image.png")
image_norm = np.linalg.norm(image, axis=2)

rows_sum = np.sum(image_norm, axis=1)
use_rows = np.argwhere(rows_sum > 0).ravel()

cols_sum = np.sum(image_norm, axis=0)
use_cols = np.argwhere(cols_sum > 0).ravel()

image_cropped = image[use_rows[:, np.newaxis], use_cols[np.newaxis, :]]
cv2.imwrite("output.png", image_cropped)

I had to fallback to Python to do this, because I couldn’t figure out how to do the argwhere operations in Nx. Is this feature available currently? Is there an alternative that I’m not considering?

Looking for a pointer in the right direction.

Thanks and happy new year!

Gus

1 Like

Nx can’t do dynamic shape modifications. You have a few options here:

  1. Crop with Evision
  2. Figure out the slice sizes outside of defn (by using Nx.argmin to figure out the starts and Nx.sum to figure out the lengths) and then use Nx.slice with those now-fixed values.