...



  • ...



  • stell bitte noch ein Testbild und den gesamten Code zur Verfügung sodass man das nur mehr mit Copy und Paste in die IDE einfügen muss, dann kann ichs mir ansehen.



  • Alles klar, wird sofort gemacht 😉



  • Ausgabe: http://www.bilder-upload.eu/show.php?file=c492af-1463141991.jpg

    sowie die 2d Koordinaten der Punkte auf der Kommandozeile:

    Point: [773, 124]
    Point: [634, 131]
    Point: [644, 405]
    Point: [783, 403]
    

    Code:
    Bild einlesen -> auf grau konvertieren -> Schwellwert nach Otsu anwenden -> Binärbild -> Rauschen entfernen -> im Binärbild die Konturen suchen -> ein Polygon auf die Kontur legen -> Ausgabe des Polygons.

    #include <opencv2/opencv.hpp>
    #include <vector>
    #include <iostream>
    #include <ctime>
    
    void show(const char* title, const cv::Mat& img)
    {
    	cv::imshow(title, img);
    	cv::waitKey();
    }
    
    int main()
    {
    	cv::Mat imgColor = cv::imread("xxx.jpg", CV_LOAD_IMAGE_COLOR);
    
    	// rgb -> gray
    	cv::Mat imgGray;
    	cvtColor(imgColor, imgGray, CV_BGR2GRAY);
    
    	// show
    	show("gray", imgGray);
    
    	// gray -> binary
    	cv::Mat imgBW;
    	cv::threshold(imgGray, imgBW, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
    
    	// remove noise
    	cv::Mat se = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));	
    	cv::erode(imgBW, imgBW, se);
    	cv::dilate(imgBW, imgBW, se);
    
    	// show 
    	show("bw", imgBW);
    
    	// Find contours
    	std::vector<std::vector<cv::Point> > contours;
    	std::vector<cv::Vec4i> hierarchy;
    	cv::findContours(imgBW, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    
    	// take first contour (TODO: maybe search for contour with biggest area?)
    	if (contours.size() == 0)
    	{
    		std::cout << "NO CONTOUR FOUND!!!\n";
    		return 0;
    	}
    
    	// fit polygon with specified error onto contour
    	std::vector<cv::Point> polygonPoints;
    	double precission = 10.0;
    	cv::approxPolyDP(cv::Mat(contours[0]), polygonPoints, precission, true);
    
    	// draw polygon lines 
    	cv::Mat imgOut;
    	imgColor.copyTo(imgOut);
    	const size_t numPolygonPoints = polygonPoints.size();
    	cv::RNG rng(time(0));
    	for (size_t i = 0; i < numPolygonPoints; ++i)
    	{
    		std::cout << "Point: "<<polygonPoints[i] << "\n";
    		cv::line(imgOut, polygonPoints[i], polygonPoints[(i + 1) % numPolygonPoints], cv::Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), 5);
    	}
    
    	// show
    	show("output", imgOut);
    	cv::imwrite("out.jpg",imgOut);
    
    	return 0;
    }
    


  • Vielen vielen Dank für die Hilfe! Das ist perfekt und bringt mich einen riesigen Schritt weiter und raus aus meiner Verzweiflung! Danke! 😃 😃 😃 😃


Anmelden zum Antworten