2

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.

273K
  • 29,503
  • 10
  • 41
  • 64
Cesar
  • 41
  • 2
  • 5
  • 16
  • 1
    Looks like Caleth's answer to [this question](https://stackoverflow.com/questions/53073277/watching-map-entries-in-vs-debugger) should sort you out. Basically, VS is protecting you from mutating the map, and a good thing too. – Paul Sanders Aug 04 '23 at 01:35
  • PS, bit of an [XY question](https://en.m.wikipedia.org/wiki/XY_problem) btw, you started out with a false assumption and it snowballed from there. – Paul Sanders Aug 04 '23 at 01:40
  • file an issue with the makers of the tool you are using. this is not strictly a programming question. – Christoph Rackwitz Aug 04 '23 at 09:59
  • 1
    @ChristophRackwitz I don't think there's any need. The link in my first comment should provide the OP with a solution. With the way he's trying to do it currently, VS is behaving 'as designed' (to stop you from accidentally mutating the map). Caleth makes it all very clear. – Paul Sanders Aug 04 '23 at 10:37
  • 3
    Do you see in the gif that i already tried map.at? `"file an issue with the makers of the tool you are using. this is not strictly a programming question."` Isn't natvis suitable for this? – Cesar Aug 04 '23 at 12:19

1 Answers1

0

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.

mr NAE
  • 3,144
  • 1
  • 15
  • 35
  • This will be helpful, I'm also using qt, i have tried importing all nativs in the repo, but it still not working when i input the map key, im missing something, how you got it? – Cesar Aug 07 '23 at 14:49
  • Natvis is a very strange system. Try to implement `std::vector` is it work? When you try to get item by map["a"] the natvis can't take it, because operator[] is not const. So, as I guess, you have to reimplement natvis for std::map and add [this](https://github.com/aleksey-nikolaev/natvis-collection/blob/master/OpenCV.natvis#L6-L8) for `std::map<std::string,cv::Mat>`. This is very hard to do. – mr NAE Aug 16 '23 at 14:09