OpenCV 断言在 roi 中失败
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 be input
int levels = 16; //to be input
int gap = 5; //to be input in mm
double depth_gap = 0.2; //to be input in mm
int feedrate = 300; //to be 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) by " << (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;
for (int y = 0; y < height; y += image_height)
{
for (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 and saving cropped image
for (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) by " << (y_window *gap) / height_threshold << "mm (y-axis)" << endl << endl;
waitKey(0);
system("PAUSE");
//return 0;
}
该程序是根据给定的最大宽度和高度以及其他一些属性将大图像划分为多个图像。
当我以不同的内核大小运行程序时,有时它会给出断言失败,有时还可以。
错误:
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:buildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcmatrix.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:buildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcmatrix.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:buildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcmatrix.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:buildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcmatrix.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:buildsmaster_PackSlave-win64-vc12-sharedopencvmodulescoresrcmatrix.cpp, line 508
你得到的是图像之外的矩形。这是因为for
循环中的条件错误。用:
for (int y = 0; y <= (height - image_height); y += image_height)
{
for (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));
}
}
仅当 height
和 width
分别是 image_height
和 image_width
的倍数时,原始条件才是正确的。
相关文章:
- 如果没有malloc,链表实现将失败
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 视图中的参数推导失败:take_while
- 链接到自行创建的dll失败
- 带有特殊路径部分的"std::filesystem::weakly_canonical"失败
- GetShortPathName在网络驱动器上使用中文文件夹时失败
- gcc和c++17的过载解析失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在WSL:configure_file上对config_file的每次调用都失败:配置文件时出现问题
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 从父数组测试用例构造二叉树失败
- LibGit2 SSH身份验证失败
- 如何让LLDB在成功时退出,在失败时等待
- OpenCV断言对RECT ROI失败了
- OpenCV 断言在 roi 中失败
- OpenCV断言失败,ROI
- 断言ROI opencv失败