萌新求大佬解答,找了3个小时了不知道怎么改。
  • 板块学术版
  • 楼主YYS114
  • 当前回复5
  • 已保存回复5
  • 发布时间2021/12/16 16:55
  • 上次更新2023/11/3 21:56:47
查看原帖
萌新求大佬解答,找了3个小时了不知道怎么改。
647699
YYS114楼主2021/12/16 16:55

#可能是vector使用不当

#define _CRT_SECURE_NO_WARNINGS 1
#include<istream>
#include<opencv2/opencv.hpp>
#include<math.h>
#include<vector>

using namespace std;
using namespace cv;

Mat src, dst,dst_gray,dst_two,dst_fen,ys,max_ys;
double dst_value = 100;
int main(int agrc, char** agrv) {
	src = imread("E://1.jpg");
	if (!src.data) {
		cout << "could not load image..." << endl;
	}
	namedWindow("input", CV_WINDOW_AUTOSIZE);
	namedWindow("tz", CV_WINDOW_AUTOSIZE);	
	namedWindow("output", CV_WINDOW_AUTOSIZE);
	imshow("input", src);
	
	src.copyTo(dst);
	cvtColor(dst, dst_gray, CV_BGR2GRAY);
	threshold(dst_gray, dst_two, dst_value, 255, THRESH_BINARY);

	vector<Mat>dst_fen;
	split(dst, dst_fen);
	subtract(dst_fen[0], dst_fen[2], ys);
	threshold(ys, ys, 110, 255, THRESH_BINARY);
	Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point());
	dilate(ys, ys, element);
	if (!ys.data) {
		cout << "could not load ys" << endl; 
	}
	//imshow("tz", max_ys);
	max_ys = ys & dst_two;
	dilate(max_ys, max_ys, element);

	vector<vector<Point>>contours;
	vector<Vec4i>hierarchy;
		findContours(max_ys, contours, hierarchy, RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

		vector<vector<Point> > contours_C(contours.size());
		vector<Rect>rect(contours.size());
		vector<Point2f>center(contours.size());
		vector<float>radius(contours.size());
		
		for (int i = 0; i < contours.size(); i++) {
			approxPolyDP(contours[i], contours_C[i], 10, true);
			rect[i] = boundingRect(contours_C[i]);
			minEnclosingCircle(contours_C[i], center[i], radius[i]);
		}
		
		for (int t = 0; t < contours.size(); t++) {
			Scalar color = Scalar(0, 0, 255);
			drawContours(dst, contours_C[t], t, color, 1, 8, vector<Vec4i>(), 0, Point());
			rectangle(dst, rect[t].tl(), rect[t].br(), color, 2, 8, 0);
			circle(dst, center[t], (int)radius[t], color, 2, 8, 0);
		}


		imshow("tz", max_ys); 




	waitKey(0);
	return 0;
}

报错

2021/12/16 16:55
加载中...