示例#1
0
/*******************************************************************************
Find matching interest points between images.
    image1 - first input image
    interestPts1 - interest points corresponding to image 1
    numInterestsPts1 - number of interest points in image 1
    image2 - second input image
    interestPts2 - interest points corresponding to image 2
    numInterestsPts2 - number of interest points in image 2
    matches - set of matching points to be returned
    numMatches - number of matching points returned
    image1Display - image used to display matches
    image2Display - image used to display matches
*******************************************************************************/
void MainWindow::MatchInterestPoints(QImage image1, CIntPt *interestPts1, int numInterestsPts1,
                             QImage image2, CIntPt *interestPts2, int numInterestsPts2,
                             CMatches **matches, int &numMatches, QImage &image1Display, QImage &image2Display)
{

    // Compute the descriptors for each interest point.
    // You can access the descriptor for each interest point using interestPts1[i].m_Desc[j].
    // If interestPts1[i].m_DescSize = 0, it was not able to compute a descriptor for that point
    ComputeDescriptors(image1, interestPts1, numInterestsPts1);
    ComputeDescriptors(image2, interestPts2, numInterestsPts2);

    
    *matches = new CMatches[numInterestsPts1];

    int x, y, z;
    numMatches = 0;

    // Compute best match from image 2 for each interest point in image 1
    // Best matching point has the smallest norm distance
    for(x = 0; x < numInterestsPts1; x++) {
        // Check if descriptor valid
        if(interestPts1[x].m_DescSize > 0) {
            CIntPt pt1 = interestPts1[x];
            int min = 0;
            double mindist = INFINITY;

            for(y = 0; y < numInterestsPts2; y++) {
                // Check if descriptor valid
                if(interestPts2[y].m_DescSize > 0) {
                    CIntPt pt2 = interestPts2[y];
                    double dist = 0.0;
                    for(z = 0; z < DESC_SIZE; z++) {
                        dist += pow(pt1.m_Desc[z] - pt2.m_Desc[z], 2);
                    }
                    if(dist < mindist) {
                        mindist = dist;
                        min = y;
                    }
                }
            }

            // Store matching points
            (*matches)[numMatches].m_X1 = interestPts1[x].m_X;
            (*matches)[numMatches].m_Y1 = interestPts1[x].m_Y;
            (*matches)[numMatches].m_X2 = interestPts2[min].m_X;
            (*matches)[numMatches].m_Y2 = interestPts2[min].m_Y;
            numMatches++;
        }
    }


    // The position of the interest point in iamge 1 is (m_X1, m_Y1)

    // Draw the matches
    DrawMatches(*matches, numMatches, image1Display, image2Display);
}
/*******************************************************************************
Find matching interest points between images.

    image1: first input image
    interestPts1: interest points corresponding to image 1
    numInterestPts1: number of interest points in image 1
    image2: second input image
    interestPts2: interest points corresponding to image 2
    numInterestPts2: number of interest points in image 2
    matches: set of matching points to be returned
    numMatches: number of matching points returned
    image1Display: image used to display matches
    image2Display: image used to display matches
*******************************************************************************/
void MainWindow::MatchInterestPoints(QImage image1, CIntPt *interestPts1, int numInterestPts1,
                             QImage image2, CIntPt *interestPts2, int numInterestPts2,
                             CMatches **matches, int &numMatches, QImage &image1Display, QImage &image2Display)
{
	
	numMatches = numInterestPts1;

    // Compute the descriptors for each interest point.
    // You can access the descriptor for each interest point using interestPts1[i].m_Desc[j].
    // If interestPts1[i].m_DescSize = 0, it was not able to compute a descriptor for that point
    ComputeDescriptors(image1, interestPts1, numInterestPts1);
    ComputeDescriptors(image2, interestPts2, numInterestPts2);

    // The position of the interest point in image 1 is (m_X1, m_Y1)
    // The position of the interest point in image 2 is (m_X2, m_Y2)
	
	*matches = new CMatches[numMatches];

	double minL2distance;
	int matchNo = 0;
	int attemptNo = 0;
	int rejectNo = 0;

	for(int image1pt = 0; image1pt < numInterestPts1; image1pt++)
	{
		// If the current point doesn't have a descriptor, skip it
		if(interestPts1[image1pt].m_DescSize == 0)
		{
			rejectNo++;
			continue;
		}

		for(int image2pt = 0; image2pt < numInterestPts2; image2pt++)
		{

			// If the current point doesn't have a descriptor, skip it
			if(interestPts2[image2pt].m_DescSize == 0)
			{
				continue;
			}

			// If it's the first match attempted, assume it's right and get the distance
			if(attemptNo == 0)
			{
				(*matches)[matchNo].m_X1 = interestPts1[image1pt].m_X;
				(*matches)[matchNo].m_Y1 = interestPts1[image1pt].m_Y;
				
				(*matches)[matchNo].m_X2 = interestPts2[image2pt].m_X;
				(*matches)[matchNo].m_Y2 = interestPts2[image2pt].m_Y;
				
				minL2distance = getDistance(interestPts1[image1pt], interestPts2[image2pt]);
				attemptNo++;
			}
			else
			{
				// If the distance is shorter, this is a better match
				if(getDistance(interestPts1[image1pt], interestPts2[image2pt]) < minL2distance)
				{
					(*matches)[matchNo].m_X1 = interestPts1[image1pt].m_X;
					(*matches)[matchNo].m_Y1 = interestPts1[image1pt].m_Y;
					
					(*matches)[matchNo].m_X2 = interestPts2[image2pt].m_X;
					(*matches)[matchNo].m_Y2 = interestPts2[image2pt].m_Y;

					minL2distance = getDistance(interestPts1[image1pt], interestPts2[image2pt]);
					attemptNo++;
				}
			}
		}
		attemptNo = 0;
		matchNo++;
	}

	numMatches = numMatches - rejectNo;

    // Draw the matches
    DrawMatches(*matches, numMatches, image1Display, image2Display);

	//delete [] (*matches);
}