Ask Your Question

Convert String to Mat Java

asked 2013-05-16 06:49:13 -0500

MysticBE gravatar image

updated 2013-05-16 11:39:35 -0500

I have a String saved that is full with Keypoints, but I need to use this string in the descriptor, which only takes Mat. How can I change my String to a Mat?

 String log = "[KeyPoint [pt={123.0, 164.0}, size=7.0, angle=-1.0, response=214.0, octave=0, class_id=-1], KeyPoint [pt={212.0, 170.0}, size=7.0, angle=-1.0, response=201.0, octave=0, class_id=-1], KeyPoint [pt={59.0, 116.0}, size=7.0, angle=-1.0, response=200.0, octave=0, class_id=-1], KeyPoint [pt={296.0, 133.0}, size=7.0, angle=-1.0, response=199.0, octave=0, class_id=-1]";

So if I could just change it from the String to the Mat, that would be perfect

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-05-16 11:06:53 -0500

Notas gravatar image

The descriptor takes a vector of Keypoints and creates a Mat with the descriptors. So what you have to do is parse your string via regular expressions or whatnot and create the vector. It would be best if you make a loop that goes over each keypoint that you have and you manually create a keypoint from the pasted data from the string. So you have

KeyPoint [pt={123.0, 164.0}, size=7.0, angle=-1.0, response=214.0, octave=0, class_id=-1]

as the first keypoint in your string and then extract the information (position, size etc.) again using regular expressions or other ways and then set the values of your keypoint object. You do this for every keypoint in the string and in the end, you got your Keypoint vector with the data that you previously had in your string.

edit flag offensive delete link more


I think you are misreading my question, i just want to turn the string "KeyPoint [pt={123.0, 164.0}, size=7.0, angle=-1.0, response=214.0, octave=0, class_id=-1]" into a Mat, I don't want to parse everything etc. since the content of my string is already the right input that the descriptor needs

MysticBE gravatar imageMysticBE ( 2013-05-16 11:37:49 -0500 )edit

No it is not. I had a look at the Java documentation and the compute method there expects a MatOfKeyPoint object. You can clearly see that it can either be created from an array or a list of Keypoint objects. And to get those Keypoint objects, you have to create them by the method I described. Just because you got your String from the toString method doesn't mean that there is a way to reverse this, i.e. creating an object from the string.

Notas gravatar imageNotas ( 2013-05-16 13:03:31 -0500 )edit

oke, thanks for the information. I had no idea it could not be reversed.. that's a shame. Is there somewhere were an example of this is given? I'm fairly new at this, I don't have the experience to just write this on the spot myself

MysticBE gravatar imageMysticBE ( 2013-05-16 13:47:45 -0500 )edit

About splitting the string, look at Use the split method like this: string.split("],") which should give you, following your example, a string array with 4 keypoints. Then loop over those strings and do for each: Use split(",") again, this time on your current keypoint string, e.g. "KeyPoint [pt={123.0, 164.0}, size=7.0, angle=-1.0, response=214.0, octave=0, class_id=-1]". This will give you another array (let's name it optionarray) that contains the individual parameters of your string. Then, to get the size, you write optionarray[1].substring(optionarray[1].indexOf("="), optionarray[1].length()-1) which should give you a string that contains 7.0. Then use Double.parseDouble(stringwith7.0) and you get the double. You do this stuff for all those options in there, besides the position it's always the same, but for ...(more)

Notas gravatar imageNotas ( 2013-05-16 15:05:24 -0500 )edit

answered 2013-05-16 12:02:38 -0500

You do not need openCV for this but string based C or C++ operations. Basicly you create a stringstream, apply a split on that string based on a known seperator, and continue doing to until you reach each element that has to come in a Matrix element of a row (1 row for 1 descriptor).

Once you have splitted enough based on known seperator, use int integervalue = atoi(stringvalue); to convert the values towards the correct format.

edit flag offensive delete link more


I don't think C or C++ operations will help since I use the Java wrapper, not C-code.

MysticBE gravatar imageMysticBE ( 2013-05-16 12:19:49 -0500 )edit

Java has string operations also which does exactly the same. Please put some effort in it... GOOGLE > Java String Splitting > first or second link will probably do it for you!

StevenPuttemans gravatar imageStevenPuttemans ( 2013-05-16 13:48:22 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-05-16 06:49:13 -0500

Seen: 902 times

Last updated: May 16 '13