Exemple #1
0
void procOCL_OCV(int tex, int w, int h)
{
    int64_t t = getTimeMs();
    cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY,  GL_TEXTURE_2D, 0, tex);
    std::vector < cl::Memory > images(1, imgIn);
    theQueue.enqueueAcquireGLObjects(&images);
    theQueue.finish();
    cv::UMat uIn, uOut, uTmp;
    cv::ocl::convertFromImage(imgIn(), uIn);
    LOGD("loading texture data to OpenCV UMat costs %d ms", getTimeInterval(t));
    theQueue.enqueueReleaseGLObjects(&images);

    t = getTimeMs();
    //cv::blur(uIn, uOut, cv::Size(5, 5));
    cv::Laplacian(uIn, uTmp, CV_8U);
    cv:multiply(uTmp, 10, uOut);
    cv::ocl::finish();
    LOGD("OpenCV processing costs %d ms", getTimeInterval(t));

    t = getTimeMs();
    cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, tex);
    images.clear();
    images.push_back(imgOut);
    theQueue.enqueueAcquireGLObjects(&images);
    cl_mem clBuffer = (cl_mem)uOut.handle(cv::ACCESS_READ);
    cl_command_queue q = (cl_command_queue)cv::ocl::Queue::getDefault().ptr();
    size_t offset = 0;
    size_t origin[3] = { 0, 0, 0 };
    size_t region[3] = { w, h, 1 };
    CV_Assert(clEnqueueCopyBufferToImage (q, clBuffer, imgOut(), offset, origin, region, 0, NULL, NULL) == CL_SUCCESS);
    theQueue.enqueueReleaseGLObjects(&images);
    cv::ocl::finish();
    LOGD("uploading results to texture costs %d ms", getTimeInterval(t));
}
Exemple #2
0
void procOCL_I2I(int texIn, int texOut, int w, int h)
{
    if(!haveOpenCL) return;

    LOGD("procOCL_I2I(%d, %d, %d, %d)", texIn, texOut, w, h);
    cl::ImageGL imgIn (theContext, CL_MEM_READ_ONLY,  GL_TEXTURE_2D, 0, texIn);
    cl::ImageGL imgOut(theContext, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texOut);
    std::vector < cl::Memory > images;
    images.push_back(imgIn);
    images.push_back(imgOut);

    int64_t t = getTimeMs();
    theQueue.enqueueAcquireGLObjects(&images);
    theQueue.finish();
    LOGD("enqueueAcquireGLObjects() costs %d ms", getTimeInterval(t));

    t = getTimeMs();
    cl::Kernel Laplacian(theProgI2I, "Laplacian"); //TODO: may be done once
    Laplacian.setArg(0, imgIn);
    Laplacian.setArg(1, imgOut);
    theQueue.finish();
    LOGD("Kernel() costs %d ms", getTimeInterval(t));

    t = getTimeMs();
    theQueue.enqueueNDRangeKernel(Laplacian, cl::NullRange, cl::NDRange(w, h), cl::NullRange);
    theQueue.finish();
    LOGD("enqueueNDRangeKernel() costs %d ms", getTimeInterval(t));

    t = getTimeMs();
    theQueue.enqueueReleaseGLObjects(&images);
    theQueue.finish();
    LOGD("enqueueReleaseGLObjects() costs %d ms", getTimeInterval(t));
}
Exemple #3
0
 /**
  * @brief Execute
  * @param nameIn
  * @param nameOut
  * @param size
  * @return
  */
 static Image *Execute(std::string nameIn, std::string nameOut, int size)
 {
     Image imgIn(nameIn);
     Image *imgOut = Execute(&imgIn, NULL, size);
     imgOut->Write(nameOut);
     return imgOut;
 }
Exemple #4
0
 /**
  * @brief Execute
  * @param fileInput
  * @param fileOutput
  * @return
  */
 static Image *Execute(std::string fileInput, std::string fileOutput)
 {
     Image imgIn(fileInput);
     Image *out = FilterNSWE::Execute(&imgIn, NULL);
     out->Write(fileOutput);
     return out;
 }
Exemple #5
0
 /**
  * @brief Execute
  * @param fileInput
  * @param fileOutput
  * @param min
  * @param max
  * @return
  */
 static Image *Execute(std::string fileInput, std::string fileOutput,
                          Vec2i min, Vec2i max)
 {
     Image imgIn(fileInput);
     Image *out = FilterCrop::Execute(&imgIn, NULL, min, max);
     out->Write(fileOutput);
     return out;
 }
    /**
     * @brief Execute
     * @param nameFileIn
     * @param nameFileOut
     * @param threshold_nuked
     * @return
     */
    static Image* Execute(std::string nameFileIn, std::string nameFileOut, float threshold_nuked = 1e4)
    {
        Image imgIn(nameFileIn);
        Image *imgOut = Execute(&imgIn, NULL, threshold_nuked);
        imgOut->Write(nameFileOut);

        return imgOut;
    }
void SCropVideoQuad::cropFrame(::fwCore::HiResClock::HiResClockType timestamp)
{
    ::arData::FrameTL::csptr frameTL = this->getInput< ::arData::FrameTL >(s_FRAMETL_INPUT);

    ::fwCore::HiResClock::HiResClockType newerTimestamp = frameTL->getNewerTimestamp();
    const CSPTR(::arData::FrameTL::BufferType) buffer   = frameTL->getClosestBuffer(newerTimestamp);

    ::arData::FrameTL::sptr frameTL1 = this->getInOut< ::arData::FrameTL >(s_FRAMETL1_INOUT);
    ::arData::FrameTL::sptr frameTL2 = this->getInOut< ::arData::FrameTL >(s_FRAMETL2_INOUT);
    ::arData::FrameTL::sptr frameTL3 = this->getInOut< ::arData::FrameTL >(s_FRAMETL3_INOUT);
    ::arData::FrameTL::sptr frameTL4 = this->getInOut< ::arData::FrameTL >(s_FRAMETL4_INOUT);

    const size_t width  = frameTL->getWidth();
    const size_t height = frameTL->getHeight();

    const size_t widthTL1  = frameTL1->getWidth();
    const size_t heightTL1 = frameTL1->getHeight();

    const size_t widthCropped  = width / 2;
    const size_t heightCropped = height / 2;

    if (widthCropped != widthTL1 || heightCropped != heightTL1)
    {
        const size_t numberOfComponents = frameTL->getNumberOfComponents();
        const ::fwTools::Type type      = frameTL->getType();

        frameTL1->clearTimeline();
        frameTL1->initPoolSize(widthCropped, heightCropped, type, numberOfComponents);
        frameTL2->clearTimeline();
        frameTL2->initPoolSize(widthCropped, heightCropped, type, numberOfComponents);
        frameTL3->clearTimeline();
        frameTL3->initPoolSize(widthCropped, heightCropped, type, numberOfComponents);
        frameTL4->clearTimeline();
        frameTL4->initPoolSize(widthCropped, heightCropped, type, numberOfComponents);
    }

    if(buffer)
    {
        const std::uint8_t* frameBuff = &buffer->getElement(0);

        // The cv image that will be processed
        ::cv::Mat imgIn(height, width, CV_8UC4, (void*)frameBuff, ::cv::Mat::AUTO_STEP);
        const ::cv::Rect roi1(0, 0, widthCropped, heightCropped);
        const ::cv::Rect roi2(widthCropped, 0, widthCropped, heightCropped);
        const ::cv::Rect roi3(0, heightCropped, widthCropped, heightCropped);
        const ::cv::Rect roi4(widthCropped, heightCropped, widthCropped, heightCropped);

        this->pushFrameInTimeline(imgIn, roi1, frameTL1, newerTimestamp);
        this->pushFrameInTimeline(imgIn, roi2, frameTL2, newerTimestamp);
        this->pushFrameInTimeline(imgIn, roi3, frameTL3, newerTimestamp);
        this->pushFrameInTimeline(imgIn, roi4, frameTL4, newerTimestamp);
    }
}
    /**
     * @brief execute
     * @param nameIn
     * @param nameOut
     * @param sigma_s
     * @param sigma_r
     * @param testing
     * @return
     */
    static ImageGL *execute(std::string nameIn, std::string nameOut,
                               float sigma_s, float sigma_r, int testing)
    {
        GLuint testTQ = glBeginTimeQuery();
        glEndTimeQuery(testTQ);

        ImageGL imgIn(nameIn);
        imgIn.generateTextureGL(GL_TEXTURE_2D, GL_FLOAT, false);

        FilterGLBilateral2DSP filter(sigma_s, sigma_r);
        ImageGL *imgRet = new ImageGL(1, imgIn.width, imgIn.height, 3, IMG_GPU, GL_TEXTURE_2D);

        GLuint testTQ1;

        if(testing > 1) {
            filter.Process(SingleGL(&imgIn), imgRet);

            testTQ1 = glBeginTimeQuery();

            for(int i = 0; i < testing; i++) {
                filter.Process(SingleGL(&imgIn), imgRet);
            }
        } else {
            testTQ1 = glBeginTimeQuery();
            filter.Process(SingleGL(&imgIn), imgRet);
        }

        GLuint64EXT timeVal = glEndTimeQuery(testTQ1);
        double ms = double(timeVal) / (double(testing) * 1000000.0);
        printf("Separate Bilateral Filter on GPU time: %f ms\n", ms);

        std::string sign = genBilString("S", sigma_s, sigma_r);
        std::string nameTime = FileLister::getFileNumber(sign, "txt");

        FILE *file = fopen(nameTime.c_str(), "w");

        if(file != NULL) {
            fprintf(file, "%f", ms);
            fclose(file);
        }

        ImageGL *imgWrite = new ImageGL(1, imgIn.width, imgIn.height, 4, IMG_CPU, GL_TEXTURE_2D);
        imgWrite->readFromFBO(filter.getFbo());
        imgWrite->Write(nameOut);
        return imgRet;
    }
    /**
     * @brief Test
     */
    static void Test()
    {
        Image imgIn(1, 512, 512, 3);
        imgIn = 1.0f;

        FilterChannel filter(0);
        Image *outR = filter.Process(Single(&imgIn), NULL);

        filter.setChannel(1);
        Image *outG = filter.Process(Single(&imgIn), NULL);

        filter.setChannel(2);
        Image *outB = filter.Process(Single(&imgIn), NULL);

        outR->Write("channel_R.pfm");
        outG->Write("channel_G.pfm");
        outB->Write("channel_B.pfm");
    }
Exemple #10
0
    /**
     * @brief ExecuteTest
     * @param nameIn
     * @param nameOut
     * @return
     */
    static Image *ExecuteTest(std::string nameIn, std::string nameOut)
    {
        Image imgIn(nameIn);

        FilterChannel filter(0);
        Image *outR = filter.Process(Single(&imgIn), NULL);

        filter.setChannel(1);
        Image *outG = filter.Process(Single(&imgIn), NULL);

        filter.setChannel(2);
        Image *outB = filter.Process(Single(&imgIn), NULL);

        ImageVec src;
        src.push_back(outR);
        src.push_back(outG);
        src.push_back(outB);

        FilterCombine filterC;
        Image *ret = filterC.Process(src, NULL);

        ret->Write(nameOut);
        return ret;
    }