Ask Your Question
0

Convert String to Mat

asked 2015-04-15 04:55:49 -0600

zms gravatar image

Hi, Anyone knows how to convert a string to Mat Format? I wanted to have this type of Mat format

cv::Mat data(1,256,CV_32F)

I'm using this code to convert from the string but have problem. Anyone has idea on how to solve this?

     for(int d=0;d<256;d++)
     {

         testing += '0' + pixelValueArray[d];

      }

     cout<<testing; //this is the string

     cv::Mat data(1,ATTRIBUTES,CV_32F,testing.data());

appreciate so much

edit retag flag offensive close merge delete

Comments

It'll be better if you add more info about the pixelValueArray and testing, if you want that I may test what I post :) And more: what is the 0 you add? is it a separator? how would you split testing into values? Or it is just one value? Then why do yo want a Mat that has 256 columns and one row?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-15 06:46:39 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2015-04-15 05:05:58 -0600

thdrksdfthmn gravatar image

updated 2015-04-15 06:44:29 -0600

If you know that testing is for sure a number, you can use stof. Because you have a vector (1x256), you can create a vector of floats (CV_32F) and then use the Mat constructor that is having as parameter the vector; or use the same syntax as you used:

cv::Mat data(1,ATTRIBUTES,CV_32F,vecOfFloats.data());

better explanation:

std::vector<int> values;
for(int d=0;d<256;d++)
{
   //testing += '0' + pixelValueArray[d];
   values.push_back(stoi('0' + pixelValueArray[d], NULL, 2));
}
cv::Mat data(1,ATTRIBUTES,CV_32F,values.data());
edit flag offensive delete link more

Comments

Hi, Yes it is confirmed the string consist of 1 and 0 like this. 00000000000000000000000000000000000000000000000010000000000000000100000000000000 00100000000000001011000000000000110110000000000001101100000000000010011000000000 00010010000000000000100100000000000011001000000000000110010000000000001100100000 0000000110010000

so I tried to do the suggestion but having problem with that. Error said float f: Error: Expression must have class type.

Here is my extended code after reading some example on stof. Maybe I'm still wrong.

     for(int d=0;d<256;d++)
     {

         testing += '0' + pixelValueArray[d];

      }

     cout<<testing; //this is the string

     float f = std::stof(testing);

    Mat data(1,256,CV_32F,f.data());
zms gravatar imagezms ( 2015-04-15 05:40:53 -0600 )edit

oh, if it is like that then it will not really work... maybe you shall do some conversions from binary... And more, I said put the values into a vector, I think you have not really explained that you are trying to do... testing is that 0s and 1s value? and it represent just one float? or all the 256?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-04-15 06:30:54 -0600 )edit
1

Hi thdrksdfthmn, This is what I want to do. i'm following the tutorial for ANN from this page

http://www.nithinrajs.in/ocr-artifici...

I had completed the step 1 and 2 and now the final for classification. In the classification codes, I need to have the input as in 0 and 1 as above for the classification code. That's where I do only stuck. It requires me to have the string to be converted to the MAT format. The 1 and 0 is the 1 X 256 arrays.

zms gravatar imagezms ( 2015-04-16 01:15:32 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-04-15 04:55:49 -0600

Seen: 3,770 times

Last updated: Apr 15 '15