c++ - OpenCV assertion fail inside roi? -


#include <fstream> #include <iostream> #include <string> #include <stdlib.h> #include <stdio.h> #include <iomanip> #include <stdio.h>  #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp>  using namespace cv; using namespace std;  int main(int argc, char** argv) {     ofstream fout("e:\\fyp\\image analysis\\imageanalysis\\imageanalysis\\cropped_image\\details.txt");      mat image = imread("rsz_2rsz_2iron-man.png", -1);      //declare global parameters     int     kernel = 3;                     //to input     int     levels = 16;                    //to input     int     gap = 5;                        //to input in mm     double  depth_gap = 0.2;                //to input in mm     int     feedrate = 300;                 //to input     int     background = 0;     int     width = image.cols;     int     height = image.rows;     double  x_window = width / kernel;     double  y_window = height / kernel;     int     x_anchor = 0;     int     y_anchor = 0;     double  height_threshold = 0;     double  width_threshold = 0;      int   max_img_width = 570; //in mm     int   max_img_height = 480; //in mm      cout << width << "x " << height << endl;     cout << "(physical dimension    : " << x_window * gap << " mm x " << y_window *gap << " mm)" << endl << endl;      height_threshold = ceil((y_window *gap) / max_img_height);     width_threshold = ceil((x_window * gap) / max_img_width);      int   width_panel = (x_window * gap) / width_threshold;     int   height_panel = (y_window *gap) / height_threshold;     int   no_panels = height_threshold * width_threshold;      cout << "no. of panels = " << height_threshold * width_threshold << endl;     cout << width_threshold << " (x-axis) x " << height_threshold << " (y-axis)" <<  endl << endl;      cout << "size of each panels : " << (x_window * gap) / width_threshold << "mm (x-axis) " << (y_window *gap) / height_threshold << "mm (y-axis)" << endl << endl;      //imshow("original image", image);      int image_width = width / width_threshold;     int image_height = height / height_threshold;      cout << image_height << " vs " << image_width << endl;      vector<mat> smallimages;      (int y = 0; y < height; y += image_height)     {         (int x = 0; x < width; x += image_width)         {             rect rect = rect(x, y, image_width, image_height);             smallimages.push_back(mat(image, rect));         }     }       //for displaying , saving cropped image     (int panel = 0; panel < no_panels; panel += 1)     {         string num_panel = to_string(panel);         string filetype = ".png";         string filename = "image " + num_panel + filetype;         //cout << num_panel << endl;         //imshow(filename, smallimages[panel]);         //waitkey(0);         string location = "e:\\fyp\\image analysis\\imageanalysis\\imageanalysis\\cropped_image\\" + filename;         imwrite(location, smallimages[panel]);     }      fout << "kernel = " << kernel << endl;     fout << "levels = " << levels << endl;     fout << "gap = " << gap << endl;     fout << "max height = " << max_img_width << endl;     fout << "max width = " << max_img_height << endl;     fout << "number of panels = " << height_threshold * width_threshold << endl;     fout << "size of each panels : " << (x_window * gap) / width_threshold << "mm (x-axis) " << (y_window *gap) / height_threshold << "mm (y-axis)" << endl << endl;      waitkey(0);     system("pause");      //return 0; } 

this program devide big image multiple image based on given max width , height , other properties.

when run program different kernel size, gave out assertion fail , ok.

the error:

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 c:\builds\master_packslave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 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 c:\builds\master_packslave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 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 c:\builds\master_packslave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 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 c:\builds\master_packslave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 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 c:\builds\master_packslave-win64-vc12-shared\opencv\modules\core\src\matrix.cpp, line 508 

you're getting rectangles outside of image. because of wrong conditions in for loops. use:

 (int y = 0; y <= (height - image_height); y += image_height)  {      (int x = 0; x <= (width - image_width); x += image_width)      {           rect rect = rect(x, y, image_width, image_height);           smallimages.push_back(mat(image, rect));      }  } 

your original conditions correct when height , width multiple of image_height , image_width, respectively.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -