void HxCPDSpatialGraphWarp::applyNLDeformationToSlice(
    SpatialGraphSelection& slice, HxSpatialGraph* spatialGraph,
    const McDArray<McVec3f>& origCoords,
    const McDArray<McVec3f>& shiftedCoords) {

    McWatch watch;
    watch.start();

    MovingLeastSquares mls;
    mls.setAlpha(portAlphaForMLS.getValue());
    mls.setLandmarks(origCoords, shiftedCoords);
    ma::SliceSelector ssh(spatialGraph, "TransformInfo");
    SpatialGraphSelection fullSliceSelection;
    ssh.getSlice(ssh.getSliceAttributeValueFromIndex(1), fullSliceSelection);

    for (int i = 0; i < fullSliceSelection.getNumSelectedEdges(); i++) {
        int edge = fullSliceSelection.getSelectedEdge(i);
        McDArray<McVec3f> newEdgePoints;
        for (int j = 0; j < spatialGraph->getNumEdgePoints(edge); j++) {

            McVec3f edgePoint = spatialGraph->getEdgePoint(edge, j);
            warpPoint(edgePoint, edgePoint, mls);
            newEdgePoints.append(edgePoint);
        }
        spatialGraph->setEdgePoints(edge, newEdgePoints);
    }

    for (int i = 0; i < fullSliceSelection.getNumSelectedVertices(); i++) {
        int curVertex = fullSliceSelection.getSelectedVertex(i);
        McVec3f curCoord = spatialGraph->getVertexCoords(curVertex);
        McVec3f curCoordWarped;
        warpPoint(curCoord, curCoordWarped, mls);
        spatialGraph->setVertexCoords(curVertex, curCoordWarped);
        // Add new segments that indicate shift.
        if (slice.isSelectedVertex(curVertex)) {
            int newVertex = spatialGraph->addVertex(curCoord);
            McDArray<McVec3f> edgePoints(2);
            edgePoints[0] = curCoord;
            edgePoints[1] = curCoordWarped;
            spatialGraph->addEdge(newVertex, curVertex, edgePoints);
        }
    }

    std::cout << "\n Apply deformation took " << watch.stop() << " seconds.";
}
Exemplo n.º 2
0
	// init application and run
	int Run()
	{
		CBitmapCapture capture(DEMO_IMAGE);

		// open camera
		if (!capture.OpenCamera())
		{
			printf("error: could not open camera\n");
			return 1;
		}
		
		const int width = capture.GetWidth();
		const int height = capture.GetHeight();

		// create temp image for the image processing
		CByteImage image(width, height, capture.GetType());
		CByteImage grayImage(width, height, CByteImage::eGrayScale);
		CByteImage tempImage(width, height, CByteImage::eGrayScale);
		CByteImage visualizationImage(width, height, CByteImage::eRGB24);
		CByteImage *pImage = &image;


		// create an application handler
		CApplicationHandlerInterface *pApplicationHandler = CreateApplicationHandler();
		pApplicationHandler->Reset();
		
		// create a main window
		m_pMainWindow = CreateMainWindow(0, 0, width, height + 190, "Hough Line Detection Demo");

		// events are sent to this class, hence this class needs to have the CMainWindowEventInterface
		m_pMainWindow->SetEventCallback(this);

		// create an image widget to display a window
		WIDGET_HANDLE pImageWidget = m_pMainWindow->AddImage(0, 190, width, height);

		// add a label and a slider for the low threshold
		WIDGET_HANDLE pLabelCannyLow = m_pMainWindow->AddLabel(15, 15, 200, 30, "Canny low threshold: 0");
		m_pSliderCannyLow = m_pMainWindow->AddSlider(15, 30, 200, 40, 0, 1020, 102, m_nCannyLowThreshold);

		// add a label and a slider for the high threshold
		WIDGET_HANDLE pLabelCannyHigh = m_pMainWindow->AddLabel(15, 70, 200, 30, "Canny high threshold: 0");
		m_pSliderCannyHigh = m_pMainWindow->AddSlider(15, 85, 200, 40, 0, 1020, 102, m_nCannyHighThreshold);
		
		// add a label and a slider for the number of lines to extract
		WIDGET_HANDLE pLabelLines = m_pMainWindow->AddLabel(260, 15, 200, 30, "Circles to extract: 0 lines");
		m_pSliderLinesToExtract = m_pMainWindow->AddSlider(260, 30, 200, 40, 0, 30, 5, m_nCirclesToExtract);
		
		// add labels/sliders for specifying the radius interval of interest
		WIDGET_HANDLE pLabelMinRadius = m_pMainWindow->AddLabel(260, 70, 200, 30, "Min radius: 0");
		m_pSliderMinRadius = m_pMainWindow->AddSlider(260, 85, 200, 40, 1, 200, 5, m_nMinRadius);
		WIDGET_HANDLE pLabelMaxRadius = m_pMainWindow->AddLabel(260, 125, 200, 30, "Max radius: 0");
		m_pSliderMaxRadius = m_pMainWindow->AddSlider(260, 140, 200, 40, 1, 200, 5, m_nMaxRadius);
		
		// add a button to toggle between the original image and the processed one
		m_pButton = m_pMainWindow->AddButton(510, 80, 110, 35, "Show Edges");

		// add a labels to display processing stats
		WIDGET_HANDLE pLabelMS = m_pMainWindow->AddLabel(560, 15, 70, 20, "0 ms");
		WIDGET_HANDLE pLabelFPS = m_pMainWindow->AddLabel(560, 45, 70, 20, "0 fps");

		// make the window visible
		m_pMainWindow->Show();
		

		char buffer[1024];
		
		CVec3dArray resultListCircles(50);
		CDynamicArrayTemplate<int> resultHits(50);
		CVec2dArray edgePoints(10000), edgeDirections(10000);
		
		// main loop
		while (!pApplicationHandler->ProcessEventsAndGetExit())
		{
			if (!capture.CaptureImage(&pImage))
				break;
			
			// this is for visualization purposes only
			ImageProcessor::ConvertImage(pImage, &visualizationImage);
			
			get_timer_value(true);
			
			// convert input image to grayscale image
			ImageProcessor::ConvertImage(&image, &tempImage, true);
			
			// smooth image
			ImageProcessor::GaussianSmooth3x3(&tempImage, &grayImage);

			// detect edges with Canny edge detector
			ImageProcessor::Canny(&grayImage, edgePoints, edgeDirections, m_nCannyLowThreshold, m_nCannyHighThreshold);
			
			// detect lines with Hough transform
			ImageProcessor::HoughTransformCircles(edgePoints, edgeDirections, width, height, m_nMinRadius, m_nMaxRadius, m_nCirclesToExtract, 1, resultListCircles, resultHits, &visualizationImage);

			const unsigned int t = get_timer_value();
			
			// display the speed stats
			sprintf(buffer, "%2.2f ms", t / 1000.0f);
			m_pMainWindow->SetText(pLabelMS, buffer);
			sprintf(buffer, "%3.2f fps", 1000000.0f / t);
			m_pMainWindow->SetText(pLabelFPS, buffer);
			sprintf(buffer, "Canny low threshold: %i", m_nCannyLowThreshold);
			m_pMainWindow->SetText(pLabelCannyLow, buffer);
			sprintf(buffer, "Canny high threshold: %i", m_nCannyHighThreshold);
			m_pMainWindow->SetText(pLabelCannyHigh, buffer);
			sprintf(buffer, "Min radius: %i", m_nMinRadius);
			m_pMainWindow->SetText(pLabelMinRadius, buffer);
			sprintf(buffer, "Max radius: %i", m_nMaxRadius);
			m_pMainWindow->SetText(pLabelMaxRadius, buffer);
			sprintf(buffer, "Circles to extract: %i", m_nCirclesToExtract);
			m_pMainWindow->SetText(pLabelLines, buffer);

			// display either the original image or the processed image
			if (m_bShowEdges)
			{
				ImageProcessor::Canny(&grayImage, &grayImage, m_nCannyLowThreshold, m_nCannyHighThreshold);
				m_pMainWindow->SetImage(pImageWidget, &grayImage);
			}
			else
				m_pMainWindow->SetImage(pImageWidget, &visualizationImage);
		}
		
		delete m_pMainWindow;
		delete pApplicationHandler;
		
		return 0;
	}