gpt4 book ai didi

c++ - OpenCV 断言在 r​​oi 内失败?

转载 作者:行者123 更新时间:2023-11-28 05:13:45 25 4
gpt4 key购买 nike

#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:\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

最佳答案

您得到的是图像之外的矩形。这是因为 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));
}
}

只有当 heightwidth 分别是 image_heightimage_width 的倍数时,您的原始条件才是正确的。

关于c++ - OpenCV 断言在 r​​oi 内失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com