# How do I create the best object finding mask?

**URL:** <https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401>\
**Category:** Uncategorized\
**Tags:** imgproc\
**Created:** [February 7, 2021, 5:53pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401 "2021-02-07T17:53:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Std](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/std/32/937_2.png) [@Std](https://forum.opencv.org/u/Std)\
**Post date:** [February 7, 2021, 5:53pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401/1 "2021-02-07T17:53:46Z")

</div>

Hello. Please tell me if it is possible to somehow improve this code for finding the object mask? I need to find an object of an arbitrary shape on a white background, but it does it incorrectly. Any information would be helpful. Thanks!

```
 struct comparator{
    bool operator() (std::tuple<std::vector<cv::Point>, bool, double> t1,
                     std::tuple<std::vector<cv::Point>, bool, double> t2) {
        return std::get<2>(t1) > std::get<2>(t2);
    }
} comparator;

int main(int, char**)
{
    // get image
    cv::Mat image = cv::imread("C:\\Documents\\2.png");
    cv::Mat grayImg;

    // convert to greyscale
    cv::cvtColor(image, grayImg, COLOR_BGRA2GRAY);

// cv::Mat canny;
// cv::Canny(grayImg,canny, 120, 255, 3);

    // finding threshes
    cv::Mat thresh;
    cv::threshold(grayImg,thresh, 245, 255, THRESH_BINARY_INV);

    // finding contours
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;

    findContours( thresh, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0) );

    // finding max contour
    std::vector<std::tuple<std::vector<cv::Point>, bool, double>> vec;

    for(size_t i = 0; i < contours.size(); ++i){

        vec.push_back(std::make_tuple(contours.at(i), cv::isContourConvex(contours.at(i)),cv::contourArea(contours.at(i))));
    }

    std::sort(vec.begin(), vec.end(), comparator);

    std::tuple<std::vector<cv::Point>, bool, double> maxContour;
    maxContour = vec.at(0);

    // create mask
    cv::Mat mask = Mat::zeros(thresh.size(), CV_8U);

    cv::fillConvexPoly(mask, std::get<0>(maxContour), Scalar(255,0,0),8,0);

    // bitwise
    cv::Mat res;
    cv::bitwise_and(image, image, res, mask);

    // create transparent background
    Mat dst;

    Mat rgb[3];
    split(image,rgb);

    Mat rgba[4]={rgb[0],rgb[1],rgb[2], thresh};
    merge(rgba,4,dst);

    return 0;

}

```

Here is the output of the program. Unfortunately, I can only upload one image.

 ![res](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/6bd7e40bcaa571242ed812e828e95c4bd400b290.png)

---

<div class="post-metadata">

**Author:** ![naser\_piltan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/naser_piltan/32/391_2.png) [@naser\_piltan](https://forum.opencv.org/u/naser_piltan)\
**Post date:** [February 7, 2021, 5:59pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401/2 "2021-02-07T17:59:41Z")

</div>

Whats your actual image ? Can you show it us ?

---

<div class="post-metadata">

**Author:** ![Std](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/std/32/937_2.png) [@Std](https://forum.opencv.org/u/Std)\
**Post date:** [February 7, 2021, 6:01pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401/3 "2021-02-07T18:01:45Z")

</div>

![1](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/1X/006a05f147fcf1fe3edc64516191677228132100.png)

---

<div class="post-metadata">

**Author:** ![Std](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/std/32/937_2.png) [@Std](https://forum.opencv.org/u/Std)\
**Post date:** [February 7, 2021, 6:02pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401/4 "2021-02-07T18:02:27Z")

</div>

This is the original image. That’s an example.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 7, 2021, 8:05pm UTC](https://forum.opencv.org/t/how-do-i-create-the-best-object-finding-mask/1401/5 "2021-02-07T20:05:37Z")

</div>

related

> <https://stackoverflow.com/questions/66088568/how-to-cut-an-object-from-a-white-background>
