我正在嘗試編寫一個返回圖像較小部分的函數。我正在使用Rect()函數進行剪切。 我調試時沒有錯誤,但是當我嘗試執行我得到以下錯誤的功能:OpenCV錯誤:在cv :: Mat線斷言失敗522
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
這裏是我的代碼:
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);
好像我的投資回報率是有點出界。
隨着api55的幫助下,我給出正確的循環:
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;
}
}
}
每次都打印這個'Rect(w,h,width,height)',看看有什麼問題也許'width'或'height' less 0 –