error while converting MatOfKeyPoints to json string

asked 2018-03-24 16:01:45 -0600

pape gravatar image

updated 2018-03-25 05:37:30 -0600

berak gravatar image

i am trying to convert a MatOfKeyPoints object in opencv android to json string .

MatOfKeyPoint refKeypoints = new MatOfKeyPoint();

Mat refDescriptors = new Mat();
FeatureDetector orbFeatureDetector = FeatureDetector.create(FeatureDetector.ORB);
orbFeatureDetector.detect(grayMat, refKeypoints);
Mat Out = new Mat();
DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
descriptorExtractor.compute(grayMat, refKeypoints, refDescriptors);
Features2d.drawKeypoints(grayMat, refKeypoints, Out,new Scalar(2,254,255),Features2d.DRAW_RICH_KEYPOINTS);
//System.out.println(refKeypoints);
Utils.matToBitmap(Out,grayBitmap);
imageView.setImageBitmap(grayBitmap);
JsonObject obj = new JsonObject();

if(refKeypoints.isContinuous()){
    int cols = refKeypoints.cols();
    int rows = refKeypoints.rows();
    int elemSize = (int) refKeypoints.elemSize();

    byte[] data = new byte[cols * rows * elemSize];

    refKeypoints.get(0, 0, data);

    obj.addProperty("rows", refKeypoints.rows());
    obj.addProperty("cols", refKeypoints.cols());
    obj.addProperty("type", refKeypoints.type());

    // We cannot set binary data to a json object, so:
    // Encoding data byte array to Base64.
    String dataString = new String(Base64.encode(data, Base64.DEFAULT));

    obj.addProperty("data", dataString);

    Gson gson = new Gson();
    String json = gson.toJson(obj);

} else {

}

The error is most probably in the line refKeypoints.get(0, 0, data); how to rectify it?

edit retag flag offensive close merge delete

Comments

i removed your screenshot.

please edit your question, and add the resp. information as text

berak gravatar imageberak ( 2018-03-24 18:32:12 -0600 )edit

what is the purpose of it ? do you really need the whole keypoint infpormation ?

you can't use byte here. try something like:

float[] b = new float[kp.channels() * (int)kp.total()];
berak gravatar imageberak ( 2018-03-25 06:07:43 -0600 )edit

i need to save the whole keypoints information in database . for that reason i need to convert into json string . how to do it?

pape gravatar imagepape ( 2018-03-25 06:33:33 -0600 )edit

why do you need to save the keypoints ? what are you trying to do with that ?

databases like sqlite do support binary blobs, no need for json or base64.

berak gravatar imageberak ( 2018-03-25 06:37:28 -0600 )edit

i have a main server running with python . the android devices need to process an image to find keypoints and send it back to python server .so i need to convert keypoints to json

pape gravatar imagepape ( 2018-03-25 07:52:28 -0600 )edit