#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
答案 0 :(得分:1)
您正在获取图像外部的矩形。这是因为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
的倍数时,您的原始条件才是正确的。