Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

mutual nearest neighbor algorithm

$
0
0
Hi!

I have to implement calculation of key point pairs, based on euclidean
distance, between points described by 128 values.
The idea is that I have the lists of points (set A, and set B )/>. First i calculate for each key point at set A its nearest neighbor in set B. Then i do this other way around. This is the function:
void FriendCalculator(ArrayList<point> ListA, ArrayList<point> ListB){
		point tempA;
		point tempB;
		
		for(int i=0;ListA.size()>i;i++){
			tempA=ListA.get(i);
			
			
			for(int j=0;ListB.size()>j;j++){
				tempB=ListB.get(j);
				float dist=0 ;
				float sum=0;
				
				for(int k=0; tempA.values.size()>k;k++){
					sum=(tempA.values.get(k)-tempB.values.get(k))*(tempA.values.get(k)-tempB.values.get(k));
					dist=sum+dist;					
				}
				dist=(float) Math.sqrt(dist);
				
				if(tempA.distance>dist || tempA.distance==0){	
					tempA.distance=dist;
					tempA.n=tempB;
				}
				else{}
				//System.out.printf("%d. Distance: %f \n",i, tempA.distance);
			}
			//System.out.printf("..\n");
			//System.out.printf("Distance: %d \n", tempA.distance);
		}
	}


Then, i search for key points pairs for which key points point at each other.
void KeyPointC(ArrayList<point> ListA,ArrayList<point> ListB,ArrayList<point> ListC){
	
		FriendCalculator(ListA,ListB);
		FriendCalculator(ListB,ListA);
		
		point tempA;
		point tempB;
		
		for(int i=0;ListA.size()>i;i++){
			tempA=ListA.get(i);
			tempB=tempA.n;
			if(tempA==tempB.n){
				System.out.printf("%d Best budies A:x:%f y:%f B:x:%f y:%f\n",i, tempA.x,tempA.y,tempA.n.x,tempA.n.y);
			}
		}
		
	}


But something is wrong, I get the result that every point from set A has an mutual nearest neighbor.
Do you have any suggestions? I'm a beginner, and there is probably some small stupid mistake.
Thank you in advance.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>