ECE4960-TS3-Archive/cv_code/cv_code.cpp
/* detect: get the position of all targets and save in to vector.
 *
 *
 *
 * return detection image.
 */
get the position of all targets and save in to vector.
return detection image.
get the position of all targets and save in to vector . return detection image .
processing_frame detect(Mat img) {
	Position tmp_decoy_pos, tmp_target_pos;
	double scale = 1.0;
	Mat mask,hsv;
	processing_frame cur_frame;
	cvtColor(img, hsv, COLOR_BGR2HSV);
	inRange(hsv, Scalar(hmin, smin, vmin), Scalar(hmax, smax, vmax), mask);
	medianBlur(mask,mask,13);
	mask = get_erosion(mask);
	mask = get_dilation(mask);
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	Mat dist_8u;
	mask.convertTo(dist_8u, CV_8U);
	findContours(dist_8u, contours, hierarchy, CV_RETR_CCOMP, CHAIN_APPROX_SIMPLE);
	vec_tmp_target_pos.clear();
	int target = 0, decoy = 0, total = 0;
	for (size_t i = 0; i < contours.size(); i++)
	{
		Moments moment = moments((Mat)contours[i]);
		double area = moment.m00;
		if (area >= 50 && area <= 250) {
			target++;
			Point pt;
			pt.x = int(moment.m10 / moment.m00);
			pt.y = int(moment.m01 / moment.m00);
			tmp_target_pos.x = pt.x;
			tmp_target_pos.y = pt.y;
			vec_tmp_target_pos.push_back(tmp_target_pos);
			circle(img, pt, 1 / 32.0, COLOR_TARGET, 5, 8);
		}
		else if (area >= 350 && area <= 1000) {
			decoy++;
			Point pt2;
			char pos2[100];
			pt2.x = int(moment.m10 / moment.m00);
			pt2.y = int(moment.m01 / moment.m00);
			circle(img, pt2, 1 / 32.0, COLOR_DECOY, 5, 8);
		}
	}
	track_mode();
	for (int i = 0;i < vec_target_pos.size();i++) {
		char tg_pos[100];
		sprintf(tg_pos, "tg%i", i);
		putText(img, tg_pos, Point(vec_target_pos[i].x, vec_target_pos[i].y), FONT_HERSHEY_DUPLEX, 1, COLOR_TARGET, 1);
	}
	sprintf(num_decoy, "#decoy=%i", decoy);
	putText(img, num_decoy, Point(5, 100), FONT_HERSHEY_DUPLEX, 1, COLOR_DECOY, 1);
	sprintf(num_target, "#target=%i", target);
	putText(img, num_target, Point(5, 50), FONT_HERSHEY_DUPLEX, 1, COLOR_TARGET, 1);
	sprintf(_fps, "fps=%i", curr_fps);
	putText(img, _fps, Point(500, 50), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255), 1);
	cur_frame.rgb = img;
	cur_frame.gray = mask;
	return cur_frame;
}
processing_frame detect ( Mat img ) { Position tmp_decoy_pos , tmp_target_pos ; double scale = 1.0 ; Mat mask , hsv ; processing_frame cur_frame ; cvtColor ( img , hsv , COLOR_BGR2HSV ) ; inRange ( hsv , Scalar ( hmin , smin , vmin ) , Scalar ( hmax , smax , vmax ) , mask ) ; medianBlur ( mask , mask , 13 ) ; mask = get_erosion ( mask ) ; mask = get_dilation ( mask ) ; vector < vector < Point > > contours ; vector < Vec4i > hierarchy ; Mat dist_8u ; mask . convertTo ( dist_8u , CV_8U ) ; findContours ( dist_8u , contours , hierarchy , CV_RETR_CCOMP , CHAIN_APPROX_SIMPLE ) ; vec_tmp_target_pos . clear ( ) ; int target = 0 , decoy = 0 , total = 0 ; for ( size_t i = 0 ; i < contours . size ( ) ; i ++ ) { Moments moment = moments ( ( Mat ) contours [ i ] ) ; double area = moment . m00 ; if ( area >= 50 && area <= 250 ) { target ++ ; Point pt ; pt . x = int ( moment . m10 / moment . m00 ) ; pt . y = int ( moment . m01 / moment . m00 ) ; tmp_target_pos . x = pt . x ; tmp_target_pos . y = pt . y ; vec_tmp_target_pos . push_back ( tmp_target_pos ) ; circle ( img , pt , 1 / 32.0 , COLOR_TARGET , 5 , 8 ) ; } else if ( area >= 350 && area <= 1000 ) { decoy ++ ; Point pt2 ; char pos2 [ 100 ] ; pt2 . x = int ( moment . m10 / moment . m00 ) ; pt2 . y = int ( moment . m01 / moment . m00 ) ; circle ( img , pt2 , 1 / 32.0 , COLOR_DECOY , 5 , 8 ) ; } } track_mode ( ) ; for ( int i = 0 ; i < vec_target_pos . size ( ) ; i ++ ) { char tg_pos [ 100 ] ; sprintf ( tg_pos , " " , i ) ; putText ( img , tg_pos , Point ( vec_target_pos [ i ] . x , vec_target_pos [ i ] . y ) , FONT_HERSHEY_DUPLEX , 1 , COLOR_TARGET , 1 ) ; } sprintf ( num_decoy , " " , decoy ) ; putText ( img , num_decoy , Point ( 5 , 100 ) , FONT_HERSHEY_DUPLEX , 1 , COLOR_DECOY , 1 ) ; sprintf ( num_target , " " , target ) ; putText ( img , num_target , Point ( 5 , 50 ) , FONT_HERSHEY_DUPLEX , 1 , COLOR_TARGET , 1 ) ; sprintf ( _fps , " " , curr_fps ) ; putText ( img , _fps , Point ( 500 , 50 ) , FONT_HERSHEY_DUPLEX , 1 , Scalar ( 0 , 0 , 255 ) , 1 ) ; cur_frame . rgb = img ; cur_frame . gray = mask ; return cur_frame ; }
detect: get the position of all targets and save in to vector.
detect : get the position of all targets and save in to vector .
//dst = Mat::zeros(img.size(), img.type()); //GaussianBlur(mask, mask, Size(5, 5), 1, 1); //target //drawContours(img, contours, static_cast<int>(i), Scalar(0, 255, (i + i * 20) % 255), -1); //char pos[100]; //sprintf(pos, "(%i,%i)mm", pt.x, pt.y); //putText(img, pos, Point(pt.x + 5, pt.y), FONT_HERSHEY_DUPLEX, 1, COLOR_TARGET, 1); //decoy //drawContours(img, contours, static_cast<int>(i), Scalar(0, 255, (i + i * 20) % 255), -1); //sprintf(pos2, "(%i,%i)mm", pt2.x, pt2.y); //putText(img, pos2, Point(pt2.x + 5, pt2.y), FONT_HERSHEY_DUPLEX, 1, COLOR_DECOY, 1); //cout << "(" << vec_target_pos[i].x<<","; //cout << vec_target_pos[i].y<<")" << endl;