Finding middle of two objects opencv

I am trying to find the midpoint between two objects in this image but can not figure it out.
Please help.

#include “pch.h”
#include <opencv2/opencv.hpp>
#include

using namespace std;
using namespace cv;

int hue_min = 64, hue_max = 94, sat_min = 157, sat_max = 255, val_min = 121, val_max = 223;

int min_area = 10000, max_area = 10000000;

int main() {

//namedWindow("bars", 1);

/*
createTrackbar("Hue_Min", "bars", &hue_min, 180);
createTrackbar("Hue_Max", "bars", &hue_max, 180);
createTrackbar("Sat_Min", "bars", &sat_min, 255);
createTrackbar("Sat_Max", "bars", &sat_max, 255);
createTrackbar("Val_Min", "bars", &val_min, 255);
createTrackbar("Val_Max", "bars", &val_max, 255);
*/

Mat imgRgb = imread("color.jpg");

Mat imgHsv;
cvtColor(imgRgb, imgHsv, COLOR_BGR2HSV);

for (;;) {
	Mat imgGrey;
	inRange(imgHsv, Scalar(hue_min, sat_min, val_min), Scalar(hue_max, sat_max, val_max), imgGrey);

	Mat canny;
	Canny(imgGrey, canny, 10, 100);

	vector<vector<Point	>> contours;
	vector<Vec4i> hierarchy;

	findContours(canny, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

	vector<Rect> bounders;
	
	for (int i = 0; i < contours.size(); i++) {
		int area = contourArea(contours[i]);
		if (area < max_area && area > min_area) {
			bounders.push_back(boundingRect(contours[i]));
		}
	}


	
	Mat imgBox = imgRgb.clone();
	for (int i = 0; i < bounders.size(); i++) {
		rectangle(imgBox, bounders[i], Scalar(0, 0, 255), 2);			
		circle(imgBox, Point(bounders[i].x + bounders[i].width / 2, bounders[i].y + bounders[i].height / 2), 10, Scalar(0, 0, 255), 2);
		}

	imshow("grey", imgBox);
	if (waitKey(30) == 27) break;
}

}

This depends on what you mean the “middle of two objects”. Assuming for example, you’re talking about two FRC vision targets that you are trying to center on and that you have already filtered the contours to be appropriate ratios AND have drawn the bounding rectangles for the contours, the algorithm is psuedocode would be something like this:

  1. Get the x axis value of the right edge of the left rectangle
  2. Get the x axis value of the left edge of the right rectangle
  3. Subtract 1 from 2 ( e.g. val2 - val1) to get the total distance between the two
  4. Cut the value 3 in half
  5. Subtract 4 from 2 (e.g. val2 - val4) and you’ll have the middle point of two rectangles.

Here is some basic Java code that will give you the x axis coordinate of the middle of a bounded rectangle. You can use this similar idea to calculate value 1 from the psuedocode above.

for (int i = 0; i < contours.size(); i++) {
	try {
		Rect r1 = Imgproc.boundingRect(contours.get(i));
		int rectCenter = (r1.x + r1.width / 2);
	} 
	catch (Exception e) {
	}
}