B4J Question Object matching by jOpenCV

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Do we have some tutorial for a new project for objects recognition, with custom data set (photos) "uploading" ?
There is a task to identify 200 objects that are the same, with have some label manually prepared (so numbers on them are badly recognized). And i think that it may be used the software like for "face recognition".

Mostly it's for mini-PC with Linux is actual, as separate custom system.

UPD:
I have found jOpenCV !
But how to use object matching ?
Of my custom objects.
Not classification, but matching.
 
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Python code::
import cv2
import numpy as np
import matplotlib.pyplot as plt
surf = cv2.xfeatures2d.SURF_create(400)

# Read Images
train = cv2.imread('box.png',0)
test = cv2.imread('box_in_scene.png',0)

# Find Descriptors
kp1,trainDes1 = surf.detectAndCompute(train, None)
kp2,testDes2  = surf.detectAndCompute(test, None)

if testDes2 is None:
    return False
else:
# Create BFMatcher and add cluster of training images. One for now.
bf = cv2.BFMatcher(cv2.NORM_L1,crossCheck=False) # crossCheck not supported by BFMatcher
clusters = np.array([trainDes1])
bf.add(clusters)

# Train: Does nothing for BruteForceMatcher though.
bf.train()

matches = bf.match(testDes2)
matches = sorted(matches, key = lambda x:x.distance)

# Since, we have index of only one training image,
# all matches will have imgIdx set to 0.
for i in range(len(matches)):
    print matches[i].imgIdx

img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)
plt.imshow(img3)
plt.show()

Any help to translate to B4J?

I cannot create "surf" object... :(
 
Last edited:
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Hi,

For object matching, I'd recommend any other example that does not use SURF, since it is not part of the official OpenCV 3.4.4 release

As explained in the first library thread post, jOpenCV wraps the official OpenCV 3.4.4 Java release plus a couple of contrib modules that I decided to include for test: Aruco and Face.

If I remember correctly, there are other object/template matching examples (both python and Java) that do not use SURF. Before using them, it's worth making sure that all the needed modules belong to the main OpenCV branch/version, and not to the contrib or non-free ones :)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Thanks for reply, @JordiCP.
But manual of v.3.4.4 shows the same example: https://docs.opencv.org/3.4.4/d7/dff/tutorial_feature_homography.html
Another article told that ORB detector is free, instead of SURF. But the syntax is same: https://pysource.com/2018/03/21/fea...urf-obr-opencv-3-4-with-python-3-tutorial-25/

How to init (create) any detector correctly in your jOpenCV wrapper ?

UPD: found template matching example

But result is no good, the template is always "found", even for sure absent:
B4X:
Public Sub processInput(myMat As OCVMat) As OCVMat
    ' Will work on the cloned frame, since we will draw on it
    Dim frame As OCVMat
    myMat.copyTo(frame)
    
    mImgProc.cvtColor1(frame, frame, mImgProc.COLOR_RGB2GRAY)
    
    Dim dst, mask As OCVMat
    dst.Initialize
    mask.Initialize

    mImgProc.matchTemplate(frame, mMasks(0), dst, mImgProc.TM_CCOEFF, mask)
    Dim result As OCVMinMaxLocResult = mCore.minMaxLoc(dst, mask)
    
    Dim maxPoint As OCVPoint = result.maxLoc
    Dim color As OCVScalar = OCV.Scalar.create4(0, 255, 0, 255)
    Dim point As OCVPoint = OCV.Point.create(maxPoint.x + mMasks(0).cols, maxPoint.y + mMasks(0).rows)
    mImgProc.rectangle(myMat, maxPoint, point, color, 2, mImgProc.LINE_8, 0)
    Return myMat
End Sub
 
Last edited:
Upvote 0
Top