我一直在用 C++ 开发一个大学项目,基本上是一个使用 OpenCV 的 28x28 像素图像的数字识别器。你可能知道 MNIST 数据集,是的,我想使用它。好吧,我遇到了一个问题。 下面我附上了我的代码片段。该代码对于少量数据行来说工作得很好,但是当我使用实际数据集时,它会意外崩溃。它不提供输出,即匹配百分比,而是直接退出。它甚至没有完成执行。
void compareCSV(const string& csvPath1, const string& csvPath2)
{
double totalMatchPercentage = 0.0;
int dataCount = 0;
int rowCount = 0;
try
{
ifstream rowcount(csvPath1);
string row;
while (getline(rowcount, row)) {
++rowCount;
}
cout << "Existing rows: " << rowCount << endl;
rowcount.close();
// Open the CSV file for reading
ifstream tempFile(csvPath2);
// Check if the CSV file is successfully opened
if (!tempFile.is_open())
{
throw runtime_error("Error opening CSV file: " + csvPath2);
}
string temp;
getline(tempFile, temp);
stringstream ss1(temp);
vector<int> tempValues;
int value1;
while (ss1 >> value1)
{
tempValues.push_back(value1);
if (ss1.peek() == ',')
{
ss1.ignore();
}
}
cout << "Read values from temp file: ";
for (int val1 : tempValues)
{
cout << val1 << " ";
}
cout << endl;
// Check if the values vector has the expected size
if (tempValues.size() < 784)
{
cerr << "Error: Unexpected number of values in line." << endl;
}
for (int j = 0; j < tempValues.size(); ++j)
{
newImage.at<uchar>(1, j) = static_cast<uchar>(tempValues[j]);
}
tempFile.close();
ifstream dataFile(csvPath1);
if (!dataFile.is_open())
{
throw runtime_error("Error opening CSV file: " + csvPath1);
}
string line;
while (getline(dataFile, line)) {
//cout << "Starting to read Data on row " << dataCount + 1 << endl;
// Parse the line and fill the binary image matrix
stringstream ss2(line);
vector<int> dataValues;
int value2;
while (ss2 >> value2)
{
dataValues.push_back(value2);
if (ss2.peek() == ',')
{
ss2.ignore();
}
}
/*cout << "Read values from line: ";
for (int val2 : dataValues)
{
cout << val2 << " ";
}
cout << endl;*/
// Check if the values vector has the expected size
if (dataValues.size() < 784)
{
cerr << "Error: Unexpected number of values in line." << endl;
}
for (int j = 0; j < dataValues.size(); ++j)
{
existingImage.at<uchar>(1, j) = static_cast<uchar>(dataValues[j]);
}
// Parse the line and fill the binary image matrix
// Compare 'binNewImage' with 'binImage' and accumulate the match percentage
double matchPercentage;
matchPercentage = PercentCalc(newImage, existingImage);
//cout << "Match Percentage for Dataset " << dataCount + 1 << ": " << matchPercentage << "%" << endl;
totalMatchPercentage += matchPercentage;
++dataCount;
}
// Close the CSV file
dataFile.close();
double percent = (dataCount > 0) ? (totalMatchPercentage / dataCount) : 0.0;
cout << "Match Percentage = " << percent << " %" << endl;
}
catch (const exception& e) {
cerr << "Exception: " << e.what() << endl;
}
}
double PercentCalc(const Mat& newBinaryImage, const Mat& existingBinaryImage) {
// Calculate the match percentage
if (newBinaryImage.size() == existingBinaryImage.size() &&
newBinaryImage.type() == existingBinaryImage.type()) {
Mat diff;
double totalcount = 0;
double excount = 0;
double newcount = 0;
for (int j = 0; j < 784; j++) {
if (newBinaryImage.at<uchar>(1, j) == 0 && existingBinaryImage.at<uchar>(1, j) == 0) {
totalcount++;
}
if (existingBinaryImage.at<uchar>(1, j) == 0) {
excount++;
}
if (newBinaryImage.at<uchar>(1, j) == 0) {
newcount++;
}
}
//cout << "total-count = " << totalcount << endl;
//cout << "ex-count = " << excount << endl;
//cout << "new-count = " << newcount << endl;
return (static_cast<double>(totalcount) / static_cast<double>(excount)) * 100;
}
else
return 0.0; // Return 0 if dimensions or type mismatch
}
int main() {
img.compareCSV("data.csv","data1.csv");
system("pause");
}
感谢大家的宝贵见解,尤其是理查德。我找出了问题所在,但不完全确定原因,但程序不再崩溃。我得到了我需要的输出。 干杯!!