I have tried findNonZero and with the boundingRect. But nothing is helping me. I am new to C++ OpenCV. I did this using Python OpenCV which involved NumPy, but unfortunately I am not able to do the same in C++.
Input image
Python:
def crop_with_arg(refactor_image):
mask = refactor_image > 0
coord = np.argwhere(mask)
x0, y0 = coord.min(axis=0)
x1, y1 = coord.max(axis=0) + 1
cropped = refactor_image[x0:x1, y0:y1]
return cropped
def crop_image(image_crop, tol=50):
mask = image_crop > tol
return image_crop[np.ix_(mask.any(1), mask.any(0))]
compressed_img = crop_with_arg(gray)
croppped_image = crop_image(compressed_img, tol=50)
I am writing the code in Objective C++ to have wrapper for iOS.