1

I've made some modifications to https://github.com/NVIDIA/video-sdk-samples/tree/master/nvEncDXGIOutputDuplicationSample to convert the desktop image to a cv mat.

I have a dx11 2d texture and I want the 640x640 center of the texture (cropping in opencv is slow). CopySubresourceRegion (https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-copysubresourceregion) works until I pass a D3D11_BOX. When I convert it to a cv mat, it's just a black screen.

my_box.left = 1600;
my_box.top = 480;
my_box.right = 1600;
my_box.bottom = 480;

Can anyone give me a hint as to what I might be missing?

  • how do you create the Mat? – Christoph Rackwitz Jul 25 '22 at 02:09
  • 2
    Make sure you enable the DirectX debug layer: https://learn.microsoft.com/en-us/windows/win32/direct3d11/using-the-debug-layer-to-test-apps and https://walbourn.github.io/direct3d-sdk-debug-layer-tricks/ check all errors, and post some code, we can't guess what you may be missing w/o code https://stackoverflow.com/help/minimal-reproducible-example – Simon Mourier Jul 25 '22 at 05:44
  • Are you sure those are the values you want in your box desc? – mateeeeeee Jul 25 '22 at 07:57
  • @SimonMourier Really that's the entire relevant code. The subresource copy works and displays without D3D11_BOX. Idk that anyone else could get anything out of it. It's not a logic problem, I just need someone that's done this – user19250735 Jul 25 '22 at 11:24
  • @ChristophRackwitz I init the dx context for opencv, then do cv::directx::convertFromD3D11Texture2D(myText, mat); This works as long as I don't provide D3D11_BOX – user19250735 Jul 25 '22 at 11:26
  • 1
    @mateeeeeee with fresh eyes this morning I figured it out. I missed this relevant portion in the docs and assumed it was the same left right bottom top everyone else does The values for right, bottom, and back are each one pixel past the end of the pixels that are included in the box region. That is, the values for left, top, and front are included in the box region while the values for right, bottom, and back are excluded from the box region. For example, for a box that is one pixel wide, (right - left) == 1; the box region includes the left pixel but not the right pixel. – user19250735 Jul 25 '22 at 11:35
  • you should post that as an answer then. self-answering is encouraged on this site (provided it solves the problem). perhaps revise the question too, in ways that lets others with the same problem find this question and the solution. you should also copy excerpts of relevant code into the question, so the question contains something approaching an [mre]. the question should contain the essentials of the problem. linking to an entire github repo should only be supplemental. – Christoph Rackwitz Jul 25 '22 at 11:46

1 Answers1

1

So there's a lot of x/y box options out there. I initially thought this was negatives. As in, you will define the area to not cap and you get the inverse. This is a very popular approach. This is a little goofier than that.

So lets say you want center. That's dimension(so w or h)/2-desired_box_size/2. The answer you get on each of these will be left and top. From here, you'll just add however much to get the box size. For a 3840 by 1600 monitor with a 640x640 crop your box looks like this:

    my_box.front = 0;
    my_box.back = 1;
    my_box.left = 1600;
    my_box.top = 480;
    my_box.right = 2240;
    my_box.bottom = -160;

Note front and back. The box is 3d as it says, but since we're working with a 2d text, we want to go 1 pixel deep. As stated in the docs, it's going to be back-front.