Finding directly ellipses in this image can be quite complicated. You can, however, look here for a few details and code links.
In this case, it is much easier to segment 3 colors: blue, red and yellow, find the outer contours and place them in an ellipse.
So, in this input image:

So, first convert the image to HSV, and then apply some threshold values ββto restore the masks. Using morphological closure operations, you will get rid of some holes and connect adjacent drops.
blue mask:

red mask:

yellow mask:

Then you can get the outer contours from these masks and save only the largest ones (in case you find that some smaller blob does not belong to the target).
Now you just need to pick up the ellipse for these contours:

Please note that I also tried on the image in your other question . The blue target is deformed, and therefore it is not an ellipse, so setting an ellipse here is not a good choice:

In this case, it is better to use the convex hull of the contours, which will be more reliable than the contour itself if the mask is not perfect (the code is indicated below):

code:
#include <opencv2/opencv.hpp> #include <vector> #include <string> using namespace std; using namespace cv; int main() { // Load image Mat3b img = imread("path_to_image"); // Convert to hsv Mat3b hsv; cvtColor(img, hsv, COLOR_BGR2HSV); // Find masks for different colors Mat1b blue_mask; inRange(hsv, Scalar(90, 150, 150), Scalar(110, 255, 255), blue_mask); Mat1b red_mask; inRange(hsv, Scalar(160, 150, 100), Scalar(180, 255, 255), red_mask); Mat1b yellow_mask; inRange(hsv, Scalar(20, 150, 100), Scalar(30, 255, 255), yellow_mask); // Apply morphological close Mat1b kernel = getStructuringElement(MORPH_ELLIPSE, Size(11,11)); morphologyEx(blue_mask, blue_mask, MORPH_CLOSE, kernel); morphologyEx(red_mask, red_mask, MORPH_CLOSE, kernel); morphologyEx(yellow_mask, yellow_mask, MORPH_CLOSE, kernel); // Find largest blob and draw it vector<Point> blue_contour, red_contour, yellow_contour; { vector<vector<Point>> contours; findContours(blue_mask.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); blue_contour = *max_element(contours.begin(), contours.end(), [](const vector<Point>& lhs, const vector<Point>& rhs){ return contourArea(lhs) < contourArea(rhs); }); } { vector<vector<Point>> contours; findContours(red_mask.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); red_contour = *max_element(contours.begin(), contours.end(), [](const vector<Point>& lhs, const vector<Point>& rhs){ return contourArea(lhs) < contourArea(rhs); }); } { vector<vector<Point>> contours; findContours(yellow_mask.clone(), contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); yellow_contour = *max_element(contours.begin(), contours.end(), [](const vector<Point>& lhs, const vector<Point>& rhs){ return contourArea(lhs) < contourArea(rhs); }); } // Fit ellipse RotatedRect blue_ellipse = fitEllipse(blue_contour); RotatedRect red_ellipse = fitEllipse(red_contour); RotatedRect yellow_ellipse = fitEllipse(yellow_contour); // Draw ellipses ellipse(img, blue_ellipse, Scalar(255, 0, 0), 3); ellipse(img, red_ellipse, Scalar(0, 0, 255), 3); ellipse(img, yellow_ellipse, Scalar(0, 255, 255), 3); imshow("Result", img); waitKey(); return 0; }
Code for convex hull: