Associate features between the two views. Then compute a homography and essential matrix using LSMed. Add
features to the edge if they an inlier in essential. Save fit score of homography vs essential.
334008
protected boolean connectViews(PairwiseImageGraph.View viewA , PairwiseImageGraph.View viewB ,
								   FastQueue<AssociatedIndex> matches) {

		// Estimate fundamental/essential with RANSAC
		PairwiseImageGraph.Motion edge = new PairwiseImageGraph.Motion();
		int inliersEpipolar;

		CameraPinhole pinhole0 = viewA.camera.pinhole;
		CameraPinhole pinhole1 = viewB.camera.pinhole;

		if( pinhole0 != null && pinhole1 != null ) {
			// Fully calibrated camera pair
			ransacEssential.setIntrinsic(0,pinhole0);
			ransacEssential.setIntrinsic(1,pinhole1);

			if( !fitEpipolar(matches, viewA.observationNorm.toList(), viewB.observationNorm.toList(),ransacEssential,edge) ) {
				if( verbose != null && verboseLevel >= 1 ) {
					verbose.println(" fit essential failed");
				}
				return false;
			}
			edge.metric = true;
			inliersEpipolar = ransacEssential.getMatchSet().size();
			edge.F.set(ransacEssential.getModelParameters());
		} else if( fitEpipolar(matches,
					viewA.observationPixels.toList(), viewB.observationPixels.toList(),
					ransacFundamental,edge) ) {
			// transform is only known up to a projective transform
			edge.metric = false;
			inliersEpipolar = ransacFundamental.getMatchSet().size();
			edge.F.set(ransacFundamental.getModelParameters());
		} else {
			if( verbose != null && verboseLevel >= 1 ) {
				verbose.println(" fit fundamental failed");
			}
			return false;
		}

		if( inliersEpipolar < MIN_FEATURE_ASSOCIATED ) {
			if( verbose != null && verboseLevel >= 1 ) {
				verbose.println(" too too few inliers. "+inliersEpipolar+" min="+MIN_FEATURE_ASSOCIATED+
						" obsA="+viewA.observationNorm.size+" obsB="+viewB.observationNorm.size);
			}
			return false;
		}

		// If only a very small number of features are associated do not consider the view
		double fractionA = inliersEpipolar/(double)viewA.descriptions.size;
		double fractionB = inliersEpipolar/(double)viewB.descriptions.size;

		if( fractionA < MIN_ASSOCIATE_FRACTION | fractionB < MIN_ASSOCIATE_FRACTION )
			return false;

		// If the geometry is good for triangulation this number will be lower
		edge.viewSrc = viewA;
		edge.viewDst = viewB;
		edge.index = graph.edges.size();
		viewA.connections.add(edge);
		viewB.connections.add(edge);
		graph.edges.add(edge);

		return true;
	}
55262
def _augment_network(self, edge_dict):
        
        for (vertex0, vertex1), feature_list in edge_dict.items():
            edge_obj = Edge(vertex0, vertex1, feature_list)
            self.edges[(vertex0, vertex1)] = edge_obj
            self._add_edge_to_vertex(vertex0, edge_obj)
            self._add_edge_to_vertex(vertex1, edge_obj)
1179340
public boolean createTransform( Point2D_F64 tl , Point2D_F64 tr ,
									Point2D_F64 br , Point2D_F64 bl )
	{
		associatedPairs.get(0).p2.set(tl);
		associatedPairs.get(1).p2.set(tr);
		associatedPairs.get(2).p2.set(br);
		associatedPairs.get(3).p2.set(bl);

		if( !computeHomography.process(associatedPairs, H) )
			return false;

//		if( !refineHomography.fitModel(associatedPairs,H,Hrefined) ) {
//			return false;
//		}
//		homography.set(Hrefined);

		ConvertMatrixData.convert(H,H32);
		transform.set(H32);

		return true;
	}
364802
private void associateL2R( T left , T right ) {
		// make the previous new observations into the new old ones
		ImageInfo<TD> tmp = featsLeft1;
		featsLeft1 = featsLeft0; featsLeft0 = tmp;
		tmp = featsRight1;
		featsRight1 = featsRight0; featsRight0 = tmp;

		// detect and associate features in the two images
		featsLeft1.reset();
		featsRight1.reset();

//		long time0 = System.currentTimeMillis();
		describeImage(left,featsLeft1);
		describeImage(right,featsRight1);
//		long time1 = System.currentTimeMillis();

		// detect and associate features in the current stereo pair
		for( int i = 0; i < detector.getNumberOfSets(); i++ ) {
			SetMatches matches = setMatches[i];
			matches.swap();
			matches.match2to3.reset();

			FastQueue<Point2D_F64> leftLoc = featsLeft1.location[i];
			FastQueue<Point2D_F64> rightLoc = featsRight1.location[i];

			assocL2R.setSource(leftLoc,featsLeft1.description[i]);
			assocL2R.setDestination(rightLoc, featsRight1.description[i]);
			assocL2R.associate();

			FastQueue<AssociatedIndex> found = assocL2R.getMatches();

//			removeUnassociated(leftLoc,featsLeft1.description[i],rightLoc,featsRight1.description[i],found);
			setMatches(matches.match2to3, found, leftLoc.size);
		}
//		long time2 = System.currentTimeMillis();
//		System.out.println("  desc "+(time1-time0)+" assoc "+(time2-time1));
	}
468967
def merge_and_fit(self, track, pairings):
        

        for (self_seg_index, track_seg_index, _) in pairings:
            self_s = self.segments[self_seg_index]
            ss_start = self_s.points[0]
            track_s = track.segments[track_seg_index]
            tt_start = track_s.points[0]
            tt_end = track_s.points[-1]

            d_start = ss_start.distance(tt_start)
            d_end = ss_start.distance(tt_end)

            if d_start > d_end:
                track_s = track_s.copy()
                track_s.points = list(reversed(track_s.points))

            self_s.merge_and_fit(track_s)
        return self
826864
public void convertIntoBundleStructure(List<Se3_F64> motions,
										   DMatrixRMaj K,
										   double[] distort,
										   List<CalibrationObservation> obs ) {

		structure = new SceneStructureMetric(false);
		observations = new SceneObservations(motions.size(),true);

		structure.initialize(1,motions.size(),layout.size(),1);

		// A single camera is assumed, that's what is being calibrated!
		structure.setCamera(0,false,cameraGenerator.initalizeCamera(K,distort));
		// A single rigid planar target is being viewed. It is assumed to be centered at the origin
		structure.setRigid(0,true,new Se3_F64(),layout.size());
		// Where the points are on the calibration target
		SceneStructureMetric.Rigid rigid = structure.rigids[0];
		for (int i = 0; i < layout.size(); i++) {
			rigid.setPoint(i,layout.get(i).x,layout.get(i).y,0);
		}

		// Add the initial estimate of each view's location and the points observed
		for (int viewIdx = 0; viewIdx < motions.size(); viewIdx++) {
			structure.setView(viewIdx,false,motions.get(viewIdx));
			SceneObservations.View v = observations.getViewRigid(viewIdx);
			structure.connectViewToCamera(viewIdx,0);
			CalibrationObservation ca = obs.get(viewIdx);
			for (int j = 0; j < ca.size(); j++) {
				PointIndex2D_F64 p = ca.get(j);
				v.add(p.index, (float)p.x, (float)p.y);
				structure.connectPointToView(p.index,viewIdx);
			}
		}
	}
1003151
boolean findRemainingCameraMatrices(LookupSimilarImages db, View seed, GrowQueue_I32 motions) {
		points3D.reset(); // points in 3D
		for (int i = 0; i < structure.points.length; i++) {
			structure.points[i].get(points3D.grow());
		}
		// contains associated pairs of pixel observations
		// save a call to db by using the previously loaded points
		assocPixel.reset();
		for (int i = 0; i < inlierToSeed.size; i++) {
			// inliers from triple RANSAC
			// each of these inliers was declared a feature in the world reference frame
			assocPixel.grow().p1.set(matchesTriple.get(i).p1);
		}

		DMatrixRMaj cameraMatrix = new DMatrixRMaj(3,4);
		for (int motionIdx = 0; motionIdx < motions.size; motionIdx++) {
			// skip views already in the scene's structure
			if( motionIdx == selectedTriple[0] || motionIdx == selectedTriple[1])
				continue;
			int connectionIdx = motions.get(motionIdx);
			Motion edge = seed.connections.get(connectionIdx);
			View viewI = edge.other(seed);

			// Lookup pixel locations of features in the connected view
			db.lookupPixelFeats(viewI.id,featsB);

			if ( !computeCameraMatrix(seed, edge,featsB,cameraMatrix) ) {
				if( verbose != null ) {
					verbose.println("Pose estimator failed! motionIdx="+motionIdx);
				}
				return false;
			}
			db.lookupShape(edge.other(seed).id,shape);
			structure.setView(motionIdx,false,cameraMatrix,shape.width,shape.height);

		}
		return true;
	}
509600
public static void drawInliers(BufferedImage left, BufferedImage right, CameraPinholeBrown intrinsic,
								   List<AssociatedPair> normalized) {
		Point2Transform2_F64 n_to_p = LensDistortionFactory.narrow(intrinsic).distort_F64(false,true);

		List<AssociatedPair> pixels = new ArrayList<>();

		for (AssociatedPair n : normalized) {
			AssociatedPair p = new AssociatedPair();

			n_to_p.compute(n.p1.x, n.p1.y, p.p1);
			n_to_p.compute(n.p2.x, n.p2.y, p.p2);

			pixels.add(p);
		}

		// display the results
		AssociationPanel panel = new AssociationPanel(20);
		panel.setAssociation(pixels);
		panel.setImages(left, right);

		ShowImages.showWindow(panel, "Inlier Features", true);
	}
1128543
def add_alignment_patterns(matrix, version):
    
    if version < 2:
        return
    matrix_size = len(matrix)
    positions = consts.ALIGNMENT_POS[version - 2]
    for pos_x in positions:
        for pos_y in positions:
            # Finder pattern?
            if pos_x - 6 == 0 and (pos_y - 6 == 0 or pos_y + 7 == matrix_size) \
                    or pos_y - 6 == 0 and pos_x + 7 == matrix_size:
                continue
            row, col = pos_x - 2, pos_y - 2
            for r in range(5):
                matrix[row + r][col:col+5] = _ALIGNMENT_PATTERN[r]
498391
def fill_view(self, view):
        
        other = view.hist
        _other_x_center = other.axis(0).GetBinCenter
        _other_y_center = other.axis(1).GetBinCenter
        _other_z_center = other.axis(2).GetBinCenter
        _other_get = other.GetBinContent
        _other_get_bin = super(_HistBase, other).GetBin
        other_sum_w2 = other.GetSumw2()
        _other_sum_w2_at = other_sum_w2.At

        _find = self.FindBin
        sum_w2 = self.GetSumw2()
        _sum_w2_at = sum_w2.At
        _sum_w2_setat = sum_w2.SetAt
        _set = self.SetBinContent
        _get = self.GetBinContent

        for x, y, z in view.points:
            idx = _find(
                _other_x_center(x),
                _other_y_center(y),
                _other_z_center(z))
            other_idx = _other_get_bin(x, y, z)
            _set(idx, _get(idx) + _other_get(other_idx))
            _sum_w2_setat(
                _sum_w2_at(idx) + _other_sum_w2_at(other_idx),
                idx)
774039
def similarity(
        M_c, X_L_list, X_D_list, given_row_id, target_row_id,
        target_column=None):
    
    score = 0.0

    # Set col_idxs: defaults to all columns.
    if target_column:
        if type(target_column) == str:
            col_idxs = [M_c['name_to_idx'][target_column]]
        elif type(target_column) == list:
            col_idxs = target_column
        else:
            col_idxs = [target_column]
    else:
        col_idxs = M_c['idx_to_name'].keys()
    col_idxs = [int(col_idx) for col_idx in col_idxs]

    ## Iterate over all latent states.
    for X_L, X_D in zip(X_L_list, X_D_list):
        for col_idx in col_idxs:
            view_idx = X_L['column_partition']['assignments'][col_idx]
            if X_D[view_idx][given_row_id] == X_D[view_idx][target_row_id]:
                score += 1.0

    return score / (len(X_L_list)*len(col_idxs))
89254
void setPerspShadowMatrix(Matrix4f modelMtx, GVRLight light)
    {
        GVRPerspectiveCamera camera = (GVRPerspectiveCamera) getCamera();

        if (camera == null)
        {
            return;
        }
        float angle = light.getFloat("outer_cone_angle");
        float near = camera.getNearClippingDistance();
        float far = camera.getFarClippingDistance();

        angle = (float) Math.acos(angle) * 2.0f;
        modelMtx.invert();
        modelMtx.get(mTempMtx);
        camera.setViewMatrix(mTempMtx);
        camera.setFovY((float) Math.toDegrees(angle));
        mShadowMatrix.setPerspective(angle, 1.0f, near, far);
        mShadowMatrix.mul(modelMtx);
        sBiasMatrix.mul(mShadowMatrix, mShadowMatrix);
        mShadowMatrix.getColumn(0, mTemp);
        light.setVec4("sm0", mTemp.x, mTemp.y, mTemp.z, mTemp.w);
        mShadowMatrix.getColumn(1, mTemp);
        light.setVec4("sm1", mTemp.x, mTemp.y, mTemp.z, mTemp.w);
        mShadowMatrix.getColumn(2, mTemp);
        light.setVec4("sm2", mTemp.x, mTemp.y, mTemp.z, mTemp.w);
        mShadowMatrix.getColumn(3, mTemp);
        light.setVec4("sm3", mTemp.x, mTemp.y, mTemp.z, mTemp.w);
    }
1200945
private void associateToDetected( List<CombinedTrack<TD>> known ) {
		// initialize data structures
		detectedDesc.reset();
		knownDesc.reset();

		// create a list of detected feature descriptions
		int N = detector.getNumberOfFeatures();
		for( int i = 0; i < N; i++ ) {
			detectedDesc.add(detector.getDescription(i));
		}

		// create a list of previously created track descriptions
		for( CombinedTrack<TD> t : known ) {
			knownDesc.add(t.desc);
		}

		// associate features
		associate.setSource(knownDesc);
		associate.setDestination(detectedDesc);
		associate.associate();

		N = Math.max(known.size(),detector.getNumberOfFeatures());
		if( associated.length < N )
			associated = new boolean[N];
	}
869554
public void decompose( DMatrixRMaj homography ) {
		if( !svd.decompose(homography) )
			throw new RuntimeException("SVD failed somehow");

		DMatrixRMaj V = svd.getV(null,false);
		DMatrixRMaj S = svd.getW(null);

		SingularOps_DDRM.descendingOrder(null,false,S, V,false);

		double s0 = S.get(0,0);
		double s1 = S.get(1,1); // This is the scale. If normalize it will be one
		double s2 = S.get(2,2);

		// force s1 to be 1
		s0 /= s1;
		s2 /= s1;
		CommonOps_DDRM.scale(1.0/s1,homography,this.H);

		// square s0 and s1 for other parts of the calculations
		s0 *= s0;
		s2 *= s2;

		v2.set(V.get(0,1),V.get(1,1),V.get(2,1));

		double a = Math.sqrt(1-s2);
		double b = Math.sqrt(s0-1);
		double div = Math.sqrt(s0-s2);

		for( int i = 0; i < 3; i++ ) {
			double e1 = (a*V.get(i,0) + b*V.get(i,2))/div;
			double e2 = (a*V.get(i,0) - b*V.get(i,2))/div;

			u1.setIdx(i,e1);
			u2.setIdx(i,e2);
		}

		setU(U1, v2, u1);
		setU(U2, v2, u2);
		setW(W1, H , v2, u1);
		setW(W2, H , v2, u2);

		// create the four solutions
		createSolution(W1, U1, u1,H,solutionsSE.get(0), solutionsN.get(0));
		createSolution(W2, U2, u2,H,solutionsSE.get(1), solutionsN.get(1));
		createMirrorSolution(0,2);
		createMirrorSolution(1,3);
	}
7858
private void computeSolutions(DMatrix4x4 Q) {
		DMatrixRMaj w_i = new DMatrixRMaj(3,3);

		for (int i = 0; i < cameras.size; i++) {
			computeW(cameras.get(i),Q,w_i);
			Intrinsic calib = solveForCalibration(w_i);
			if( sanityCheck(calib)) {
				solutions.add(calib);
			}
		}
	}
600100
public static<T extends ImageGray<T>, FD extends TupleDesc> Homography2D_F64
	computeTransform( T imageA , T imageB ,
					  DetectDescribePoint<T,FD> detDesc ,
					  AssociateDescription<FD> associate ,
					  ModelMatcher<Homography2D_F64,AssociatedPair> modelMatcher )
	{
		// get the length of the description
		List<Point2D_F64> pointsA = new ArrayList<>();
		FastQueue<FD> descA = UtilFeature.createQueue(detDesc,100);
		List<Point2D_F64> pointsB = new ArrayList<>();
		FastQueue<FD> descB = UtilFeature.createQueue(detDesc,100);

		// extract feature locations and descriptions from each image
		describeImage(imageA, detDesc, pointsA, descA);
		describeImage(imageB, detDesc, pointsB, descB);

		// Associate features between the two images
		associate.setSource(descA);
		associate.setDestination(descB);
		associate.associate();

		// create a list of AssociatedPairs that tell the model matcher how a feature moved
		FastQueue<AssociatedIndex> matches = associate.getMatches();
		List<AssociatedPair> pairs = new ArrayList<>();

		for( int i = 0; i < matches.size(); i++ ) {
			AssociatedIndex match = matches.get(i);

			Point2D_F64 a = pointsA.get(match.src);
			Point2D_F64 b = pointsB.get(match.dst);

			pairs.add( new AssociatedPair(a,b,false));
		}

		// find the best fit model to describe the change between these images
		if( !modelMatcher.process(pairs) )
			throw new RuntimeException("Model Matcher failed!");

		// return the found image transform
		return modelMatcher.getModelParameters().copy();
	}
899867
def add_edge(edges, edge_points, coords, i, j):
    
    if (i, j) in edges or (j, i) in edges:
        # already added
        return( edges.add((i, j)), edge_points.append(coords[[i, j]]))