C++/MFC. Insert Image in CSV file Using MFC - c++

Hello I'm studying MFC and I wanna know how to insert some images un csv file.
The file structure is as follows:The result folder contains 1.jpg, 2.jpg files.
In csv file, at the top "Index, Name, Age, Picture" must be included and "Index, Name, Age" are in the List Control.
I've finished entering the information in the csv file using the code below. However, I can't figure out how to insert the images in csv file.
`
CString _FilePath = theApp.m_ResultDir + _T("Result.csv"); //m_ResultDir : result folder Location
std::ofstream File(_FilePath,'w');
File << "Index, Name, Age, Picture\n";
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetHeaderCtrl();
int nRow = m_ListControl.GetItemCount();
int nCol = pHeader->GetItemCount();
CString text;
for (int i = 0; i < nRow; i++)
{
text = "";
for (int j = 0; j < nCol; j++)
{
text = text+ m_ListControl.GetItemText(i, j) + _T(", ");
}
File << text + "\n";
}
File.close();
`
It would be easy problem, but I'd appreciate it if you understand because it's my first time doing this.
This are what I tried.
First, I tried using TypeLib and select excel.exe and i contained some header files. However, I wanna make it csv file not xlsx file.
Second, using result folder location, I tried to add images. but failed.
`
CString image;
image.Format(theApp.m_ResultDir+_T("%d.jpg"), i+1);
text += image;
`

First argument to CString::Format is a format specification, followed by the arguments to be formatted. So something like that would work:
image.Format(_T("%s%d.jpg"), theApp.m_ResultDir, i+1);

Related

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;

Using QImage load() with filename containing spaces in QT

I am using QImage.load() to load an image in my program, but it only works when the filename doesn't contain any spaces (for exemple: "/Users/Emile/Dropbox/crookedStall cover.jpg"). As I want the user to be able to select any image from their computer, this is a bit of an issue.
The filename of the selected image is returned by a function is stored in a QString. I have tried using QString.replace() to escape the spaces with a backslash but that didn't seem to work. Obviously, simply removing the spaces doesn't work either.
I've looked around a bit and didn't find any working solutions.
How can I load an image with a filename that contains spaces? Thanks!
This works fine for me:
This is just using QFileDialog::getOpenFileNames to locate the files
QString filter = QString("Supported Files (*.shp *.kml *.jpg *.png );;All files (*)");
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select File(s)"), QDir::homePath(), filter);
for(int idx =0; idx < fileNames.size(); ++idx)
{
QImage image ;
bool success = image.load(fileNames.at(i));
qDebug() << "File loaded succesfully " << success ;
}

Find Indexof in MFC C++

How can I find the indexof in GetWindowsText?
I just want to get extensions from box->fileExt->GetWindowsText(save);
For example my input is .exe .txt .bmp
So I want to get them separately. For example something like that:
.exe
.txt
.bmp
Currently my code is this:
for (int i = 0; i < files; i++)
{
box->testBox1.AddString(save);
fileExtensions.Add(save)`enter code here`;
CString check;
box->fileExt.GetWindowText(check);
CString store = check;
check.Find(' ') == save;
break;
continue;
if (fileExtensions[fileCounter] == store)
{
box->textBox2.AddString(fileExtensions[fileCounter]);
fileCounter++;
}
}//end for
It does not work.
You need to split string based on separator character(s). You can use Tokenize method of CString to do that:
CString sExtensions(_T(".exe .txt .bmp"));
CString sExt;
int nCurPos = 0;
CString sSeparators(_T(" ;"));
CStringArray Extensions;
sExt = sExtensions.Tokenize(sSeparators, nCurPos);
while (!sExt.IsEmpty())
{
Extensions.Add(sExt);
sExt = sExtensions.Tokenize(sSeparators, nCurPos);
}

How to use "PASCAL Annotation" files to train SVM with C++

I need to test my selfwritten person detector using Hog Descriptor (Dalal paper) on a Scientific dataset (INRIA person dataset). I have thousands of pos and neg Images in my Folder to train a Support Vector Machine (SVM). However to label the Images as positive (1.0) or negative (-1.0) I need to read the information from a text file that is provided with the dataset in a so called "PASCAL Annotation" format.
My problem is that I don't know how to read this format efficiently. I'm using C++ and OpenCV. Does anyone know how to do this efficiently? Are there already code snippets for C++?
In the end I need a loop that goes through a file "Annotations.lst" where all the picture filenames are listed. The program loads the picture and the corresponding annotation file (picturename.txt) to see if this picture belongs to a positive or negative training data (or later when actually detecting: if the tested picture belongs to a detected person or not)
Thanks for your help!
Maybe it's not the best implementation but works fine, hope it becomes useful even today! You will need filesystem library in order to work with files.
string line,value; //Line stores lines of the file and value stores characters of the line
int i=0; //Iterate through lines
int j=0; //Iterate through characters
int n=0; //Iterate through ,()-...
char v; //Stores variable value as a char to be able to make comparisions easily
vector <Rect> anotations; //Stores rectangles for each image
vector <int> bbValues; //Bounding box values (xmin,ymin,xmax,ymax)
fs::path anotationsFolder = "THE FOLDER PATH OF ANOTATIONS"; //Path of anotations folder
fs::path anotationsParsedFolder = "THE FOLDER PATH TO STORE PARSED ANOTATIONS"; //Path to store new anotations
fs::recursive_directory_iterator it(anotationsFolder); //Iteradores of files
fs::recursive_directory_iterator endit;
cout<<"Loading anotations from "<<anotationsFolder<<endl;
while((it != endit)) //Until end of folder
{
if((fs::is_regular_file(*it))) //Good practice
{
fs::path imagePath(it->path()); //Complete path of the image
cout<<"Reading anotations from"<<it->path().filename()<<endl;
ifstream inputFile; //Declare input file with image path
inputFile.open(imagePath.string().data(), std::ios_base::in);
i=0;
while (! inputFile.eof() ){ //Until end of file
getline (inputFile,line);//Get lines one by one
if ((i>=17) && ((i-17)%7==0)){ //In lines numer 17,24,31,38...where bounding boxes coordinates are
j=69;
v=line[j]; //Start from character num 69 corresponding to first value of Xmin
while (j<line.size()){ //Until end of line
if (v=='(' || v==',' || v==')' || v==' ' || v=='-'){ //if true, push back acumulated value unless previous value wasn't a symbol also
if (n==0){
bbValues.push_back(stoi(value)); //stoi converts string in to integer ("567"->567)
value.clear();
}
n++;
}
else{
value+=v; //Append new number
n=0;//Reset in order to know that a number has been read
}
j++;
v=line[j];//Read next character
}
Rect rect(bbValues[0],bbValues[1],bbValues[2]-bbValues[0],bbValues[3]-bbValues[1]); //Build a rectangle rect(xmin,ymin,xmax-xmin,ymax-ymin)
anotations.push_back(rect);
bbValues.clear();
}
i++;//Next line
}
inputFile.close();
cout<<"Writing..."<<endl;
//Save the anotations to a file
ofstream outputFile; //Declare file
fs::path outputPath(anotationsParsedFolder / it->path().filename());// Complete path of the file
outputFile.open(outputPath.string().data(), ios_base::trunc);
// Store anotations as x y width heigth
for (int i=0; i<anotations.size(); i++){
outputFile<<anotations[i].x<<" ";
outputFile<<anotations[i].y<<" ";
outputFile<<anotations[i].width<<" ";
outputFile<<anotations[i].height<<endl;
}
anotations.clear();
outputFile.close();
}
++it;//Next file in anotations folder
}

Cannot save filepath to database from c++

I am trying to save an image path to a mysql database from c++. The insertion takes place but the path is saved in this form:
C:Usersakrs.aDesktopatch_1images 01aa1363659036.jpg
rather than
C:\Users\akrs.a\Desktop\batch_1\images\001aa1363659036.jpg
so it is omitting '\','\b' and '\0'.
The code for the insertion in c++ is:
for (int i = 0 ; i < 2; i++)
{
std::string imgpath=dresses[i]->imgPath->data(); //gets the path
std::ostringstream querydb;
querydb<<"insert into base_table(imgPath,store,apparelType) values('"<< imgpath <<"','testdb','dress')";
mysql_query(connect,querydb.str().c_str());
}
mysql_close(connect);
I tried to print out the querydb too and the imagepath is sent correctly.How can I solve this problem?
try to replace all "\" with "\\" in "imgpath" variable.