bool Matrix::getROI(QList<cv::Point> &massCenters) {
    cv::Mat src_copy = this->clone();
    unsigned int elementBig = 0;
    unsigned int elementSmall = 0;

    if (cols > 1500) {
        elementBig = 51;
        elementSmall = 15;
    }
    else if (cols > 1200) {
        elementBig = 31;
        elementSmall = 14;
    }
    else if (cols > 800) {
        elementBig = 21;
        elementSmall = 8;
    }
    else {
        elementBig = 12;
        elementSmall = 4;
    }

    cv::Mat elementRectBig = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(elementBig,elementBig));
    cv::Mat elementRectSmall = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(elementSmall,elementSmall));

    Histogram h;
    cv::MatND histValues = h.getHistogramValues(src_copy,128);

    QList<QPoint> histMaxList;
    QPoint histMax(0,0);
    QPoint firstValley(0,0);
    for(int i = 0; i < 50; i++) {                   //detecting global maximum [0-50]
        int value = histValues.at<float>(i);
        if (value > histMax.y()) {
            histMax.setX(i);
            histMax.setY(value);
            firstValley.setX(i);
            firstValley.setY(value);
        }
    }

    int prev4 = 0;
    int prev3 = 0;
    int prev2 = 0;
    int prev1 = 0;
    int next1 = 0;
    int next2 = 0;
    int next3 = 0;
    int next4 = 0;
    for(int i = 0; i < 50; i++) {           //detecting local maxima
        prev4 = (i<4) ? 0 : histValues.at<float>(i-4);
        prev3 = (i<3) ? 0 : histValues.at<float>(i-3);
        prev2 = (i<2) ? 0 : histValues.at<float>(i-2);
        prev1 = (i<1) ? 0 : histValues.at<float>(i-1);
        next1 = (i>49) ? 0 : histValues.at<float>(i+1);
        next2 = (i>48) ? 0 : histValues.at<float>(i+2);
        next3 = (i>47) ? 0 : histValues.at<float>(i+3);
        next4 = (i>46) ? 0 : histValues.at<float>(i+4);
        int currentValue = histValues.at<float>(i);
        if (currentValue > prev4
                && currentValue > prev3
                && currentValue >= prev2
                && currentValue >= prev1
                && currentValue >= next1
                && currentValue >= next2
                && currentValue > next3
                && currentValue > next4) {
            histMaxList.append(QPoint(i,currentValue));
        }
    }
    for (int i = 0; i < histMaxList.size();) {    //remove too small local maxima
        if(histMaxList[i].y() < histMax.y()/10) {
            histMaxList.removeAt(i);
        }
        else {
            i++;
        }
    }

    if (histMaxList.empty()) {
        histMaxList.append(histMax);
    }
    for (int i = histMaxList[0].x(); i < histMaxList[0].x()+8; i++) {
        int currentValue = histValues.at<float>(i);
        if (currentValue < firstValley.y()) {
            firstValley.setX(i);
            firstValley.setY(currentValue);
        }
    }

    cv::threshold(src_copy, src_copy, firstValley.x()*2, 255, CV_THRESH_BINARY_INV);
    cv::morphologyEx(src_copy, src_copy, cv::MORPH_OPEN, elementRectSmall);        //shoud be depending on radius
    cv::morphologyEx(src_copy, src_copy, cv::MORPH_CLOSE, elementRectBig);

    cv::namedWindow("contours", CV_WINDOW_NORMAL);
    cv::imshow("contours", src_copy);

    std::vector<std::vector<cv::Point> > contours;
    std::vector<cv::Vec4i> Hierarchy;

    /// Find contours
    cv::findContours(src_copy, contours, Hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cv::Point(0,0));
    qSort(contours.begin(), contours.end(), sortFunction);   //areas nach größe sortieren [groß -> klein]

    double areaLimit = static_cast<double>(3000)*rows*cols/(static_cast<double>(1920*1080));
    for (unsigned int i = 0; i < contours.size(); i++) {
        int area = cv::contourArea(contours[i]);
        if (area < static_cast<int>(areaLimit)) {                      //should be depending on radius and resolution
            contours.erase(contours.begin()+i);
        }
        else {
            i++;
        }
    }

    if (contours.empty()) {
        return false;               //Fehler, da gefundene areas zu klein -> keine Motordaten stellen
    }

    massCenters.clear();
    getMassCenters(contours, massCenters);
    cv::circle(*this, cv::Point(massCenters[0].x,massCenters[0].y), 5, cv::Scalar(120), CV_FILLED);
    return true;
}