so, i have been working with ORB for 2/3 months
i already make a simple code for a live cam detecting object and it can pinpoint the middle point of the object and make a boundering shape from the source image
but sometimes the M i got for cv2.findHomography() is giving None
i already anticipated with
if len(M)>0:
but it still cant handle the none type and give error
TypeError: object of type 'NoneType' has no len()
what should i changes with this?
ps : the M value example give
M = [[-9.30240003e-01 -1.71728582e+00 6.03724232e+02]
[-6.08129496e-02 -1.11204061e-01 3.90162374e+01]
[-1.55375499e-03 -2.83800942e-03 1.00000000e+00]]
this is the full code:
def LIVE_CAM_ORB(live_cam):
img = live_cam
#change the camera image and input image to black and white
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#search the keypoint of live cam
kp2, des2 = orb.detectAndCompute(img,None)
# Match descriptors.
matches = bf.knnMatch(des1,des2,k=2)
# Apply ratio test
good = []
for m,n in matches:
if m.distance < 0.75*n.distance:
good.append(m)
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC,5.0)
#(for debugging)
#matchesMask = mask.ravel().tolist()
print( "M = {}".format(M))
if len(M)>0:
dst = cv2.perspectiveTransform(pts,M)
(tl, tr, br, bl) = dst
midpoint = (tl+bl+br+tr)/4
midpoint = (tl[0]+bl[0]+br[0]+tr[0])/4
#midpoint = np.rint(midpoint) //round the integer
midpoint = midpoint.astype(int)
live_cam = cv2.polylines(live_cam,[np.int32(dst)],True,255,3, cv2.LINE_AA)
live_cam = cv2.circle(live_cam,(midpoint[0],midpoint[1]),5,255,-1)
thank for Burak
i just got home and try the
if M is not None
and its work, thank you