qq_41747698 2021-10-08 15:15 采纳率: 75%
浏览 446
已结题

opencv dnn::Net变量初始化引起的内存越界

opencv 初始化样例代码为 dnn::Net = readNetFromonnx(string path),然后自动释放,但是这在dll中无法正常使用

dllexport class ocr(){
    private:
    Net net
    public:
    void set_net(string_path)
    {net = readnetFromonnx(path)}
}

这会导致所有内存分配错误,所有参数为乱码,所以我用简单方法添加了如下两段,创建该类对象时在构造函数中读取一次默认模型分配内存,然后set_net指定路径读取用户自己的模型,即可解决分配内存错误问题

ocr()
{net = net = readnetFromonnx(path)}
~ocr()
{nothing}

但是这样又出现了如下错误
stack overrun:
Unhandled exception at 0x00E61A6C in testdll_1.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.
可以运行但无法正常退出,初步推断为参数导致的内存越界,应该如何正确初始化该变量?

即便使用一个函数其中只有imshow图像也会存在该内存越界的问题

更多代码如下

头文件如下


class _declspec(dllexport) ocr
{
private:
    //string _model_path;
    int crop_mode = 1;
    int thresh_mode = 1;
    int bitwise_mode2 = 0;
    double blur1 = 3;
    double blur2 = 11;

    int morph_xx, morph_yy;
    int morph_x = 1;
    int morph_y = 1;
    int morph_x2 = 1;
    int morph_y2 = 3;
    int iteration = 1;
    int canny_max = 300;
    int min_width = 8;
    int flag;
    int flag2;
    string model_path = "./1.onnx";
    dnn::Net net;
    int rect_thresh = 0;
    int blocksize = 31;
    int C = 10;
    int a = 3;
    int c = 5;
    int b = -5;
    int d = 10;
    int softmax_thresh = 0.9;
    int _h = 1;
    int _w = 1;
    int _channel = 1;
    int RGB_mode = 0;//0为默认bgr
                     //unsigned char * img
    int showimg = 1;

public:
    ocr(void);
    ~ocr(void);
    
    void set_print();//打印参数
    void set_showimg(int x);

    void set_morph2(int x, int y);//canny用,默认即可,竖向断裂

    void set_morph(int x, int y);//同正负,正数变粗,负数变细
    void set_morph_easy(int x);//x=1: 3,1--字体横向变细,x=2:-3,-1--字体横向变粗, x=3:1,1--不变

    void set_blocksize(int _C, int _blocksize = 31);//阈值 blocksize自适应区块大小,影响背景过滤程度,默认31,C需人工调整,白色字体C为负数,深色字体正数,影响噪点
    void set_blocksize_easy(int x);//0:字体无粘连,背景无线条,自动阈值, x=1:31,15--字体深色   2:31,-15--字体浅色,背景深色   3:31,4--字体深色但较细

    void set_net(const string model_path);//const string 路径
                                          //void set_net_easy();





#include "stdafx.h"
#include "recognize.h"
#include <stdlib.h>  
#include <iostream>
#include <fstream>

#include "opencv2\opencv.hpp"
#include "opencv2\dnn.hpp"
#include "opencv2\dnn\all_layers.hpp"

#include <string>
#include <vector>
#include <algorithm>
#include <array>
#include<cmath>
#include<windows.h>
//#include "omp.h"  

using namespace std;
using namespace cv;
using namespace dnn;

ocr::ocr(void)
{
    //this->net0 = new Net();
    net = cv::dnn::readNetFromONNX(model_path);
}

ocr::~ocr(void)
{
    ;
    //delete this->net0;
    //~Net();
}

void ocr::set_print()
{
    //cout<<blur <<" blur2" << "\n";
    cout << "blur is" << this->blur2;
    cout << "morph_x, morph_y :" << this->morph_x << this->morph_y << "\n";
    cout << "morph_x2 , morph_y2 :" << this->morph_x2 << this->morph_y2 << "\n";
    //cout << "model_path :" << this->model_path << "\n";
    cout << "rect_thresh :" << this->rect_thresh << "\n";
    cout << "blocksize, C :" << this->blocksize << C << "\n";
    cout << "softmax_thresh :" << this->softmax_thresh << "\n";
}
  • 写回答

3条回答 默认 最新

查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月20日
  • 已采纳回答 10月14日
  • 修改了问题 10月8日
  • 赞助了问题酬金 10月8日
  • 展开全部

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大