Exemplo n.º 1
0
void NoneLocalMeans::recompileOpenCLCode(Image::pointer input) {
    // Check if there is a need to recompile OpenCL code
    if(input->getDimensions() == mDimensionCLCodeCompiledFor &&
       input->getDataType() == mTypeCLCodeCompiledFor && !recompile)
        return;
    
    recompile = false;
    OpenCLDevice::pointer device = getMainDevice();
    std::string buildOptions = "";
    if(!device->isWritingTo3DTexturesSupported()) {
        buildOptions = "-DTYPE=" + getCTypeAsString(mOutputType);
    }
    buildOptions += " -D WINDOW=";
    buildOptions += std::to_string((windowSize-1)/2);
    buildOptions += " -D GROUP=";
    buildOptions += std::to_string((groupSize-1)/2);
    buildOptions += " -D KVERSION=";
    buildOptions += std::to_string(k);
    buildOptions += " -D EUCLID=";
    buildOptions += std::to_string(k);
    cl::Program program;
    if(input->getDimensions() == 2) {
        program = getOpenCLProgram(device, "2D", buildOptions);
    } else {
        //createOpenCLProgram(std::string(FAST_SOURCE_DIR) + "Algorithms/NoneLocalMeans/NoneLocalMeans3Dgs.cl", "3D");
        program = getOpenCLProgram(device, "3D", buildOptions);
    }
    mKernel = cl::Kernel(program, "noneLocalMeans");
    mDimensionCLCodeCompiledFor = input->getDimensions();
    mTypeCLCodeCompiledFor = input->getDataType();
}
Exemplo n.º 2
0
void SeededRegionGrowing::recompileOpenCLCode(Image::pointer input) {
    // Check if there is a need to recompile OpenCL code
    if(input->getDimensions() == mDimensionCLCodeCompiledFor &&
            input->getDataType() == mTypeCLCodeCompiledFor)
        return;

    OpenCLDevice::pointer device = getMainDevice();
    std::string buildOptions = "";
    if(input->getDataType() == TYPE_FLOAT) {
        buildOptions = "-DTYPE_FLOAT";
    } else if(input->getDataType() == TYPE_INT8 || input->getDataType() == TYPE_INT16) {
        buildOptions = "-DTYPE_INT";
    } else {
        buildOptions = "-DTYPE_UINT";
    }
    std::string filename;
    if(input->getDimensions() == 2) {
        filename = "Algorithms/SeededRegionGrowing/SeededRegionGrowing2D.cl";
    } else {
        filename = "Algorithms/SeededRegionGrowing/SeededRegionGrowing3D.cl";
    }
    int programNr = device->createProgramFromSource(std::string(FAST_SOURCE_DIR) + filename, buildOptions);
    mKernel = cl::Kernel(device->getProgram(programNr), "seededRegionGrowing");
    mDimensionCLCodeCompiledFor = input->getDimensions();
    mTypeCLCodeCompiledFor = input->getDataType();
}
Exemplo n.º 3
0
void BinaryThresholding::execute() {
    if(!mLowerThresholdSet && !mUpperThresholdSet) {
        throw Exception("BinaryThresholding need at least one threshold to be set.");
    }

    Image::pointer input = getStaticInputData<Image>(0);
    Segmentation::pointer output = getStaticOutputData<Segmentation>(0);

    output->createFromImage(input);

    if(getMainDevice()->isHost()) {
        throw Exception("Not implemented yet.");
    } else {
        OpenCLDevice::pointer device = OpenCLDevice::pointer(getMainDevice());
        cl::Program program;
        if(input->getDimensions() == 3) {
            program = getOpenCLProgram(device, "3D");
        } else {
            program = getOpenCLProgram(device, "2D");
        }
        cl::Kernel kernel;
        if(mLowerThresholdSet && mUpperThresholdSet) {
            kernel = cl::Kernel(program, "tresholding");
            kernel.setArg(3, mLowerThreshold);
            kernel.setArg(4, mUpperThreshold);
        } else if(mLowerThresholdSet) {
            kernel = cl::Kernel(program, "thresholdingWithOnlyLower");
            kernel.setArg(3, mLowerThreshold);
        } else {
            kernel = cl::Kernel(program, "thresholdingWithOnlyUpper");
            kernel.setArg(3, mUpperThreshold);
        }
        cl::NDRange globalSize;
        OpenCLImageAccess::pointer access = input->getOpenCLImageAccess(ACCESS_READ, device);
        if(input->getDimensions() == 2) {
            OpenCLImageAccess::pointer access2 = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            kernel.setArg(0, *access->get2DImage());
            kernel.setArg(1, *access2->get2DImage());
            globalSize = cl::NDRange(output->getWidth(), output->getHeight());
        } else {
            // TODO no 3d image write support
            OpenCLImageAccess::pointer access2 = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            kernel.setArg(0, *access->get3DImage());
            kernel.setArg(1, *access2->get3DImage());
            globalSize = cl::NDRange(output->getWidth(), output->getHeight(), output->getDepth());
        }
        kernel.setArg(2, (uchar)mLabel);

        cl::CommandQueue queue = device->getCommandQueue();
        queue.enqueueNDRangeKernel(
                kernel,
                cl::NullRange,
                globalSize,
                cl::NullRange
        );
    }
}
Exemplo n.º 4
0
void ImageSlicer::execute() {
	Image::pointer input = getStaticInputData<Image>();
	Image::pointer output = getStaticOutputData<Image>();

	if(input->getDimensions() != 3)
		throw Exception("Image slicer can only be used for 3D images");

	if(!mArbitrarySlicing && !mOrthogonalSlicing)
		throw Exception("No slice plane given to the ImageSlicer");

	// TODO
	if(mOrthogonalSlicing) {
		orthogonalSlicing(input, output);
	} else {
		arbitrarySlicing(input, output);
	}
}
Exemplo n.º 5
0
/*
void NoneLocalMeans::recompileOpenCLCode(Image::pointer input) {
	// Check if there is a need to recompile OpenCL code
	if (input->getDimensions() == mDimensionCLCodeCompiledFor &&
		input->getDataType() == mTypeCLCodeCompiledFor && !recompile)
		return;

	OpenCLDevice::pointer device = getMainDevice();
    recompile = false;
	std::string buildOptions = "";
	const bool writingTo3DTextures = device->getDevice().getInfo<CL_DEVICE_EXTENSIONS>().find("cl_khr_3d_image_writes") != std::string::npos;
	if (!writingTo3DTextures) {
		switch (mOutputType) {
		case TYPE_FLOAT:
			buildOptions += " -DTYPE=float";
			break;
		case TYPE_INT8:
			buildOptions += " -DTYPE=char";
			break;
		case TYPE_UINT8:
			buildOptions += " -DTYPE=uchar";
			break;
		case TYPE_INT16:
			buildOptions += " -DTYPE=short";
			break;
		case TYPE_UINT16:
			buildOptions += " -DTYPE=ushort";
			break;
		}
	}
    buildOptions += " -D WINDOW=";
    buildOptions += std::to_string((windowSize-1)/2);
	buildOptions += " -D GROUP=";
    buildOptions += std::to_string((groupSize-1)/2);
    
	std::string filename;
	//might have to seperate color vs gray here, for better runtime
	if (input->getDimensions() == 2) {
        if(k == 0){
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dconstant.cl";
        }else if(k == 1){
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dgaussian.cl";
        }else{
            filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dconstant.cl";
        }
		//filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2DgsPixelWise.cl";
		//filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dgs.cl";
        //filename = "Algorithms/NoneLocalMeans/NoneLocalMeans2Dc.cl";
	}
	else {
		filename = "Algorithms/NoneLocalMeans/NoneLocalMeans3Dgs.cl";
	}
	int programNr = device->createProgramFromSource(std::string(FAST_SOURCE_DIR) + filename, buildOptions);
	mKernel = cl::Kernel(device->getProgram(programNr), "noneLocalMeans");
	mDimensionCLCodeCompiledFor = input->getDimensions();
	mTypeCLCodeCompiledFor = input->getDataType();
}*/
void NoneLocalMeans::execute() {
    Image::pointer input = getStaticInputData<Image>(0);
    Image::pointer output = getStaticOutputData<Image>(0);
    
    // Initialize output image
    ExecutionDevice::pointer device = getMainDevice();
    if(mOutputTypeSet) {
        output->create(input->getSize(), mOutputType, input->getNrOfComponents());
        output->setSpacing(input->getSpacing());
    } else {
        output->createFromImage(input);
    }
    mOutputType = output->getDataType();
    SceneGraph::setParentNode(output, input);
    
    
    if(device->isHost()) {
        switch(input->getDataType()) {
                fastSwitchTypeMacro(executeAlgorithmOnHost<FAST_TYPE>(input, output, groupSize, windowSize, denoiseStrength, sigma));
        }
    } else {
        OpenCLDevice::pointer clDevice = device;
        
        recompileOpenCLCode(input);
        
        cl::NDRange globalSize;
        
        OpenCLImageAccess::pointer inputAccess = input->getOpenCLImageAccess(ACCESS_READ, device);
        if(input->getDimensions() == 2) {
            OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
            mKernel.setArg(2, (denoiseStrength*denoiseStrength));
            mKernel.setArg(3, (sigma*sigma));
            globalSize = cl::NDRange(input->getWidth(),input->getHeight());
            mKernel.setArg(0, *inputAccess->get2DImage());
            mKernel.setArg(1, *outputAccess->get2DImage());
            clDevice->getCommandQueue().enqueueNDRangeKernel(
                    mKernel,
                    cl::NullRange,
                    globalSize,
                    cl::NullRange
            );
        } else {
            // Create an auxilliary image
            //Image::pointer output2 = Image::New();
            //output2->createFromImage(output);
            
            globalSize = cl::NDRange(input->getWidth(),input->getHeight(),input->getDepth());
            
            if(clDevice->isWritingTo3DTexturesSupported()) {
                mKernel.setArg(2, (denoiseStrength*denoiseStrength));
                mKernel.setArg(3, (sigma*sigma));
                OpenCLImageAccess::pointer outputAccess = output->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                //OpenCLImageAccess::pointer outputAccess2 = output2->getOpenCLImageAccess(ACCESS_READ_WRITE, device);
                
                //cl::Image3D* image2;
                cl::Image3D* image;
                image = outputAccess->get3DImage();
                //image2 = outputAccess->get3DImage();
                mKernel.setArg(0, *inputAccess->get3DImage());
                mKernel.setArg(1, *image);
                clDevice->getCommandQueue().enqueueNDRangeKernel(
                        mKernel,
                        cl::NullRange,
                        globalSize,
                        cl::NullRange
                );
            }else{
                mKernel.setArg(2, (denoiseStrength*denoiseStrength));
                mKernel.setArg(3, (sigma*sigma));
                OpenCLBufferAccess::pointer outputAccess = output->getOpenCLBufferAccess(ACCESS_READ_WRITE, device);
                mKernel.setArg(0, *inputAccess->get3DImage());
                mKernel.setArg(1, *outputAccess->get());
                clDevice->getCommandQueue().enqueueNDRangeKernel(
                        mKernel,
                        cl::NullRange,
                        globalSize,
                        cl::NullRange
                );
            }
            
            
        }
    }
}
Exemplo n.º 6
0
#include "VTKImageImporter.hpp"
#include "VTKImageExporter.hpp"
#include "ImageImporter.hpp"

using namespace fast;

// TODO rewrite this test so that it doesn't use the vtk exporter
TEST_CASE("Import an image from VTK to FAST", "[fast][VTK]") {
    ImageImporter::pointer importer = ImageImporter::New();
    importer->setFilename(std::string(FAST_TEST_DATA_DIR) + "US-2D.jpg");
    Image::pointer fastImage = importer->getOutput();

    // VTK Export
    vtkSmartPointer<VTKImageExporter> vtkExporter = VTKImageExporter::New();
    vtkExporter->SetInput(fastImage);
    vtkSmartPointer<vtkImageData> vtkImage = vtkExporter->GetOutput();
    vtkExporter->Update();

    // VTK Import example
    VTKImageImporter::pointer vtkImporter = VTKImageImporter::New();
    vtkImporter->setInput(vtkImage);
    Image::pointer importedImage = vtkImporter->getOutput();
    vtkImporter->update();

    CHECK(fastImage->getWidth() == importedImage->getWidth());
    CHECK(fastImage->getHeight() == importedImage->getHeight());
    CHECK(fastImage->getDepth() == 1);
    CHECK(fastImage->getDimensions() == 2);
    CHECK(fastImage->getDataType() == TYPE_FLOAT);
}
Exemplo n.º 7
0
void
SegmentationRenderer::draw(Matrix4f perspectiveMatrix, Matrix4f viewingMatrix, float zNear, float zFar, bool mode2D) {
    std::lock_guard<std::mutex> lock(mMutex);
    OpenCLDevice::pointer device = std::dynamic_pointer_cast<OpenCLDevice>(getMainDevice());


    if(mColorsModified) {
        // Transfer colors to device (this doesn't have to happen every render call..)
        std::unique_ptr<float[]> colorData(new float[3*mLabelColors.size()]);
        std::unordered_map<int, Color>::iterator it;
        for(it = mLabelColors.begin(); it != mLabelColors.end(); it++) {
            colorData[it->first*3] = it->second.getRedValue();
            colorData[it->first*3+1] = it->second.getGreenValue();
            colorData[it->first*3+2] = it->second.getBlueValue();
        }

        mColorBuffer = cl::Buffer(
                device->getContext(),
                CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                sizeof(float)*3*mLabelColors.size(),
                colorData.get()
        );
    }

    if(mFillAreaModified) {
        // Transfer colors to device (this doesn't have to happen every render call..)
        std::unique_ptr<char[]> fillAreaData(new char[mLabelColors.size()]);
        std::unordered_map<int, Color>::iterator it;
        for(it = mLabelColors.begin(); it != mLabelColors.end(); it++) {
            if(mLabelFillArea.count(it->first) == 0) {
                // Use default value
                fillAreaData[it->first] = mFillArea;
            } else {
                fillAreaData[it->first] = mLabelFillArea[it->first];
            }
        }

        mFillAreaBuffer = cl::Buffer(
                device->getContext(),
                CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                sizeof(char)*mLabelColors.size(),
                fillAreaData.get()
        );
    }

    mKernel = cl::Kernel(getOpenCLProgram(device), "renderToTexture");
    mKernel.setArg(2, mColorBuffer);
    mKernel.setArg(3, mFillAreaBuffer);
    mKernel.setArg(4, mBorderRadius);
    mKernel.setArg(5, mOpacity);


    for(auto it : mDataToRender) {
        Image::pointer input = std::static_pointer_cast<Image>(it.second);
        uint inputNr = it.first;

        if(input->getDimensions() != 2)
            throw Exception("SegmentationRenderer only supports 2D images. Use ImageSlicer to extract a 2D slice from a 3D image.");

        if(input->getDataType() != TYPE_UINT8)
            throw Exception("SegmentationRenderer only support images with dat type uint8.");

        // Check if a texture has already been created for this image
        if(mTexturesToRender.count(inputNr) > 0 && mImageUsed[inputNr] == input)
            continue; // If it has already been created, skip it

        // If it has not been created, create the texture

        OpenCLImageAccess::pointer access = input->getOpenCLImageAccess(ACCESS_READ, device);
        cl::Image2D *clImage = access->get2DImage();

        // Run kernel to fill the texture
        cl::CommandQueue queue = device->getCommandQueue();

        if (mTexturesToRender.count(inputNr) > 0) {
            // Delete old texture
            glDeleteTextures(1, &mTexturesToRender[inputNr]);
            mTexturesToRender.erase(inputNr);
            glDeleteVertexArrays(1, &mVAO[inputNr]);
            mVAO.erase(inputNr);
        }

        cl::Image2D image;
        cl::ImageGL imageGL;
        std::vector<cl::Memory> v;
        GLuint textureID;
        // TODO The GL-CL interop here is causing glClear to not work on AMD systems and therefore disabled
        /*
        if(DeviceManager::isGLInteropEnabled()) {
            // Create OpenGL texture
            glGenTextures(1, &textureID);
            glBindTexture(GL_TEXTURE_2D, textureID);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, input->getWidth(), input->getHeight(), 0, GL_RGBA, GL_FLOAT, 0);

            // Create CL-GL image
            imageGL = cl::ImageGL(
                    device->getContext(),
                    CL_MEM_READ_WRITE,
                    GL_TEXTURE_2D,
                    0,
                    textureID
            );
            glBindTexture(GL_TEXTURE_2D, 0);
            glFinish();
            mKernel.setArg(1, imageGL);
            v.push_back(imageGL);
            queue.enqueueAcquireGLObjects(&v);
        } else {
         */
        image = cl::Image2D(
                device->getContext(),
                CL_MEM_READ_WRITE,
                cl::ImageFormat(CL_RGBA, CL_FLOAT),
                input->getWidth(), input->getHeight()
        );
        mKernel.setArg(1, image);
        //}


        mKernel.setArg(0, *clImage);
        queue.enqueueNDRangeKernel(
                mKernel,
                cl::NullRange,
                cl::NDRange(input->getWidth(), input->getHeight()),
                cl::NullRange
        );

        /*if(DeviceManager::isGLInteropEnabled()) {
            queue.enqueueReleaseGLObjects(&v);
        } else {*/
        // Copy data from CL image to CPU
        auto data = make_uninitialized_unique<float[]>(input->getWidth() * input->getHeight() * 4);
        queue.enqueueReadImage(
                image,
                CL_TRUE,
                createOrigoRegion(),
                createRegion(input->getWidth(), input->getHeight(), 1),
                0, 0,
                data.get()
        );
        // Copy data from CPU to GL texture
        glGenTextures(1, &textureID);
        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, input->getWidth(), input->getHeight(), 0, GL_RGBA, GL_FLOAT, data.get());
        glBindTexture(GL_TEXTURE_2D, 0);
        glFinish();
        //}

        mTexturesToRender[inputNr] = textureID;
        mImageUsed[inputNr] = input;
        queue.finish();
    }

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    drawTextures(perspectiveMatrix, viewingMatrix, mode2D);
    glDisable(GL_BLEND);
}