OpenCV imread Acess Violation - PullRequest
       7

OpenCV imread Acess Violation

0 голосов
/ 24 сентября 2019

Я пытаюсь импортировать изображения из папки, используя imread, но происходит сбой из-за нарушения прав доступа. «Исключение: нарушение прав чтения. This-> u было 0xFFFFFFFFFFFFFFEB.»

Класс, выполняющий это

#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 ++, вызов imread в строке 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, моим кодом или неправильно определенными зависимостями, но я искалнекоторое время и ничего не нашел.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...