void GNMGenericNetwork::ConnectPointsByLine(GIntBig nFID, const OGRLineString* poLineString, const std::vector<OGRLayer*>& paPointLayers, double dfTolerance, double dfCost, double dfInvCost, GNMDirection eDir) { VALIDATE_POINTER0(poLineString, "GNMGenericNetwork::ConnectPointsByLine"); OGRPoint oStartPoint, oEndPoint; poLineString->StartPoint(&oStartPoint); poLineString->EndPoint(&oEndPoint); double dfHalfTolerance = dfTolerance / 2; GNMGFID nSrcFID = FindNearestPoint(&oStartPoint, paPointLayers, dfHalfTolerance); GNMGFID nTgtFID = FindNearestPoint(&oEndPoint, paPointLayers, dfHalfTolerance); if(nSrcFID == -1 || nTgtFID == -1) return; // connect nSrcFID with nTgtFID via nFID ConnectFeatures(nSrcFID, nTgtFID, (GNMGFID)nFID, dfCost, dfInvCost, eDir); }
bool CPictogramDecoder::FindBBox(int* ret, float corrThreshold) { bool cornerTempFound = false; if(corrected) return false; int r = 0; Point tlf, trf, blf, brf; cv::equalizeHist(image, eqlize); for(int row = 0; row < eqlize.rows; ++row) { unsigned char* inp = eqlize.ptr<unsigned char>(row); for (int col = 0; col < eqlize.cols; ++col) { if (*inp < 50) { *inp = 0; } else if (*inp > 255-50) { *inp = 255; } else { *inp = 128; } *inp++; } } vector<Point> foundList; vector<double> confidencesList; CPictogramDecoder::FastMatchTemplate(eqlize, tl, corrThreshold, &foundList, &confidencesList); if(foundList.size()>=4) { double conf=0; confList.resize(4); FindNearestPoint(cv::Point(0,0), &foundList, &confidencesList, &tlf, &conf); confList[0] = conf; FindNearestPoint(cv::Point(image.size().width,0), &foundList, &confidencesList, &trf, &conf); confList[1] = conf; FindNearestPoint(cv::Point(0,image.size().height), &foundList, &confidencesList, &blf, &conf); confList[2] = conf; FindNearestPoint(cv::Point(image.size().width,image.size().height), &foundList, &confidencesList, &brf, &conf); confList[3] = conf; //check the four corners are four distinct points if(tlf.x==trf.x && tlf.y==trf.y) return false; if(tlf.x==blf.x && tlf.y==blf.y) return false; if(trf.x==brf.x && trf.y==brf.y) return false; if(blf.x==brf.x && blf.y==brf.y) return false; if(tlf.x==brf.x && tlf.y==brf.y) return false; if(trf.x==blf.x && trf.y==blf.y) return false; //cv::circle(image, tlf, 15, Scalar::all(0)); //cv::circle(image, trf, 15, Scalar::all(0)); //cv::circle(image, blf, 15, Scalar::all(0)); //cv::circle(image, brf, 15, Scalar::all(0)); //printf("%d %d\n", tlf.x, tlf.y); //printf("%d %d\n", trf.x, trf.y); //printf("%d %d\n", blf.x, blf.y); //printf("%d %d\n", brf.x, brf.y); //return true; cornerTempFound = true; } //return false; if(!cornerTempFound) { return false; } cv::Mat eig_img; cv::Mat temp_img; vector<Point2f> corners; int i, max_corner_count = 150; int idx; int x; int y; float cornerThres = 0.4f; //TL Corner x = tlf.x; y = tlf.y; cv::Mat roi = image(cv::Rect(x, y, 10, 10)); cv::goodFeaturesToTrack(roi, corners, max_corner_count, cornerThres, 1); // If we find multiple corners, take the BR corner idx = 0; corners.push_back(Point(tlf.x-x, tlf.y-y)); for (i = 0; i < corners.size(); i++) { if(i==0 || (corners[i].x > corners[idx].x && corners[i].y > corners[idx].y)) { idx = i; } } if(corners.size() > 0) { bbox[0].x = corners[idx].x + x; bbox[0].y = corners[idx].y + y; cv::circle(image, bbox[0], 5, Scalar::all(0)); } //TR Corner x = trf.x-10; y = trf.y; roi = image(cv::Rect(x, y, 10, 10)); cv::goodFeaturesToTrack(roi, corners, max_corner_count, cornerThres, 1); // If we find multiple corners, take the BL corner idx=0; corners.push_back(Point(trf.x-x, trf.y-y)); for (i = 0; i < corners.size(); i++) { if(i==0 || (corners[i].x < corners[idx].x && corners[i].y > corners[idx].y)) { idx = i; } } if(corners.size() > 0) { bbox[1].x = corners[idx].x + x; bbox[1].y = corners[idx].y + y; cv::circle(image, bbox[1], 5, Scalar::all(0)); } //else // return false;//we can "guess" the point instead of returning false //BL Corner x = blf.x; y = blf.y-10; roi = image(cv::Rect(x, y, 10, 10 )); cv::goodFeaturesToTrack(roi, corners, max_corner_count, cornerThres, 1); // If we find multiple corners, take the TR corner idx=0; corners.push_back(Point(blf.x-x, blf.y-y)); for (i = 0; i < corners.size(); i++) { if(i==0 || (corners[i].x > corners[idx].x && corners[i].y < corners[idx].y)) { idx = i; } } if(corners.size() > 0) { bbox[2].x = corners[idx].x + x; bbox[2].y = corners[idx].y + y; cv::circle(image, bbox[2], 5, Scalar::all(0)); } //BR Corner x = brf.x-10; y = brf.y-10; roi = image(cv::Rect(x, y, 10, 10 )); cv::goodFeaturesToTrack(roi, corners, max_corner_count, cornerThres, 1); // If we find multiple corners, take the TL corner idx=0; corners.push_back(Point(brf.x-x, brf.y-y)); for (i = 0; i < corners.size(); i++) { if(i==0 || (corners[i].x < corners[idx].x && corners[i].y < corners[idx].y)) { idx = i; } } if(corners.size() > 0) { bbox[3].x = corners[idx].x + x; bbox[3].y = corners[idx].y + y; cv::circle(image, bbox[3], 5, Scalar::all(0)); } return true; }
bool CPictogram::FindBBox(int* ret, float corrThreshold) { if(corrected) return false; int r = 0; Point tlf, trf, blf, brf; cv::Mat eqlize; cv::equalizeHist(image, image); for(int row = 0; row < image.rows; ++row) { unsigned char* inp = image.ptr<unsigned char>(row); for (int col = 0; col < image.cols; ++col) { if (*inp < 30) { *inp = 0; } else if (*inp > 255-30) { *inp = 255; } else { *inp = 255; } *inp++; } } /* cv::Mat tlROI = image(Rect(0,0,image.size().width/2, image.size().height/2)); if(CPictogram::FastMatchTemplate(tlROI, tl, corrThreshold, &tlf.x, &tlf.y)) { //cv::circle(tlROI, tlf, 5, Scalar::all(255)); r |= 1; } cv::Mat trROI = image(Rect(image.size().width/2,0, image.size().width/2-1, image.size().height/2)); if(CPictogram::FastMatchTemplate(trROI, tr, corrThreshold, &trf.x, &trf.y)) { //cv::circle(trROI, trf, 5, Scalar::all(255)); r |= 2; } cv::Mat blROI = image(Rect(0,image.size().height/2, image.size().width/2, image.size().height/2-1)); if(CPictogram::FastMatchTemplate(blROI, bl, corrThreshold, &blf.x, &blf.y)) { //cv::circle(blROI, blf, 5, Scalar::all(255)); r |= 4; } cv::Mat brROI = image(Rect(image.size().width/2,image.size().height/2, image.size().width/2-1, image.size().height/2-1)); if(CPictogram::FastMatchTemplate(brROI, br, corrThreshold, &brf.x, &brf.y)) { //cv::circle(brROI, brf, 5, Scalar::all(255)); r |= 8; } if(ret!=0) *ret = r; if(r != (1|2|4|8)) return false;*/ vector<Point> foundPointsList; vector<double> confidencesList; CPictogram::FastMatchTemplate(image, tl, corrThreshold, &foundPointsList, &confidencesList); if(foundPointsList.size()>=4) { FindNearestPoint(cv::Point(0,0), &foundPointsList, &tlf); FindNearestPoint(cv::Point(image.size().width,0), &foundPointsList, &trf); FindNearestPoint(cv::Point(0,image.size().height), &foundPointsList, &blf); FindNearestPoint(cv::Point(image.size().width,image.size().height), &foundPointsList, &brf); cv::circle(image, tlf, 5, Scalar::all(255)); cv::circle(image, trf, 5, Scalar::all(255)); cv::circle(image, blf, 5, Scalar::all(255)); cv::circle(image, brf, 5, Scalar::all(255)); printf("%d %d\n", tlf.x, tlf.y); printf("%d %d\n", trf.x, trf.y); printf("%d %d\n", blf.x, blf.y); printf("%d %d\n", brf.x, brf.y); } //cv::Mat g; //cvtColor( image, g, CV_RGB2GRAY ); //IplImage g1 = g; //IplImage* gray = &g1; // IplImage g1 = image; // IplImage* gray = &g1; // // //TL Corner // CvRect roi = cvRect(tlf.x+1, tlf.y+1, 100, 100); // cvSetImageROI( gray, roi); // // int i, corner_count = 150; // IplImage *eig_img, *temp_img; // CvPoint2D32f *corners; // // eig_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // temp_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // corners = (CvPoint2D32f *) cvAlloc (corner_count * sizeof (CvPoint2D32f)); // // cvGoodFeaturesToTrack (gray, eig_img, temp_img, corners, &corner_count, 0.1, 15); // cvFindCornerSubPix (gray, corners, corner_count, // cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); // // // If we find multiple corners, take the TL corner // int idx = 0; // for (i = 0; i < corner_count; i++) // { // if(i==0 || (corners[i].x < corners[idx].x && corners[i].y < corners[idx].y)) // { // idx = i; // } // } // if(corner_count > 0) // { // bbox[0].x = corners[idx].x + tlf.x+1; // bbox[0].y = corners[idx].y + tlf.y+1; // //cvCircle (gray, cvPointFrom32f (corners[idx]), 3, CV_RGB (255, 0, 0), 2); // } // else // return false;//we can "guess" the point instead of returning false // // cvResetImageROI(gray); // cvReleaseImage (&eig_img); // cvReleaseImage (&temp_img); // // //TR Corner // roi = cvRect(trf.x-101, trf.y+1, 100, 100); // cvSetImageROI( gray, roi); // // corner_count = 150; // // eig_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // temp_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // // cvGoodFeaturesToTrack (gray, eig_img, temp_img, corners, &corner_count, 0.1, 15); // cvFindCornerSubPix (gray, corners, corner_count, // cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); // // // If we find multiple corners, find the TR one // idx = 0; // for (i = 0; i < corner_count; i++) // { // if(i==0 || (corners[i].x > corners[idx].x && corners[i].y < corners[idx].y)) // { // idx = i; // } // } // if(corner_count > 0) // { // bbox[1].x = corners[idx].x + trf.x-101; // bbox[1].y = corners[idx].y + trf.y+1; // //cvCircle (gray, cvPointFrom32f (corners[idx]), 3, CV_RGB (255, 0, 0), 2); // } // else // return false;//we can "guess" the point instead of returning false // // cvResetImageROI(gray); // cvReleaseImage (&eig_img); // cvReleaseImage (&temp_img); // // //BL Corner // roi = cvRect(blf.x+1, blf.y-101, 100, 100); // cvSetImageROI( gray, roi); // // corner_count = 150; // // eig_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // temp_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // // cvGoodFeaturesToTrack (gray, eig_img, temp_img, corners, &corner_count, 0.1, 15); // cvFindCornerSubPix (gray, corners, corner_count, // cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); // // // If we find multiple corners, find the BL one // idx = 0; // for (i = 0; i < corner_count; i++) // { // if(i==0 || (corners[i].x < corners[idx].x && corners[i].y > corners[idx].y)) // { // idx = i; // } // } // if(corner_count > 0) // { // bbox[2].x = corners[idx].x + blf.x+1; // bbox[2].y = corners[idx].y + blf.y-101; // //cvCircle (gray, cvPointFrom32f (corners[idx]), 3, CV_RGB (255, 0, 0), 2); // } // else // return false;//we can "guess" the point instead of returning false // // cvResetImageROI(gray); // cvReleaseImage (&eig_img); // cvReleaseImage (&temp_img); // // // //BR corner // roi = cvRect(brf.x-101, brf.y-101, 100, 100); // cvSetImageROI( gray, roi); // // corner_count = 150; // // eig_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // temp_img = cvCreateImage (cvGetSize (gray), IPL_DEPTH_32F, 1); // // cvGoodFeaturesToTrack (gray, eig_img, temp_img, corners, &corner_count, 0.1, 15); // cvFindCornerSubPix (gray, corners, corner_count, // cvSize (3, 3), cvSize (-1, -1), cvTermCriteria (CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, 0.03)); // // // If we find multiple corners, find the BR one // idx = 0; // for (i = 0; i < corner_count; i++) // { // if(i==0 || (corners[i].x > corners[idx].x && corners[i].y > corners[idx].y)) // { // idx = i; // } // } // if(corner_count > 0) // { // bbox[3].x = corners[idx].x + brf.x-101; // bbox[3].y = corners[idx].y + brf.y-101; // //cvCircle (gray, cvPointFrom32f (corners[idx]), 3, CV_RGB (255, 0, 0), 2); // } // else // return false;//we can "guess" the point instead of returning false // // //cvReleaseImage (&gray); // cvReleaseImage (&eig_img); // cvReleaseImage (&temp_img); // cvFree(&corners); return true; }