Reputation: 941
I am trying to write a function that returns smaller parts of an image. I am using the function Rect() for cutting. I get no error when debugging but when I try to execute the function I get the following 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:\opencv\opencv-master\modules\core\src\matrix.cpp, line 522
Here is my code:
void divideImage(Mat input_image, vector<Mat> output_images, int width_fraction, int height_fraction ) {
int width = input_image.rows / width_fraction - 1;
int height = input_image.cols / height_fraction - 1;
for (int w = 0; w < input_image.rows; w+=width) {
for (int h = 0; h < input_image.cols; h+=height) {
Mat tiles = input_image(Rect(w, h, width, height));
output_images.push_back(tiles);
}
}
}
int main(int argc, char** argv)
{
// Get parameters from command line
CommandLineParser parser(argc, argv, keys);
String image_path1 = parser.get<String>(0);
if (image_path1.empty())
{
help();
return -1;
}
// Load image
cv::Mat img_1_rgb = imread(image_path1, 1);
Mat img_1;
cvtColor(img_1_rgb, img_1, CV_BGR2GRAY);
vector<Mat> output_images(4);
divideImage(img_1, output_images, 2, 2);
Seems like my ROI is somehow out of bounds.
With the help of api55 I present the correct loop:
void divideImage(Mat input_image, vector<Mat> output_images, int width_fraction, int height_fraction ) {
int width = (input_image.cols / width_fraction) - 1;
int height = (input_image.rows / height_fraction) - 1;
for (int w = 0; w < input_image.cols-width_fraction*width_fraction; w+=width) {
for (int h = 0; h < input_image.rows-height_fraction*height_fraction; h+=height) {
Mat tiles = input_image(Rect(w, h, width, height));
output_images.push_back(tiles);
//cout << w << " " << h << " " << width << " " << height << " " << endl;
}
}
}
Upvotes: 0
Views: 1003
Reputation: 11420
Your code is wrong. Let me give you an example:
Lets say your image is of a size of 640x480
now lets calculate the width variable of your function with the same parameters you use
int width = 640 / 2 - 1; // 319
now lets start our loop, for the first time w=0
and you get something like
Rect(0, 0, 319, 239)
Then for the next width iteration you will have w+=width
which is w=319
and something like
Rect(319, 0, 319, 239)
The second iteration will have w+=width
again which is w=638
, as you clearly can see 638 is less than the rows of my image (640), thus it will try to do
Rect(638, 0, 319, 239)
which will jump the assert mentioned, since
roi.x + roi.width <= m.cols
will be translated to
638 + 319 <= 640
which is false.
You have to change how it is looping, also in the best case scenario which it works, you will loose n columns/rows, being n the number of divisions. (you can try to put a limit like
input_image.rows - width_fraction
in the for loop check, if you do not care for the dropped columns.
Further suggestions, learn how to use the debugger!! it should jump on the assert, unless that you run it in release mode, if not, something is wrong, the code should fail always.
Upvotes: 1