I have finished implementing my own solution. It is not very graceful, but he is doing his job. I would be interested to know about the improvements. HoughLines2 did not always give me good results for finding line segments, and I had to deal with the threshold value for different scenarios a lot. Instead, I chose FindCountours, where I took the outlines with two elements, I should be guaranteed lines with 1 pixel. Having found the lines, I repeated them and traced them to find the rectangles.
Where the points are * CvSeq line endpoints
while(points->total>0){ if(p1.x==-1&&p1.y==-1){ cvSeqPopFront(points,&p1); cvSeqPopFront(points,&p2); } if((pos=findClosestPoint(&p1,&p2, points,maxDist))>=0){ p3 = (CvPoint*)cvGetSeqElem( points,pos ); pos2 = (pos%2==0)?pos+1:pos-1; //lines are in pairs of points p4 = (CvPoint*)cvGetSeqElem( points,pos2 ); if(isVertical(&p1,&p2) && isHorizontal(p3,p4)){ printf("found Corner %d %d\n",p2.x,p3->y); } else if(isHorizontal(&p1,&p2) && isVertical(p3,p4) ){ printf("found Corner %d %d\n",p3->x,p2.y); } memcpy(&p1,p3,sizeof(CvPoint)); memcpy(&p2,p4,sizeof(CvPoint)); cvSeqRemove(points, (pos>pos2)?pos:pos2); cvSeqRemove(points, (pos>pos2)?pos2:pos); } else { p1.x=-1; p1.y=-1; } } int findClosestPoint (CvPoint *p1, CvPoint *p2, CvSeq *points, int maxDist) { int ret = -1,i; float dist, minDist = maxDist; CvPoint* test; int (*dirTest)(CvPoint *,CvPoint *); if(isVertical(p1,p2)){ //vertical line if(p2->y > p1->y) {//going down dirTest = isBelow; } else { // going up dirTest = isAbove; } } else if (isHorizontal(p1,p2)){ //horizontal line if(p2->x > p1->x) {//going right dirTest = isRight; } else { //going left dirTest = isLeft; } } for( i = 0; i < points->total; i++ ) { test = (CvPoint*)cvGetSeqElem( points, i ); if(dirTest(p2,test)){ //only test points in the region we care about dist = sqrt(pow(test->x - p2->x,2)+pow(test->y - p2->y,2)); if(dist<minDist){ minDist = dist; ret = i; } } } return ret; } int isVertical(CvPoint *p1, CvPoint *p2){ return p1->x == p2->x; } int isHorizontal(CvPoint *p1, CvPoint *p2){ return p1->y == p2->y; } int isRight(CvPoint *pt1, CvPoint *pt2){ return pt2->x > pt1->x; } int isLeft(CvPoint *pt1, CvPoint *pt2){ return pt2->x < pt1->x; } int isBelow(CvPoint *pt1, CvPoint *pt2){ return pt2->y > pt1->y; } int isAbove(CvPoint *pt1, CvPoint *pt2){ return pt2->y < pt1->y; }