GDB cv :: Mat python problem when debugging C ++ programs

When debugging a C ++ OpenCV program, I would like to see the image in my program under GDB, I mean that I would like to visualize the data in GDB. Fortunately, I have:

  • GDB with python support;
  • I installed python 2.7.4, the numpy library and the official version of opencv 2.4.4;
  • I installed the python interface file "cv2.pyd" in the python site-packages folder.

Now I can run a pure python script that loads and displays the image. But my problem arises when I try to display an image from GDB. (The image is in my C ++ program)

#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv; 
...
Mat orgImg = imread("1.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Then I set a breakpoint after that, then GDB hit a breakpoint, I ran such a command on the GDB command line

source test.py

Test.py python script, :

import gdb
import cv2
import numpy

class PlotterCommand(gdb.Command):
    def __init__(self):
        super(PlotterCommand, self).__init__("plot",
                                             gdb.COMMAND_DATA,
                                             gdb.COMPLETE_SYMBOL)
    def invoke(self, arg, from_tty):
        args = gdb.string_to_argv(arg)
        v = gdb.parse_and_eval(args[0])
        t = v.type.strip_typedefs()
        print t
        a = numpy.asarray(v)
        cv2.namedWindow('debugger')
        cv2.imshow('debugger',a)
        cv2.waitKey(0)

PlotterCommand()

plot orgImg

GDB :

cv::Mat
Python Exception <type 'exceptions.TypeError'> mat data type = 17 is not supported: 
Error occurred in Python command: mat data type = 17 is not supported
Error occurred in Python command: mat data type = 17 is not supported

, python GDB "cv:: Mat", python . - ? .

EDIT: script, cv (not cv2), :

import gdb
import cv2.cv as cv

class PlotterCommand(gdb.Command):
    def __init__(self):
        super(PlotterCommand, self).__init__("plot",
                                             gdb.COMMAND_DATA,
                                             gdb.COMPLETE_SYMBOL)
    def invoke(self, arg, from_tty):
        args = gdb.string_to_argv(arg)
        v = gdb.parse_and_eval(args[0])  
        a = cv.CreateImageHeader((v['cols'],v['rows']), cv.IPL_DEPTH_8U, 1)
        cv.SetData(a, v['data'])
        cv.NamedWindow('debugger')
        cv.ShowImage('debugger', a)
        cv.WaitKey(0)

PlotterCommand()

cv.SetData(a, v ['data']) " .

"v" cv:: Mat, :

{flags = 1124024320, dims = 2, rows = 44, cols = 37, data = 0x3ef2d0 '\377' <repeats 200 times>..., refcount = 0x3ef92c, datastart = 0x3ef2d0 '\377' <repeats 200 times>..., dataend = 0x3ef92c "\001", datalimit = 0x3ef92c "\001", allocator = 0x0, size = {p = 0x22fe10}, step = {p = 0x22fe38, buf = {37, 1}}}

, , , , gdb.Value python.

+1
2

, (. )

, ++:

#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace cv; 
...
Mat img = imread("1.jpg", CV_LOAD_IMAGE_GRAYSCALE);
...

GDB , "img". GDB OpenCV Python, python pretty script ( script GPLv3)

1, GDB python 2, OpenCV python ( Windows cv2.pyd) 3, python, numpy

############################################################
#filename: cvplot.py
import gdb
import cv2.cv as cv
import sys


class PlotterCommand(gdb.Command):
    def __init__(self):
        super(PlotterCommand, self).__init__("plot",
                                             gdb.COMMAND_DATA,
                                             gdb.COMPLETE_SYMBOL)
    def invoke(self, arg, from_tty):
        args = gdb.string_to_argv(arg)


        # generally, we type "plot someimage" in the GDB commandline
        # where "someimage" is an instance of cv::Mat
        v = gdb.parse_and_eval(args[0])

        # the value v is a gdb.Value object of C++
        # code cv::Mat, we need to translate to
        # a python object under cv2.cv
        image_size =  (v['cols'],v['rows'])
        # print v
        # these two below lines do not work. I don't know why
        # channel = gdb.execute("call "+ args[0] + ".channels()", False, True)
        # channel = v.channels();
        CV_8U =0
        CV_8S =1
        CV_16U=2
        CV_16S=3
        CV_32S=4
        CV_32F=5
        CV_64F=6
        CV_USRTYPE1=7
        CV_CN_MAX = 512
        CV_CN_SHIFT = 3
        CV_MAT_CN_MASK = (CV_CN_MAX - 1) << CV_CN_SHIFT
        flags = v['flags']
        channel = (((flags) & CV_MAT_CN_MASK) >> CV_CN_SHIFT) + 1
        CV_DEPTH_MAX = (1 << CV_CN_SHIFT)
        CV_MAT_DEPTH_MASK = CV_DEPTH_MAX - 1
        depth = (flags) & CV_MAT_DEPTH_MASK
        IPL_DEPTH_SIGN = 0x80000000
        cv_elem_size = (((4<<28)|0x8442211) >> depth*4) & 15
        if (depth == CV_8S or depth == CV_16S or depth == CV_32S):
                mask = IPL_DEPTH_SIGN
        else:
                mask = 0
        ipl_depth = cv_elem_size*8 | mask     
        img = cv.CreateImageHeader(image_size, ipl_depth, channel)

        # conver the v['data'] type to "char*" type
        char_type = gdb.lookup_type("char")
        char_pointer_type =char_type.pointer()
        buffer = v['data'].cast(char_pointer_type)

        # read bytes from inferior memory, because
        # we run the opencv-python module in GDB own process
        # otherwise, we use memory corss processes        
        buf = v['step']['buf']
        bytes = buf[0] * v['rows'] # buf[0] is the step? Not quite sure.
        inferior = gdb.selected_inferior()
        mem = inferior.read_memory(buffer, bytes)

        # set the img raw data
        cv.SetData(img, mem)

        # create a window, and show the image
        cv.NamedWindow('debugger')
        cv.ShowImage('debugger', img)

        # the below statement is necessory, otherwise, the Window
        # will hang
        cv.WaitKey(0) 

PlotterCommand()
############################################################

script GDB "plot" cv:: Mat. : "source cvplot.py", script GDB, : "plot img", cv:: Mat OpenCV Window, GDB , .

BTW: , "# print v" script, script :

Python Exception <type 'exceptions.UnicodeEncodeError'> 'ascii' codec can't encode characters in position 80-100: ordinal not in range(128): 
Error occurred in Python command: 'ascii' codec can't encode characters in position 80-100: ordinal not in range(128)

"print img" GDB, :

$2 = {flags = 1124024320, dims = 2, rows = 243, cols = 322, data = 0xb85020 "\370\362รจรจรฉ?รจรจ?รจรฉ?รจ?รจ?รจรจรจรจรจรจรจ\372\357รจรจรจรจรจรจรจรจรจรจรจรจรจรจรจ?รจ?รจรจรจรจ???รจรจ?รจรฉรจรจรจ?รจรจ??รจรจรจรฉรจรฉรฉรจรจรจรจรจรจรจรจรจรจรจรจรจรจรจรจ?รจ?รจรจรจรจรจรจรจ?รจรจรจ?รจ"..., refcount = 0xb981c8, datastart = 0xb85020 "\370\362รจรจรฉ?รจรจ?รจรฉ?รจ?รจ?รจรจรจรจรจรจรจ\372\357รจรจรจรจรจรจรจรจรจรจรจรจรจรจรจ?รจ?รจรจรจรจ???รจรจ?รจรฉรจรจรจ?รจรจ??รจรจรจรฉรจรฉรฉรจรจรจรจรจรจรจรจรจรจรจรจรจรจรจรจ?รจ?รจรจรจรจรจรจรจ?รจรจรจ?รจ"..., dataend = 0xb981c6 "\255\272\001", datalimit = 0xb981c6 "\255\272\001", allocator = 0x0, size = {p = 0x22fe64}, step = {p = 0x22fe8c, buf = {322, 1}}}

, , , , , , python . ( WinXP)

Tromey, Andre_, Pmuldoon GDB IRC, Hui Ning , GDB maillist GDB maillist, OpenCV OpenCV GDB

+2

inferior.read_memory pixmap gdb. , Qt Creator, QImage.

+1

All Articles