roi out of bounds [closed]
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 ...
Use a debugger
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 !
and yea, debug it, for heavens sake !
My bad - I am totally new to it . em sorry !!
@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 ;(
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/
What Are You Trying To Achieve? maybe you can find a more up-to-date code
@sadiq, run it through the debugger, let it crash, give us a backtrace (or stacktrace on VS, iirc)
yes sir this the original code but the code having errors - I am solving the errors of this code.
@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 ;)