I'm gonna to crop an image by using row and column detection. When it detect the black pixel, then it will know the column or row need to crop but the python show me;
if img[y,x] == [0]: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Here is the image
Can anyone please tell me what am i doing wrong?
import cv2
import numpy as np
img = cv2.imread('C:/Users/user/Desktop/FYP 2019/Sign Language 1/red1.png')
gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret,thresh = cv2.threshold(blur,125,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
#get the images dimensions
h = img.shape[0]
w = img.shape[1]
lower_min_point = -1
upper_min_point = -1
left_point = -1
right_point = -1
for y in range(0, h):
for x in range(0,w):
if img[y,x] == [0]:
lower_min_point = y
break
if lower_min_point != -1:
break
print (lower_min_point)
for y in range(h,0):
for x in range(0,w):
if img[y,x] == [0]:
upper_min_point = y
break
if upper_min_point != -1:
break
print(upper_min_point)
for x in range(0,w):
for y in range(0,h):
if img[y,x] == [0]:
left_point = x
break
if left_point != -1:
break
print(left_point)
for x in range(w,0):
for y in range(0,h):
if img[y,x] == [0]:
right_point = x
break
if right_point != -1:
break
print(right_point)
crop = img[lower_min_point:upper_min_point, left_point:right_point]
cv2.imshow("img",crop)
cv2.waitKey(0)
cv2.destroyAllWindows()