Converting Between cv::Mat and QImage or QPixmap

In a previous article, I outlined how to compile OpenCV for Mac OS X and to build and run a small example. In that post I used OpenCV‘s High-Level GUI (highgui) module to display a simple interface using the Qt framework. This is great if you’re writing a one-off program or are just experimenting with OpenCV, but how do you interface with your own Qt-based project if you don’t want to (or can’t) use the highgui module?

OpenCV Library

OpenCV Library

+

Qt Library

Qt Library

The first hurdle is converting between data formats so you can work with images in both OpenCV and Qt. So how do you move an image’s data between a cv::Mat and a QImage or QPixmap so you can display it in a widget?

In this article, I present some functions to handle these conversions and explain how to use them.

Continue reading

Compiling OpenCV On Mac OS X 10.6

A couple of years ago I needed to do some basic image processing and found OpenCV. OpenCV is a BSD-licensed library for digital image processing which implements several hundred computer vision algorithms. Unfortunately compiling it on the Mac was not straightforward—requiring Fink or MacPorts—and the one existing Mac framework was out of date and no longer maintained.

OpenCV Library

OpenCV Library

Fast forward to last week. I had another requirement for some image processing magic and I thought I’d check out OpenCV again. What a difference! It’s now really easy to compile and use on the Mac. I didn’t find a writeup online on how to compile on the Mac—the one on the OpenCV site is out of date—so I thought I’d write up a short summary for future reference and any Googlers out there in internet land.

Here’s how I did it…

Continue reading

Point Cloud Library on Mac OS X: A Build Diary

I’ve been using and contributing to various open source projects for almost 20 years. I love the fact that we can leverage each other’s work and help other developers save time instead of reinventing the wheel or fighting the same problem a thousand times over. Open source tools and libraries make what I do possible.  Whenever I use open source, I like to compile from the source so I can contribute back to the projects – even if only little patches for fixing compilation warnings, clarifying error messages, or adding to documentation.  I used to enjoy configuration and build challenges – such as tweaking and compiling my own Linux kernels – but that’s behind me now.  I just want things to work.

I’m a solo developer working on a commercial cross-platform Mac OS X/Windows application.  This means that I have to be careful how I allocate my development time since I also have research to conduct and a business to run.  Part of this is maintaining control of my development “ecosystem”.  The more moving pieces there are – tools, 3rd party libs, etc. – the more overhead, and the less time I have for actual development.  Keeping it simple means saving myself a lot of time.  It also makes it quick and easy to bring short-term contractors on board.

I started to investigate open source tools for processing point clouds for a future version of my software – I want to handle scan data from FARO and Leica scanners.  I quickly zeroed in on the Point Cloud Library which is a BSD-licensed library to read, visualize, manipulate, and process point clouds. It looks like exactly what I need and includes a bunch of capabilities I don’t understand in the least, but seem like they might be useful in the future as I learn about it. Most of the capabilities are too advanced for me to contribute much to them directly, but I thought I’d be able to contribute “around the edges”, so I wanted to build it from source.

Point Cloud Library

Point Cloud Library

This article is a blow-by-blow account of the steps I took while trying to build the Point Cloud Library (PCL) on Mac OS X 10.6 starting with the information from PCL’s Compiling from Source page. The first thing I tried to do is build and link PCL with only the mandatory dependencies. After that, I’ll want to look at the optional pieces and build them if I need them.

As I was working through it, I put together notes for myself on what I did in case I had to do it again or have a contractor do it.  As with most other things I have on this site, I thought at least one other person might benefit through the power of Google, so I decided to put the notes up here verbatim.  Knowing what I know now, I could shorten this dramatically into “Steps to Build PCL on Mac OS X” [even though I haven’t been successful yet], but I decided it might be interesting to show the action-packed sequences of what actually happened…

I hope this is taken in the spirit it’s intended – documentation of some of the problems just getting PCL to configure and build, along with what I hope are some constructive recommendations [at the end of this voluminous tome]. [Warning: I get a little rant-like in the last commentary section – it’s not directed specifically at the PCL, but software development projects in general.]

I have to point out that the PCL guys have been great, answering my initial question quickly, and they’ve been encouraging [so far – hope they still are after this!]. This doesn’t always happen in open source projects, so it’s great to see. They deserve a lot of credit for their work, which is of great value to many  developers and companies worldwide.  All the developers and supporting companies are super-awesome for making this library available as open source. Very much appreciated!

[If you want to skip the gruesome details – don’t forget they’re action-packed! – I list some bugs and recommendations near the end.]

So… here we go!

Continue reading

QTextDocument, HTML, and Unicode: It’s All Greek To Me

To generate reports in my Qt-based software, I create a QTextDocument, which may contain some Unicode characters, convert it to HTML, and then save it to a file.

But the Unicode characters were not being being displayed properly when I opened it in a browser:

Unicode Not Displayed Properly

Unicode Not Displayed Properly

Taking a look at the generated HTML, I noticed that the content encoding was not being set:

So I thought I could simply specify the encoding in the call to toHtml() which would set the encoding in the HTML header.

That looks better! But wait…

Unicode Still Not Displayed Properly

Unicode Still Not Displayed Properly

Uhhh… That looks worse.

After scouring the interwebs, I eventually found the answer. The QTextStream encodes based on the system locale so if you don’t set it explicitly you might not get what you expect. According to the QTextStream docs:

By default, QTextCodec::codecForLocale() is used, and automatic unicode detection is enabled.

For some reason the automatic detection did not work for my case, but the solution is to set the encoding manually using QTextStream::setCodec() like this:

Aha! Now my Greek looked Greek!

Unicode Displayed Properly

Unicode Displayed Properly

In-memory ZIP File Access Using Qt

A while ago I was working on a little tool to manage my media files – TV shows and movies that I’d ripped from my DVD collection that I wanted to make available through XBMC on my home theater system. To display things like plot, characters, runtime, etc., XBMC reads NFO files – which are just XML files – one for each media file. Entering this information by hand is not a fun afternoon activity, so I looked for a way to automate it. I found a couple of different sites that provided the data I needed via APIs and eventually settled on TheTVDB.com for TV series and themoviedb.org for movies. So all I needed to do was read the data using the APIs and write it to the NFO files. Simple!

For each TV series, TheTVDB provides a ZIP file containing several XML files which splits up the information to make it more manageable and allows for easier internationalization. A typical ZIP from TheTVDB contains files such as actors.xml, banners.xml, and en.xml. The actors.xml file contains a list of actors, the roles they play, a link to an image, etc.. banners.xml provides links to fan art and thumbnails which XBMC can use for display. en.xml provides the core data: a description of the TV series and a list of all the episodes from the series including plot, runtime, air date, etc. for each of them.

So what’s the best way to download and access this zipped data? Continue reading

QString::toStdString, QString::fromStdString, and -no-stl

I use the Qt library in my day-to-day development work. I build my Qt libraries directly from the git repository and they are configured specifically for my projects. On the Mac OS X side, this is the command line I use:

The Windows one is similar:

You will notice that I am explicitly removing a whole bunch of stuff – SVG, WebKit, audio, etc.. The time it takes to build the Qt libraries is not insignificant and since I typically track the git repo, I don’t want to waste time building things I will not be using in my projects. The WebKit code takes an extraordinary amount of time to build, for example. I also don’t want to build things into the libs that I’m not going to be using for my commercial software – such as STL. One of the problems this poses, however, is how to handle build problems with other projects I want to build, for example the GUI for the open-source Cppcheck program.

For the most part the projects I’m interested in don’t use any of the capabilities I’m eliminating from my Qt build, but one option has been problematic for a couple of projects: -no-stl. This is because the projects use the functions QString::toStdString() and QString::fromStdString() which do not exist when you build Qt with the -no-stl option. While STL may be available to the source you are building, it was not compiled into the Qt libs, so this causes an error.

When I try to build Cppcheck using my own Qt build, I get errors like this:

../gui/mainwindow.cpp: In member function ‘Settings MainWindow::GetCppcheckSettings()’: ../gui/mainwindow.cpp:457: error: ‘class QString’ has no member named ‘toStdString’

../gui/threadresult.cpp: In member function ‘virtual void ThreadResult::reportOut(const std::string&)’: ../gui/threadresult.cpp:42: error: ‘fromStdString’ is not a member of ‘QString’

Solutions?

Continue reading

Cppcheck

Cppcheck is an open source cool tool by Daniel Marjamäki used to statically analyse C/C++ code for errors, questionable code, and style. I’ve been tracking it for a number of months and, while it is still in active development, it is already a very useful tool. There are command line and GUI versions available for several platforms.

The Cppcheck wiki page gives a lot more information about it, and lists a couple of hundred checks that are performed on your code.

A git repository is available which makes it easy to keep up-to-date with the latest developments. The source also includes a Qt-based GUI which is quite straightforward to compile using Qt Creator. The GUI is functional, but could use a little bit of work to make it more friendly. In order to view error or warning in the source, for example, Cppcheck requires an external program. It really ought to show you the code in context in the main window, allowing you to correct things within the application.

Continue reading