Interactive Visual Debugging of Computer Vision applications
Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Interactive Visual Debugging of Computer Vision applications
What is the most common way to debug computer vision applications? Usually the answer is temporary, hacked together, custom code that must be removed from the code for release compilation.
In this tutorial we will show how to use the visual debugging features of the cvv module (opencv2/cvv.hpp) instead.
In this tutorial you will learn how to:
Add cvv debug calls to your application
Use the visual debug GUI
Enable and disable the visual debug features during compilation (with zero runtime overhead when disabled)
Code
The example code
captures images (videoio), e.g. from a webcam,
applies some filters to each image (imgproc),
detects image features and matches them to the previous image (features2d).
If the program is compiled without visual debugging (see CMakeLists.txt below) the only result is some information printed to the command line. We want to demonstrate how much debugging or development functionality is added by just a few lines of cvv commands.
// system includes
#include <iostream>
// library includes
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/videoio.hpp>
#defineCVVISUAL_DEBUGMODE
#include <opencv2/cvv/debug_mode.hpp>
#include <opencv2/cvv/show_image.hpp>
#include <opencv2/cvv/filter.hpp>
#include <opencv2/cvv/dmatch.hpp>
#include <opencv2/cvv/final_show.hpp>
usingnamespacestd;
usingnamespacecv;
template<classT> std::stringtoString(constT& p_arg)
{
std::stringstreamss;
ss << p_arg;
returnss.str();
}
intmain(intargc, char** argv)
{
// parser keysconstchar *keys =
"{ help h usage ? | | show this message }""{ width W | 0| camera resolution width. leave at 0 to use defaults }""{ height H | 0| camera resolution height. leave at 0 to use defaults }";
CommandLineParserparser(argc, argv, keys);
if (parser.has("help")) {
parser.printMessage();
return0;
}
intres_w = parser.get<int>("width");
intres_h = parser.get<int>("height");
// setup video capturecv::VideoCapturecapture(0);
if (!capture.isOpened()) {
std::cout << "Could not open VideoCapture" << std::endl;
return1;
}
if (res_w>0 && res_h>0) {
printf("Setting resolution to %dx%d\n", res_w, res_h);
capture.set(cv::CAP_PROP_FRAME_WIDTH, res_w);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, res_h);
}
cv::MatprevImgGray;
std::vector<cv::KeyPoint> prevKeypoints;
cv::MatprevDescriptors;
intmaxFeatureCount = 500;
Ptr<ORB> detector = ORB::create(maxFeatureCount);
cv::BFMatchermatcher(cv::NORM_HAMMING);
for (intimgId = 0; imgId < 10; imgId++) {
// capture a framecv::MatimgRead;
capture >> imgRead;
printf("%d: image captured\n", imgId);
std::stringimgIdString{"imgRead"};
imgIdString += toString(imgId);
cvv::showImage(imgRead, CVVISUAL_LOCATION, imgIdString.c_str());
// convert to grayscalecv::MatimgGray;
cv::cvtColor(imgRead, imgGray, COLOR_BGR2GRAY);
cvv::debugFilter(imgRead, imgGray, CVVISUAL_LOCATION, "to gray");
// detect ORB featuresstd::vector<cv::KeyPoint> keypoints;
cv::Matdescriptors;
detector->detectAndCompute(imgGray, cv::noArray(), keypoints, descriptors);
printf("%d: detected %zd keypoints\n", imgId, keypoints.size());
// match them to previous image (if available)if (!prevImgGray.empty()) {
std::vector<cv::DMatch> matches;
matcher.match(prevDescriptors, descriptors, matches);
printf("%d: all matches size=%zd\n", imgId, matches.size());
std::stringallMatchIdString{"all matches "};
allMatchIdString += toString(imgId-1) + "<->" + toString(imgId);
cvv::debugDMatch(prevImgGray, prevKeypoints, imgGray, keypoints, matches, CVVISUAL_LOCATION, allMatchIdString.c_str());
// remove worst (as defined by match distance) bestRatio quantiledoublebestRatio = 0.8;
std::sort(matches.begin(), matches.end());
matches.resize(int(bestRatio * matches.size()));
printf("%d: best matches size=%zd\n", imgId, matches.size());
std::stringbestMatchIdString{"best " + toString(bestRatio) + " matches "};
bestMatchIdString += toString(imgId-1) + "<->" + toString(imgId);
cvv::debugDMatch(prevImgGray, prevKeypoints, imgGray, keypoints, matches, CVVISUAL_LOCATION, bestMatchIdString.c_str());
}
prevImgGray = imgGray;
prevKeypoints = keypoints;
prevDescriptors = descriptors;
}
cvv::finalShow();
return0;
}
We compile the program either using the above CmakeLists.txt with Option CVV_DEBUG_MODE=ON (cmake -DCVV_DEBUG_MODE=ON) or by adding the corresponding define CVVISUAL_DEBUGMODE to our compiler (e.g. g++ -DCVVISUAL_DEBUGMODE).
The first cvv call simply shows the image (similar to imshow) with the imgIdString as comment.
The image is added to the overview tab in the visual debug GUI and the cvv call blocks.
image
The image can then be selected and viewed
image
Whenever you want to continue in the code, i.e. unblock the cvv call, you can either continue until the next cvv call (Step), continue until the last cvv call (*>>*) or run the application until it exists (Close).
We decide to press the green Step button.
The next cvv calls are used to debug all kinds of filter operations, i.e. operations that take a picture as input and return a picture as output.
If you open the filter call, you will end up in the so called "DefaultFilterView". Both images are shown next to each other and you can (synchronized) zoom into them.
image
When you go to very high zoom levels, each pixel is annotated with its numeric values.
image
We press Step twice and have a look at the dilated image.
Now we use the View selector in the top right and select the "DualFilterView". We select "Changed Pixels" as filter and apply it (middle image).
image
After we had a close look at these images, perhaps using different views, filters or other GUI features, we decide to let the program run through. Therefore we press the yellow *>>* button.
The program will block at
cvv::finalShow();
and display the overview with everything that was passed to cvv in the meantime.
image
The cvv debugDMatch call is used in a situation where there are two images each with a set of descriptors that are matched to each other.
We pass both images, both sets of keypoints and their matching to the visual debug module.
Since we want to have a look at matches, we use the filter capabilities (*#type match*) in the overview to only show match calls.
image
We want to have a closer look at one of them, e.g. to tune our parameters that use the matching. The view has various settings how to display keypoints and matches. Furthermore, there is a mouseover tooltip.
image
We see (visual debugging!) that there are many bad matches. We decide that only 70% of the matches should be shown - those 70% with the lowest match distance.
image
Having successfully reduced the visual distraction, we want to see more clearly what changed between the two images. We select the "TranslationMatchView" that shows to where the keypoint was matched in a different way.
image
It is easy to see that the cup was moved to the left during the two images.
Although, cvv is all about interactively seeing the computer vision bugs, this is complemented by a "RawView" that allows to have a look at the underlying numeric data.
image
There are many more useful features contained in the cvv GUI. For instance, one can group the overview tab.
image
Result
By adding a view expressive lines to our computer vision program we can interactively debug it through different visualizations.
Once we are done developing/debugging we do not have to remove those lines. We simply disable cvv debugging (cmake -DCVV_DEBUG_MODE=OFF or g++ without -DCVVISUAL_DEBUGMODE) and our programs runs without any debug overhead.