How to create a specific view using natvis debug visualizer?

381 Views Asked by At

I'm using the Microsoft extension Image Watch to preview OpenCV images.

I'm trying to visualize images that are inside a container by adding their container key, but the Visual Studio debugger watch window doesn't support directly adding a variable inside a container.

For example:

#include <opencv2/opencv.hpp>

struct MyStruct {
    cv::Mat img;
};

int main() {

    cv::Mat img(64, 64, CV_8UC3, cv::Scalar(0, 0, 255));
    map["a"].img = img;
    cv::Mat myImg = map["a"].img;
}

When I try to visualize the image by adding into the watch window map["a"].img, it's invalid.

I need to add it from inspecting/iterating the map, until find the desired key:

enter image description here

It only works entering this name: ((std::_Tree_node<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const ,MyStruct>,void *>*)0x0000026e1a358a70)->_Myval.second.img

After a lot of searches, I have found this: Natvis framework.

I wonder if it could work for this use case, I have never used natvis before.

I followed the tutorial and added a new .natvis to my project:

Project > Add new item > Visual C++ > Utility > Debugger visualization file (.natvis).

<?xml version="1.0" encoding="utf-8"?> 
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010"> 
</AutoVisualizer>

And now I'm completely lost, I have re-read the examples but I still couldn't figure out how to create the specific view to be able to preview the image by its key, i.e. map["a"].img.

1

There are 1 best solutions below

2
mr NAE On

When I tried to visualize the OpenCV images I stack on same problem. I found that natvis from this repository are very helpful.

In your case Image Watch extension can't deduce the type of nested object (In the Watch window), it is a very simple system. You have to do something like that on <Type Name="std::map&lt;std::string,MyStruct&gt;"> and <Type Name="MyStruct">. But in this case you need to know how STL containers shown

Additionally, problem may not only in the Image Watch extension, but in Debugger system. Check your debug natvis messages. Also, sometime, debbuger throw an exceptions when you trace step by step on uninitialized objects such as cv::Mat.