我正在尝试使用imread从文件夹导入图像,但是由于访问冲突“抛出异常:读取访问冲突而导致崩溃”。 这个-> u是0xFFFFFFFFFFFFFFFFEB。”
这样做的班级
#pragma once
#include "opencv2/opencv.hpp"
#include "ImageOut.h"
#include "dirent.h"
#include <fstream>
typedef struct TrainingData TrainingData;
bool isFileExtension(const char *file, const char *extension);
char *combined(const char *str1, const char *str2);
class TrainingManager
{
private:
const char *path = "C:\\Users\\Cameron\\source\\repos\\JAVA_JNI_FTC\\Data\\";
const char *pathData = "C:\\Users\\Cameron\\source\\repos\\JAVA_JNI_FTC\\Data";
TrainingData *data;
public:
void init_TrainingData(TrainingData *);
TrainingManager();
~TrainingManager();
};
struct TrainingData
{
cv::Mat * images;
ImageInfo *info;
int size;
};
C ++文件,第35行的未读调用会导致错误
#include "pch.h"
#include "TrainingManager.h"
TrainingManager::TrainingManager()
{
}
void TrainingManager::init_TrainingData(TrainingData *data)
{
DIR *dir;
struct dirent *ent;
std::ifstream file;
if ((dir = opendir(this->pathData)) != NULL)
{
int fileCount = 0;
while ((ent = readdir(dir)) != NULL)
{
fileCount++;
}
fileCount -= 2;
closedir(dir);
data->images = (cv::Mat *) malloc(sizeof(cv::Mat) * fileCount/2);
data->info = (ImageInfo *) malloc(sizeof(ImageInfo) * fileCount/2);
dir = opendir(this->pathData);
int infoIndex, imageIndex;
infoIndex = imageIndex = 0;
while ((ent = readdir(dir)) != NULL)
{
if (isFileExtension(ent->d_name, "png"))
{
std::cout << combined(this->path, ent->d_name);
std::string string = "C:\\Users\\Cameron\\source\\repos\\JAVA_JNI_FTC\\Data\\Data1.png";
*(data->images + imageIndex) = NULL;
cv::Mat *mat = new cv::Mat();
imageIndex++;
}
else if (isFileExtension(ent->d_name, "txt"))
{
file.open(combined(this->path, ent->d_name));
int *info = (int *) data->info + infoIndex;
int buf = 0;
int index = 0;
while (file >> buf || index < 4)
{
*(info + index) = buf;
index++;
}
}
}
}
else {
perror("");
}
}
bool isFileExtension(const char *file, const char *extension)
{
int index = 0;
while (*(file + index) != '\0') //go through the file until '.' indicating the start of the file extension
{
index++;
if (*(file + index) == '.')
{
index++;
int checkIndex = 0;
while (true)
{
if (*(file + index + checkIndex) == *(extension + checkIndex)) //check the chars after the start of the period to check file extension
{
if (*(extension + checkIndex) == '\0') return true; //if everything is equal up to
checkIndex++;
}
else
{
return false;
}
}
}
}
return false;
}
char *combined(const char *str1, const char *str2)
{
char *newString = (char *) malloc(strlen(str1) + strlen(str2) - 4);
int index = 0;
while (*(str1 + index) != '\0')
{
*(newString + index) = *(str1 + index);
index++;
}
int last = index;
index = 0;
while (*(str2 + index) != '\0')
{
*(newString + index + last) = *(str2 + index);
index++;
}
return newString;
}
int strlen(char *str)
{
int counter = 0;
while (*(str + counter) = '\0')
{
counter++;
}
return counter;
}
我不知道这是打开cv,我的代码还是定义不正确的依赖项所导致的错误,但是我已经寻找了一段时间,却一无所获。