OpenCV: BoundingRect als einzelnes Bild extrahieren.



  • Ich erstelle mehrere boundingRect()s in meinem Bild und würde gerne alle zu einem einzelnen Bild extrahierien. Habe diesen Code, der allerdings ab der zweiten for-Schleife nicht mehr richtig funktioniert:

    //...
    for (size_t i = 0; i < lines.size(); i++) {
            Vec4i current = lines[i];
            Point pt1 = Point(current[0], current[1]);
            Point pt2 = Point(current[2], current[3]);
    
            Vec4i previous = lines[i - 1];
            Point ppt1 = Point(previous[0], previous[1]);
            Point ppt2 = Point(previous[2], previous[3]);
    
            double angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x) * 180.0 / CV_PI;
            if (angle) {
                line(cdst, pt1, pt2, Scalar(0, 0, 255), 2, CV_AA);
            }
    
            vector<Point> pt;
            vector<Mat> subregions;
            pt.push_back(Point(current[0], current[1]));
            pt.push_back(Point(current[2], current[3]));
            pt.push_back(Point(previous[0], previous[1]));
            pt.push_back(Point(previous[2], previous[3]));
    
            for (int i = 0; i < pt.size(); i++) {
    
                Rect roi = boundingRect(Mat (pt[i]));
    
                Mat mask = Mat::zeros(src2.size(), CV_8UC1);
                drawContours(mask, pt, i, Scalar(255), CV_FILLED);
                Mat contourRegion;
                Mat imageROI;
                src2.copyTo(imageROI, mask);
                contourRegion = imageROI(roi);
                // Mat maskROI = mask(roi); 
    
                subregions.push_back(contourRegion);
            }
        }
    

    Ich muss ja durch all boundingRect()s iterarien, um die einzelnen ausgeben zu können. Allerding klappt es so leider nicht. Habt ihr für mich Verbesserungsvorschläge meines Codes? 😞 😕

    Ich wäre sehr dankbar!



  • vickal93 schrieb:

    nicht mehr richtig funktioniert
    Allerding klappt es so leider nicht.

    Vec4i previous = lines[i - 1];
    was passiert hier wohl beim ersten punkt; also i = 0?

    `double angle = atan2(pt2.y - pt1.y, pt2.x - pt1.x) * 180.0 / CV_PI;

    if (angle) { ... }

    `
    if(double_wert) finde ich sehr sehr unschön.

    line(..) also ich bin zwar so und so eher ein trottel, da ich schon deine fehlerbeschreibung nicht verstanden habe, aber wieso du hier eine linie zeichnest(?), erschließt sich mir nicht. eventuell hast du vergessen, deinen code auf das wesentliche zu kürzen?

    for(size_t i;; ){ for(int i;; ){} } finde ich genau so unschön.
    und damit meine ich nicht mal int als indextyp.

    Vec4i current = lines[i];
            Point pt1 = Point(current[0], current[1]);
            Point pt2 = Point(current[2], current[3]);
    
            Vec4i previous = lines[i - 1];
            Point ppt1 = Point(previous[0], previous[1]);
            Point ppt2 = Point(previous[2], previous[3]);
    
            pt.push_back(Point(current[0], current[1]));
            pt.push_back(Point(current[2], current[3]));
            pt.push_back(Point(previous[0], previous[1]));
            pt.push_back(Point(previous[2], previous[3]))
    

    scheint mir kein idealer weg...
    wie wäre es, wenn du es erst mal ausführlicher versuchst?

    lines -> points
    dann den eigentlichen algo:
    erster punkt
    andere punkte

    was passiert am ende mit subregions ?

    bb


Anmelden zum Antworten