Xilinx / Vitis_Libraries

Vitis Libraries
https://docs.xilinx.com/r/en-US/Vitis_Libraries
Apache License 2.0
894 stars 356 forks source link

L2 dense optical flow example issue #205

Open mbugti opened 2 months ago

mbugti commented 2 months ago

Hi. I am using L2 example of pyr dense optical flow example to compute optical flow. I wanted to create a bounding box at a specif point, which then should relocate according to flow. Which i successfully performed in opencv version. However, in accelerated example, the bounding box relocates in whole frame instead of ROI. I am unaware of that if i am using the correct approach or not. would appreciate if anyone could guide.

This is the flow that i managed to extract. Which i am not completely sure if its correct or not

flow_Vec.push_back(flow_buf);
q.enqueueMigrateMemObjects(flow_Vec, CL_MIGRATE_MEM_OBJECT_HOST);
q.finish();

std::vector<unsigned int> flow_data(HEIGHT * WIDTH);
q.enqueueMigrateMemObjects(flow_Vec, CL_MIGRATE_MEM_OBJECT_HOST);
q.finish();

// cl::copy(q, flow_buf, flow_data.begin(), flow_data.end());

unsigned int flow_data_at_pos = flow_data[bbox_y * WIDTH + bbox_x];

    // Extract x and y components from the flow_data
    int flow_x = (flow_data_at_pos >> 16) & 0xFFFF; // Higher 16 bits
    int flow_y = flow_data_at_pos & 0xFFFF;         // Lower 16 bits

    // Convert unsigned components to signed if necessary
    if (flow_x & 0x8000) flow_x -= 0x10000; // Handle negative values for x
    if (flow_y & 0x8000) flow_y -= 0x10000; // Handle negative values for y

This is the initialization of the bounding box // Update the bounding box position based on flow components bbox_x += flow_x; bbox_y += flow_y;

    // Ensure the bounding box stays within image bounds
    if (bbox_x < 0) bbox_x = 0;
    if (bbox_y < 0) bbox_y = 0;
    if (bbox_x + bbox_width > WIDTH) bbox_x = WIDTH - bbox_width;
    if (bbox_y + bbox_height > HEIGHT) bbox_y = HEIGHT - bbox_height;

This is the relocation of bounding box

// write output flow vectors to Mat after splitting the bits.
for (int i = 0; i < pyr_h[0]; i++) {
    for (int j = 0; j < pyr_w[0]; j++) {
        unsigned int tempcopy = 0;
        {
            // tempcopy = *(flow.data + i*pyr_w[0] + j);
            tempcopy = flow.read(i * pyr_w[0] + j);
        }

// fprintf(fp_flow, "%u\n", tempcopy); short splittemp1 = (tempcopy >> 16); short splittemp2 = (0x0000FFFF & tempcopy);

        TYPE_FLOW_TYPE* uflow = (TYPE_FLOW_TYPE*)&splittemp1;
        TYPE_FLOW_TYPE* vflow = (TYPE_FLOW_TYPE*)&splittemp2;
        // Update the bounding box position based on flow components
        bbox_x += flow_x;
        bbox_y += flow_y;

        // Ensure the bounding box stays within image bounds
        if (bbox_x < 0) bbox_x = 0;
        if (bbox_y < 0) bbox_y = 0;
        if (bbox_x + bbox_width > WIDTH) bbox_x = WIDTH - bbox_width;
        if (bbox_y + bbox_height > HEIGHT) bbox_y = HEIGHT - bbox_height;
        glx.at<float>(i, j) = (float)*uflow;
        gly.at<float>(i, j) = (float)*vflow;

Any help would be much appreciated.

Thank you