image Watch: viewing in-memory images in the Visual Studio debugger

本文介绍如何利用Visual Studio的ImageWatch插件调试OpenCV图像处理应用,通过实例演示了加载图片、运行边缘检测等操作,并展示了如何查看内存中图像的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

image Watch: viewing in-memory images in the Visual Studio debugger

Image Watch is a plug-in for Microsoft Visual Studio that lets you to visualize in-memory images (cv::Mat or IplImage_ objects, for example) while debugging an application. This can be helpful for tracking down bugs, or for simply understanding what a given piece of code is doing.

Prerequisites

This tutorial assumes that you have the following available:

  1. Visual Studio 2012 Professional (or better) with Update 1 installed. Update 1 can be downloaded here.
  2. An OpenCV installation on your Windows machine (Tutorial: Installation in Windows).
  3. Ability to create and build OpenCV projects in Visual Studio (Tutorial: How to build applications with OpenCV inside the Microsoft Visual Studio).

Installation

Download the Image Watch installer. The installer comes in a single file with extension .vsix (Visual Studio Extension). To launch it, simply double-click on the .vsix file in Windows Explorer. When the installer has finished, make sure to restart Visual Studio to complete the installation.

Example

Image Watch works with any existing project that uses OpenCV image objects (for example, cv::Mat). In this example, we use a minimal test program that loads an image from a file and runs an edge detector. To build the program, create a console application project in Visual Studio, name it “image-watch-demo”, and insert the source code below.

// Test application for the Visual Studio Image Watch Debugger extension

#include <iostream>                        // std::cout
#include <opencv2/core/core.hpp>           // cv::Mat
#include <opencv2/highgui/highgui.hpp>     // cv::imread()
#include <opencv2/imgproc/imgproc.hpp>     // cv::Canny()

using namespace std;
using namespace cv;

void help()
{
    cout
        << "----------------------------------------------------" << endl
        << "This is a test program for the Image Watch Debugger " << endl
        << "plug-in for Visual Studio. The program loads an     " << endl
        << "image from a file and runs the Canny edge detector. " << endl
        << "No output is displayed or written to disk."
        << endl
        << "Usage:"                                               << endl
        << "image-watch-demo inputimage"                          << endl
        << "----------------------------------------------------" << endl
        << endl;
}

int main(int argc, char *argv[])
{
    help();

    if (argc != 2)
    {
        cout << "Wrong number of parameters" << endl;
        return -1;
    }

    cout << "Loading input image: " << argv[1] << endl;
    Mat input;
    input = imread(argv[1], CV_LOAD_IMAGE_COLOR);

    cout << "Detecting edges in input image" << endl;
    Mat edges;
    Canny(input, edges, 10, 100);

    return 0;
}

Make sure your active solution configuration (Build ‣ Configuration Manager) is set to a debug build (usually called “Debug”). This should disable compiler optimizations so that viewing variables in the debugger can work reliably.

Build your solution (Build ‣ Build Solution, or press F7).

Before continuing, do not forget to add the command line argument of your input image to your project (Right click on project ‣ Properties ‣ Configuration Properties ‣ Debugging and then set the field Command Arguments with the location of the image).

Now set a breakpoint on the source line that says

Mat edges;

To set the breakpoint, right-click on the source line and select Breakpoints ‣ Insert Breakpoint from the context menu.

Launch the program in the debugger (Debug ‣ Start Debugging, or hit F5). When the breakpoint is hit, the program is paused and Visual Studio displays a yellow instruction pointer at the breakpoint:

../../../../_images/breakpoint.png

Now you can inspect the state of you program. For example, you can bring up the Locals window (Debug ‣ Windows ‣ Locals), which will show the names and values of the variables in the current scope:

../../../../_images/vs_locals.png

Note that the built-in Locals window will display text only. This is where the Image Watch plug-in comes in. Image Watch is like another Locals window, but with an image viewer built into it. To bring up Image Watch, select View ‣ Other Windows ‣ Image Watch. Like Visual Studio’s Locals window, Image Watch can dock to the Visual Studio IDE. Also, Visual Studio will remember whether you had Image Watch open, and where it was located between debugging sessions. This means you only have to do this once–the next time you start debugging, Image Watch will be back where you left it. Here’s what the docked Image Watch window looks like at our breakpoint:

../../../../_images/toolwindow.jpg

The radio button at the top left (Locals/Watch) selects what is shown in the Image List below: Locals lists all OpenCV image objects in the current scope (this list is automatically populated). Watch shows image expressions that have been pinned for continuous inspection (not described here, see Image Watch documentation for details). The image list shows basic information such as width, height, number of channels, and, if available, a thumbnail. In our example, the image list contains our two local image variables, input and edges.

If an image has a thumbnail, left-clicking on that image will select it for detailed viewing in the Image Viewer on the right. The viewer lets you pan (drag mouse) and zoom (mouse wheel). It also displays the pixel coordinate and value at the current mouse position.

../../../../_images/viewer.jpg

Note that the second image in the list, edges, is shown as “invalid”. This indicates that some data members of this image object have corrupt or invalid values (for example, a negative image width). This is expected at this point in the program, since the C++ constructor for edges has not run yet, and so its members have undefined values (in debug mode they are usually filled with “0xCD” bytes).

From here you can single-step through your code (Debug->Step Over, or press F10) and watch the pixels change: if you step once, over the Mat edges; statement, theedges image will change from “invalid” to “empty”, which means that it is now in a valid state (default constructed), even though it has not been initialized yet (using cv::Mat::create(), for example). If you make one more step over the cv::Canny() call, you will see a thumbnail of the edge image appear in the image list.

Now assume you want to do a visual sanity check of the cv::Canny() implementation. Bring the edges image into the viewer by selecting it in the Image List and zoom into a region with a clearly defined edge:

../../../../_images/edges_zoom.png

Right-click on the Image Viewer to bring up the view context menu and enable Link Views (a check box next to the menu item indicates whether the option is enabled).

../../../../_images/viewer_context_menu.png

The Link Views feature keeps the view region fixed when flipping between images of the same size. To see how this works, select the input image from the image list–you should now see the corresponding zoomed-in region in the input image:

../../../../_images/input_zoom.png

You may also switch back and forth between viewing input and edges with your up/down cursor keys. That way you can easily verify that the detected edges line up nicely with the data in the input image.

More ...

Image watch has a number of more advanced features, such as

  1. pinning images to a Watch list for inspection across scopes or between debugging sessions
  2. clamping, thresholding, or diff’ing images directly inside the Watch window
  3. comparing an in-memory image against a reference image from a file

Please refer to the online Image Watch Documentation for details–you also can get to the documentation page by clicking on the Help link in the Image Watch window:

../../../../_images/help_button.jpg

EnMAP-Box是一款高效、便捷的遥感图像处理软件,其独特之处在于它是一个免安装的应用程序,用户可以直接运行而无需进行复杂的安装过程。这款工具主要用于处理和分析来自各种遥感传感器的数据,如EnMAP(环境多波段光谱成像仪)和其他同类设备获取的高光谱图像。EnMAP-Box的设计目标是为科研人员和实践工作者提供一个直观、易用的平台,以执行复杂的遥感数据处理任务。 在使用EnMAP-Box之前,一个关键的前提条件是需要有一个兼容的IDL(Interactive Data Language)环境。IDL是一种强大的编程语言,特别适用于科学数据的处理和可视化,尤其是在地球科学和遥感领域。它提供了丰富的库函数,支持对多维数组操作,这使得它成为处理遥感图像的理想选择。EnMAP-Box是基于IDL开发的,因此,用户在使用该软件之前需要确保已经正确配置了IDL环境。 EnMAP-Box的主要功能包括: 1. 数据导入:能够读取多种遥感数据格式,如ENVI、HDF、GeoTIFF等,方便用户将不同来源的遥感图像导入到软件中进行分析。 2. 预处理:提供辐射校正、大气校正、几何校正等功能,用于改善原始图像的质量,确保后续分析的准确性。 3. 分光分析:支持高光谱图像的光谱特征提取,如光谱指数计算、光谱端元分离等,有助于识别地物类型和监测环境变化。 4. 图像分类:通过监督或非监督方法进行图像分类,可以自动或半自动地将图像像素划分为不同的地物类别。 5. 时间序列分析:对于多时相遥感数据,EnMAP-Box能进行时间序列分析,揭示地表动态变化趋势。 6. 结果导出与可视化:处理后的结果可以导出为各种格式,同时软件内置了图像显示和地图投影功能,帮助用户直观地查看和理解处理结果。 7. 自定义脚本:利用IDL的强大功能,用户可以编写自定义脚本来实现特定的遥感处理需求,增强了软件的灵活性和可扩展性。 在使用EnMAP-Box的过程中,用户可能会遇到一些挑战,例如对IDL编程语言不熟悉,或者对遥感数据处理的基本概念和方法缺乏了解。这时,可以通过查阅软件自带的文档、教程,以及在线资源来提升技能。同时,积极参与相关的学习社区和论坛,与其他用户交流经验,可以帮助解决遇到的问题。 EnMAP-Box作为一款基于IDL的遥感图像处理工具,为遥感数据分析提供了便利,但需要用户具备一定的IDL基础和遥感知识。通过熟练掌握EnMAP-Box,用户可以高效地处理和解析遥感数据,揭示地表信息,为环境保护、资源管理等领域提供科学支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值