如图1、2所示程序,程序中并没有使用除法,但仍然出现了被除数不能为0的问题,求大佬解惑!
代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char**argv)
{
Mat image, img;
image = imread("D:\\VS2013\\1.bmp");
img = image.clone();
Mat gray_image;
cvtColor(img, gray_image, CV_BGR2GRAY);
Mat thre_image;
threshold(gray_image, thre_image, 120, 255, CV_THRESH_BINARY);
Mat erode_image, dilate_image;
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
erode(thre_image, erode_image, element);
dilate(erode_image, dilate_image, element);
vector<vector<Point>> contours;
vector<cv::Mat> rois;
findContours(dilate_image, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i<(int)contours.size(); i++) //分割的个数
{
double x = contourArea(contours[i]);//i个分割部分的面积
if (x > 71000)
{
Rect boundrect = boundingRect(contours[i]);
Mat ROI = img(boundrect).clone();
rois.push_back(ROI);
}
}
for (int j = 0; j<(int)rois.size(); j++)
{
char filename[500];
sprintf_s(filename, "%s%d%s", "D:\\fenge\\", j, ".jpg");
imwrite(filename, rois[j]);
//string Img_Name = "D:/fenge/" + to_string(j) + ".jpg";
//imwrite(Img_Name, rois[j]);
}
// imshow("test", img);
waitKey();
system("pause");
return 0;
}