Page MenuHomec4science

ijm- k-nearest Neighbor
Updated 2,584 Days AgoPublic

here is an implementation in macro language for k-neasrest neighbor

// clear environment
run("Close All");
roiManager("Reset");

// open an image 
run("Blobs (25K)");
run("Invert LUT");

// Blur (denoise) and find Local Maximas
run("Gaussian Blur...", "sigma=1");
run("Find Maxima...", "noise=50 output=[Point Selection]");
// from the existing point
getSelectionCoordinates(xCoord,yCoord);
totalPoints = lengthOf(xCoord);

// specify the min and max distance
minDistance =	15 ;
maxDistance = 	100;
// and the Nbr of neighbor
k = 3;

// go throuhgh 
for (i = 0 ; i <  lengthOf(xCoord) ; i++){
	// define current point of Interest
	xCurrent = xCoord[i];
	yCurrent = yCoord[i];
	// defime an array with the size of the totalPoints
	allNeighBor = newArray(totalPoints);
	// and fill it with the maximum distance allowed
	Array.fill(allNeighBor, maxDistance);
	
	//
	for (j = 0 ; j < totalPoints ; j++ ){
		if ( i != j){
			// euclidian distance calculation
			distance = sqrt ( (pow ((xCurrent-xCoord[j]),2)) + (pow ((yCurrent-yCoord[j]),2)) );
			// check if above the minimal distance 
			if ((distance > minDistance )&&(distance < maxDistance )){
				allNeighBor[j] = distance;
			}
		}
	}
	// get the rank positions of the all the neighbor
	rankPosArr = Array.rankPositions(allNeighBor);

	// make the correspondings lines and add to the manager 
	for (jj = 0 ; jj < k ; jj++) {
		makeLine(xCurrent,yCurrent,xCoord[rankPosArr[jj]],yCoord[rankPosArr[jj]]);
		Roi.setName("P"+IJ.pad(i,5)+"-N"+IJ.pad(jj,5));
		roiManager("Add");
	}

}

roiManager("Show All without labels");
Last Author
romainGuiet
Last Edited
Oct 25 2017, 12:11

Event Timeline

romainGuiet created this document.Oct 25 2017, 11:13
romainGuiet edited the content of this document. (Show Details)
romainGuiet changed the title from K-nearest Neighbor to ijm- K-nearest Neighbor.Oct 25 2017, 12:11
romainGuiet changed the title from ijm- K-nearest Neighbor to ijm- k-nearest Neighbor.