roi out of bounds [closed]

asked 2019-01-10 11:12:01 -0600

sadiq gravatar image

updated 2019-01-10 11:20:22 -0600

berak gravatar image

I have the following error i don't have any idea how to fix it. can you please help me to fix this error? The full code is below.

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in cv::Mat::Mat, file ........\opencv\modules\core\src\matrix.cpp, line 323 #include "opencv2/core/core.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "math.h" #include <fstream> #include <iostream> #include <ctime> #include <vector>

using namespace cv;
using namespace std;
// Gradient Checking
#define G_CHECKING 0
// Conv2 parameter
#define CONV_FULL 0
#define CONV_SAME 1
#define CONV_VALID 2
// Pooling methods
#define POOL_MAX 0
#define POOL_MEAN 1
#define POOL_MAX 2
#define POOL_STOCHASTIC 1

#define ATD at<double>
#define elif else if
int NumHiddenNeurons = 50; //200
int NumHiddenLayers = 2;
int nclasses = 4; //10
int KernelSize = 3; //13
int KernelAmount = 8;
int PoolingDim = 2; //4
int batch;
int Pooling_Methed = POOL_STOCHASTIC;

typedef struct ConvKernel{
    Mat W;
    double b;
    Mat Wgrad;
    double bgrad;
}ConvK;

typedef struct ConvLayer{
    vector<ConvK> layer;
    int kernelAmount;
}Cvl;

typedef struct Network{
    Mat W;
    Mat b;
    Mat Wgrad;
    Mat bgrad;
}Ntw;

typedef struct SoftmaxRegession{
    Mat Weight;
    Mat Wgrad;
    Mat b;
    Mat bgrad;
    double cost;
}SMR;

Mat 
concatenateMat(vector<vector<Mat> > &vec){

    int subFeatures = vec[0][0].rows * vec[0][0].cols;
    int height = vec[0].size() * subFeatures;
    int width = vec.size();
    Mat res = Mat::zeros(height, width, CV_64FC1);

    for(int i=0; i<vec.size(); i++){
        for(int j=0; j<vec[i].size(); j++){
            Rect roi = Rect(i, j * subFeatures, 1, subFeatures);
            Mat subView = res(roi);
            Mat ptmat = vec[i][j].reshape(0, subFeatures);
            ptmat.copyTo(subView);
        }
    }
    return res;
}

Mat 
concatenateMat(vector<Mat> &vec){

    int height = vec[0].rows;
    int width = vec[0].cols;
    Mat res = Mat::zeros(height * width, vec.size(), CV_64FC1);
    for(int i=0; i<vec.size(); i++){
        Mat img(vec[i]);
       //  reshape(int cn, int rows=0),// cn is num of channels.
        Mat ptmat = img.reshape(0, height * width);
        Rect roi = cv::Rect(i, 0, ptmat.cols, ptmat.rows);
        Mat subView = res(roi);
        ptmat.copyTo(subView);
    }
    return res;
}

void
unconcatenateMat(Mat &M, vector<vector<Mat> > &vec, int vsize){

    int sqDim = M.rows / vsize;
    int Dim =  sqrt ((double) sqDim);
    for(int i=0; i<M.cols; i++){
        vector<Mat> oneColumn;
        for(int j=0; j<vsize; j++){
            Rect roi = Rect(i, j * sqDim, 1, sqDim);
            Mat temp;
            M(roi).copyTo(temp);
            Mat img = temp.reshape(0, Dim);
            oneColumn.push_back(img);
        }
        vec.push_back(oneColumn);
    }
}

int 
ReverseInt (int i){
    unsigned char ch1, ch2, ch3, ch4;
    ch1 = i & 255;
    ch2 = (i >> 8) & 255;
    ch3 = (i >> 16) & 255;
    ch4 = (i >> 24) & 255;
    return((int) ch1 << 24) + ((int)ch2 << 16) + ((int)ch3 << 8) + ch4;
}

void 
read_Mnist(string filename, vector<Mat> &vec){
    ifstream file(filename, ios::binary);
    if (file.is_open()){
        int magic_number = 0;
        int number_of_images = 0;
        int n_rows = 0;
        int n_cols = 0;
        file.read ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by sturkmen
close date 2020-11-09 10:27:30.178111

Comments

1

Use a debugger

LBerger gravatar imageLBerger ( 2019-01-10 11:16:33 -0600 )edit

NO, don't throw a WALL OF CODE at us.

please try to track down the problem, locally, and if it still persists, try again with a Minimal Reproducable Example !

berak gravatar imageberak ( 2019-01-10 11:22:03 -0600 )edit

and yea, debug it, for heavens sake !

berak gravatar imageberak ( 2019-01-10 11:22:57 -0600 )edit

My bad - I am totally new to it . em sorry !!

sadiq gravatar imagesadiq ( 2019-01-10 11:31:11 -0600 )edit

@sadiq, have no fear, what we want from you is:

don't be sorry, get wise ;)

see the main problem is -- noone can reproduce the problem with the code you show here. you even broke the editor with far too many lines ;(

berak gravatar imageberak ( 2019-01-10 11:39:40 -0600 )edit
2

May be that's original codeand readme in repo is "A single-layer Convolutional Neural Network See more details here: http://eric-yuan.me/cnn/" and there is a version 3 http://eric-yuan.me/cnn3/

LBerger gravatar imageLBerger ( 2019-01-10 11:43:11 -0600 )edit

What Are You Trying To Achieve? maybe you can find a more up-to-date code

sturkmen gravatar imagesturkmen ( 2019-01-10 11:49:59 -0600 )edit

@sadiq, run it through the debugger, let it crash, give us a backtrace (or stacktrace on VS, iirc)

berak gravatar imageberak ( 2019-01-10 11:53:03 -0600 )edit

yes sir this the original code but the code having errors - I am solving the errors of this code.

sadiq gravatar imagesadiq ( 2019-01-10 12:01:18 -0600 )edit

@sadiq, also let me revoke the downvote, which was mainly for: as it is now -- hopeless problem ;(

i'm sure you will be able to improve it ;)

berak gravatar imageberak ( 2019-01-10 12:01:20 -0600 )edit

Using opencv 4 and changing maxiter to 100 (instead of 200000), results are

Read trainX successfully, including 784 features and 60000 samples.
Read trainY successfully, including 60000 samples
Read testX successfully, including 784 features and 10000 samples.
Read testY successfully, including 10000 samples
lrate = 0.0236276
Network Learning, trained learning rate: 0.0236276
learning step: 0, Cost function value = 2.30938, randomNum = 18508
learning step: 1, Cost function value = 2.30848, randomNum = 32834

.....

LBerger gravatar imageLBerger ( 2019-01-10 12:17:40 -0600 )edit