C++ OpenCV Read string instead of loadFromCSV - c++

I am using OPENCV 3.0.0 and C++.
I need to read a csv type string instead of reading a csv file.
In the sample code , it uses
Ptr<TrainData> data = TrainData::loadFromCSV(filename, 0, response_idx, response_idx+1, typespec);
How could I read a csv type string like this
string str1 = " 0,31,29,82,1,0,0,0,2,normal " ;
then can use it to TrainData ?
Thanks!!

Related

OpenCV Creating Video From Text File

I am trying to build an application, which get data from webcam or external device, saves Video Frames into text file, then read frames from created text file.
I don't know whether it is a good idea to save to text file, I'm open suggestions.
So far I've done to saving to a text file.
My problem is reading from text file. Basically I read text line by line, but I don't know how to convert this text into Mat object.
So far my code is:
ifstream read_storage(new_vid_frm_path);
if(!read_storage.is_open()) {
perror("\n\n\n\t\t\t(-)FAIL : Can't Open SavedVideoFrames.txt\n\n\n\t\t\t");
return -1;
}
VideoWriter *vid = new VideoWriter(new_vid_frm_path,CV_FOURCC('P', 'I', 'M', '1'),30,Size(vc.get(CV_CAP_PROP_FRAME_WIDTH),vc.get(CV_CAP_PROP_FRAME_HEIGHT)));
Mat line;
vector<Mat> vid_frms;
while ( getline (read_storage,line) ) {
cout << line << '\n';
}
read_storage.close();
if(vid_frms.size() == 0){
printf("\n\n\n\t\t\t(-)FAIL: Error In Frame\n\n\n\t\t\t");
return -1;
}
for(size_t i = 0; i<vid_frms.size(); i++)
(*vid).write(vid_frms[i]);
printf("\n\n\n\t\t\t(+)SUCCESS: Video Processing Complete \n\n\n\t\t\t ");
Do you have ay suggestions how can I cast or convert this line string to Mat obejct?
while ( getline (read_storage,line) ) {
cout << line << '\n';
}
Thanks.
By the way, I looked at this solution, but I couldn't understand.
Convert a string of bytes to cv::mat
I couldn't find the byte type in c++ and I think there might be a direct conversion between String to Mat object.
You can save anything (just about) in OpenCV to a .xml or .yml text file and then read it back in using the OpenCV XML/YAML FileStorage methods.
I highly recomend this over using native C++ methods for file stuff.
It's specifically designed to handle all this legwork for you.

C++: How to correctly write in a .csv file

I have following data in a text file
04EC3AC705EA3AC8042C3BC7051A3AC8043C3AD7054A3AE8
I want to put this data into a .csv file in a single column as follows.
04EC,3AC7
05EA,3AC8
042C,3BC7
051A,3AC8
043C,3AD7
054A,3AE8
What I tried so far is:
fp=fopen("D:/pattern.csv", "a+")
Then I read the original values from the text file in a buffer
fwrite(buffer, 48,1,fp);
But again I get all values in a single cell in the csv sheet.
Please tell me a better way to achieve this. I developed this code in MSVS 2010 win32 and I will be using this code in another application written in C++/CLI Windows form application.
Thanks
If I well understand, your input file contains only one line with a single string. And you want to convert it to pairs of 4 char ?
What about a more 'C++' way like this ?
std::string input_file_name = "input.txt";
std::string output_file_name = "output.csv";
std::string line;
std::ifstream input_file(input_file_name.c_str());
input_file >> line;
std::ofstream output_file(output_file_name.c_str());
for(unsigned long int i = 0; i + 7 < line.length(); i += 8)
output_file << line.substr(i, 4) << "," << line.substr(i + 4, 4) << std::endl;

fread() binary mode not working properly in c++

I was facing a problem with the fread() function while coding in c++ .
The target is to open a file in binary mode for writing using :
fopen_s(&filename,filelocation,"wb"); , write a string and an int to be precise using fwrite(&buffer,size_of_read,count,filename) and then to read back the data into other variables (note that this may seem foolish to write and then read data from the same file in the same program, but this is just for testing purpose, since the project I am working on is strictly based upon encryption, and this is one of the modules)...
Here's my code :
int x = 1; // this is piece of data to write to the file
FILE *src; // the file variable that I am using
string text = "byebye"; // this happens to be the piece of data
src = fopen("F:\\_log.log", "wb"); // open file in binary write mode
fwrite(&text, 1, 7, src); // that's 7 because of 6 characters plus '\0'
fwrite(&x, 4, 1, src); // 4 bytes for int ---> writing x=1 to file
text.clear(); // for verification purpose
fclose(src);
src = fopen("F:\\_log.log", "rb");
int y = 0; // this is the int that will contain read data
string read; // string that contains read data
fread(&read, 1, 7, src); // read the data and store in read
fread(&y, 4, 1, src); // read the stored int into y
cout << "string is : " << read << "\nNumber is : " << y << endl;
The output of the above program seems to be correct for integers but not for strings...
string is :
Number is : 1
The Number is : 1 statements clears that first y was zero but the data from the file had been successfully read and stored in y which then turned to 1
But, if the integer has been correctly read, then why not the string read
Also, there seems to be a sync in text and read because if I change the command text.clear(); to text="higuys"; then the output is :
string is : higuys
Number is : 1
How do I solve this ? (completely unable to understand what in the world is being processed in that code...)
Additional information (though it may not do much good) :
OS : Windows 10
IDE : codeblocks (tried with visual studio too)
Compiler : GNU GCC
Debugger : GDB
I have tried to check for any errors using perror(); and tricks like cout<<fread(&read,1,7,src)<<endl; to check the number of bytes read, but everything was OK. Please help me with this one.....
EDIT :
OK, I have tried using array of char instead of string and it worked !!
But still, one thing is not clear and that is : why are the two strings text and read in sync ?
You need to change
string read;
to
char read[7];
string is a class and not a buffer, so you cannot fread giving the address of a string object.
If the program is not only for a fixed 7 bytes string and not only C (you tagged the question as C++) then you should consider using ifstream/ofstream and string and write/read using operators << and >>.

How can I read from an XML-string in OpenCV?

I know how to load/save a cv::Mat instance into a XML-file (See this question).
But what I really need, is to parse a std::string (or char *) that contains the XML, and get the cv::Mat. Say I get the XML out of a database, and not from a file.
Is that possible?
You can do it since OpenCV 2.4.1.
Here is a code sample from release notes:
//==== storing data ====
FileStorage fs(".xml", FileStorage::WRITE + FileStorage::MEMORY);
fs << "date" << date_string << "mymatrix" << mymatrix;
string buf = fs.releaseAndGetString();
//==== reading it back ====
FileStorage fs(buf, FileStorage::READ + FileStorage::MEMORY);
fs["date"] >> date_string;
fs["mymatrix"] >> mymatrix;

Exiv2: How to read photo with UTF8 filepath?

I am using GTKmm and exiv2 to read EXIF metadata form photos. However Exiv2 functions accept only std::string file paths... When I try it on not ASCII filepath it crushes the program.
Is there any way to read that data? It would be great if Exiv2 accepted Glib::ustrings...
I'm interested in solutions for Windows and Linux.
Ok, I have a solution!
You just need to use function Glibmm::locale_from_utf8 to convert UTF8 string to std(ascii) string.
Here is an example:
void get_exif_data(const Glib::ustring &image_src)
{
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(Glib::locale_from_utf8(image_src));
image->readMetadata();
Exiv2::ExifData &exifData = image->exifData();
Exiv2::ExifData::const_iterator it = exifData.begin();
for(it;it!=exifData.end();it++) cout << it->key() + ": " + it->getValue() << endl;
}
If this is in Windows then you can use GetShortPathName.