├── .gitignore ├── CMakeLists.txt ├── cvplot.py ├── lena.png ├── main.cpp └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | Debug/ 2 | readme.html -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project( viewer ) 3 | find_package( OpenCV REQUIRED ) 4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") 5 | set(CMAKE_BUILD_TYPE Debug) 6 | add_executable( viewer main.cpp) 7 | target_link_libraries( viewer ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /cvplot.py: -------------------------------------------------------------------------------- 1 | #filename: cvplot.py 2 | #from: https://sourceware.org/ml/gdb/2013-04/msg00104.html 3 | import gdb 4 | import cv2.cv as cv 5 | import sys 6 | #from subprocess import call 7 | 8 | class PlotterCommand(gdb.Command): 9 | def __init__(self): 10 | super(PlotterCommand, self).__init__("plot", 11 | gdb.COMMAND_DATA, 12 | gdb.COMPLETE_SYMBOL) 13 | def invoke(self, arg, from_tty): 14 | args = gdb.string_to_argv(arg) 15 | 16 | # generally, we type "plot someimage" in the GDB commandline 17 | # where "someimage" is an instance of cv::Mat 18 | v = gdb.parse_and_eval(args[0]) 19 | 20 | # the value v is a gdb.Value object of C++ 21 | # code's cv::Mat, we need to translate to 22 | # a python object under cv2.cv 23 | image_size = (v['cols'],v['rows']) 24 | # print v 25 | # these two below lines do not work. I don't know why 26 | # channel = gdb.execute("call "+ args[0] + ".channels()", False, True) 27 | # channel = v.channels(); 28 | CV_8U =0 29 | CV_8S =1 30 | CV_16U=2 31 | CV_16S=3 32 | CV_32S=4 33 | CV_32F=5 34 | CV_64F=6 35 | CV_USRTYPE1=7 36 | CV_CN_MAX = 512 37 | CV_CN_SHIFT = 3 38 | CV_MAT_CN_MASK = (CV_CN_MAX - 1) << CV_CN_SHIFT 39 | flags = v['flags'] 40 | channel = (((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1 41 | CV_DEPTH_MAX = (1 << CV_CN_SHIFT) 42 | CV_MAT_DEPTH_MASK = CV_DEPTH_MAX - 1 43 | depth = (flags) & CV_MAT_DEPTH_MASK 44 | IPL_DEPTH_SIGN = 0x80000000 45 | cv_elem_size = (((4<<28)|0x8442211) >> depth*4) & 15 46 | if (depth == CV_8S or depth == CV_16S or depth == CV_32S): 47 | mask = IPL_DEPTH_SIGN 48 | else: 49 | mask = 0 50 | ipl_depth = cv_elem_size*8 | mask 51 | img = cv.CreateImageHeader(image_size, ipl_depth, channel) 52 | 53 | # conver the v['data'] type to "char*" type 54 | char_type = gdb.lookup_type("char") 55 | char_pointer_type =char_type.pointer() 56 | buffer = v['data'].cast(char_pointer_type) 57 | 58 | # read bytes from inferior's memory, because 59 | # we run the opencv-python module in GDB's own process 60 | # otherwise, we use memory corss processes 61 | buf = v['step']['buf'] 62 | bytes = buf[0] * v['rows'] # buf[0] is the step? Not quite sure. 63 | inferior = gdb.selected_inferior() 64 | mem = inferior.read_memory(buffer, bytes) 65 | 66 | # set the img's raw data 67 | cv.SetData(img, mem) 68 | 69 | # create a window, and show the image 70 | cv.StartWindowThread() 71 | cv.NamedWindow('viewer') 72 | cv.ShowImage('viewer', img) 73 | 74 | # the below statement is necessory, otherwise, the Window 75 | # will hang 76 | cv.WaitKey(0) 77 | cv.DestroyWindow('viewer') 78 | 79 | # save matrix as an xml file and open it with matrix viewer 80 | # cv.Save("/tmp/dump.xml", img, "matrix") 81 | # call(["matrix-viewer", "/tmp/dump.xml"]) 82 | # cv.SaveImage("/tmp/viewer.png", img) 83 | # call(["matrix-viewer", "/tmp/viewer.png"]) 84 | 85 | PlotterCommand() -------------------------------------------------------------------------------- /lena.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beenfrog/gdb-opencv-viewer/ec23c56a1ff2c4cbec547e6b52c4b634b35bd6a7/lena.png -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | Mat img_color, img_gray; 11 | img_color = imread("../lena.png"); 12 | cvtColor(img_color, img_gray, CV_BGR2GRAY); 13 | 14 | int arr[] = {1,2,3,4,5,6}; 15 | vector vec(arr, arr+6); 16 | 17 | map str_int_map; 18 | str_int_map[string("one")] = 1; 19 | str_int_map[string("two")] = 2; 20 | str_int_map[string("three")] = 3; 21 | 22 | vector img_vec; 23 | img_vec.push_back(img_color); 24 | img_vec.push_back(img_gray); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Description 2 | View OpenCV Mat in GDB, Code from: [Visualize in memory OpenCV image or matrix from GDB pretty printers](https://sourceware.org/ml/gdb/2013-04/msg00104.html) 3 | 4 | ## How to use 5 | * Required packages: opencv, python wrapper for opencv (cv2), python, numpy 6 | * Clone this repository 7 | ``` 8 | git clone git@github.com:beenfrog/gdb-opencv-viewer.git 9 | ``` 10 | * Add the follow lines in ~/.gdbinit. The alias command is optional. 11 | ``` 12 | source /FULL-PATH-OF-THE-FILE/cvplot.py 13 | alias view = plot 14 | alias imshow = plot 15 | ``` 16 | * Usage 17 | ``` 18 | plot m, the m is an object of cv2::Mat 19 | view m, if you set alias view = plot 20 | imshow m, if you set alias imshow = plot 21 | ``` 22 | 23 | ## Demo 24 | * debug and view image in gdb: 25 | ``` 26 | cd gdb-opencv-viewer 27 | mkdir Debug && cd Debug 28 | cmake -DCMAKE_BUILD_TYPE=Debug .. && make 29 | gdb ./viewer 30 | break 26 31 | run 32 | plot img_color 33 | plot img_gray 34 | ``` 35 | * view Mat in vector: 36 | if just use the follow command: 37 | ``` 38 | plot img_vec[0] 39 | plot img_vec[1] 40 | ``` 41 | we will get: 42 | ``` 43 | Python Exception Could not find operator[].: 44 | Error occurred in Python command: Could not find operator[]. 45 | ``` 46 | and the right way to view the mat in vector is: 47 | ``` 48 | pvector img_vec 49 | plot $1 50 | plot $2 51 | ``` 52 | See more in next section. 53 | 54 | * NOTE: There may be some bugs with the imshow in python under GNU/Linux, which make you unable to close the image by click the "close" button on the dialog, but you can just click the image and press any key to close the image dialog. 55 | 56 | ## Others 57 | It is easy to print the C++ STL elements such as vector, map with [GDB evaluators/views/utilities](http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt) 58 | 59 | * Install 60 | ``` 61 | wget http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt 62 | cat dbinit_stl_views-1.03.txt >> ~/.gdbinit 63 | ``` 64 | 65 | * Demo (continue from the previous demo) 66 | ``` 67 | pvector vec 68 | pmap str_int_map char* int 69 | ... 70 | see more from the raw file: dbinit_stl_views-1.03.txt 71 | ``` 72 | 73 | 74 | ## Similar Projects to Show Image in GDB 75 | * [gdb-imshow](https://github.com/renatoGarcia/gdb-imshow) 76 | * [GDB-ImageWatch](https://github.com/cuekoo/GDB-ImageWatch) 77 | * [matrix-viewer](https://github.com/crep4ever/matrix-viewer) 78 | * [gdb-imagewatch](https://github.com/csantosbh/gdb-imagewatch) --------------------------------------------------------------------------------