When debugging a C++ OpenCV program, I'd like to see an image in my program under GDB, I mean I would like to visualize the data under GDB. Luckily I have:
- GDB with python support;
- I have installed python 2.7.4, numpy library, and opencv official release 2.4.4;
- I have installed the python interface file "cv2.pyd" to my python's site-packages folder.
Now, I can run a pure python script which load and show an image. But my issue comes when I try to show 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 the breakpoint, I run such command in GDB's command line
source test.py
The test.py is a python script which try to show an image:
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()
After that, I just run the command
plot orgImg
But GDB get an error:
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
You see, the python object under GDB is "cv::Mat", but it can not to converted to a correct python object to show. Anyone can help me? Thanks.
EDIT: I try to create a more simple script which use cv (not cv2), but it still not work:
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()
The above code does not work as the statement "cv.SetData(a, v['data'])" does not really do an buffer address assignment.
The "v" is a representation of cv::Mat, which has the contents:
{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}}}
So, you see the "data" field is the raw buffer pointer, but I'm not sure how to transfer this gdb.Value to a python buffer type.