Disable OpenCV Logging

When we use OpenCV from command line process, sometimes OpenCV will print internal logging messages such as:

[ INFO:0] global .\opencv-master\sources\modules\core\src\parallel\registry_parallel.impl.hpp (90) cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(2, sorted by priority): TBB(1000); OPENMP(990)
[ INFO:0] global .\opencv-master\sources\modules\core\include\opencv2/core/parallel/backend/parallel_for.tbb.hpp (54) cv::parallel::tbb::ParallelForBackend::ParallelForBackend Initializing TBB parallel backend: TBB_INTERFACE_VERSION=12010
[ INFO:0] global .\opencv-master\sources\modules\core\src\parallel\parallel.cpp (73) cv::parallel::createParallelForAPI core(parallel): using backend: TBB (priority=1000)

This might be useful while debugging OpenCV problems but it is a noise if we want to print our own messages to the console. In this case, it is desirable to disable the internal OpenCV logging using the following code:

#include "opencv2/core/utils/logger.hpp"

int main()
{
    cv::utils::logging::setLogLevel(cv::utils::logging::LogLevel::LOG_LEVEL_SILENT);

    return 1;
}

History of All Visual C++ Versions

ProductRelease dateC++ version_MSC_VER
Visual Studio 2019 16.1110-Aug-2114.291929
Visual Studio 2019 16.1025-May-2114.291929
Visual Studio 2019 16.902-Mar-2114.281928
Visual Studio 2019 16.910-Nov-2014.281928
Visual Studio 2019 16.705-Aug-2014.271927
Visual Studio 2019 16.619-May-2014.261926
Visual Studio 2019 16.516-Mar-2014.251925
Visual Studio 2019 16.403-Dec-1914.241924
Visual Studio 2019 16.323-Sep-1914.231923
Visual Studio 2019 16.224-Jul-1914.221922
Visual Studio 2019 16.121-May-1914.211921
Visual Studio 2019 16.002-Apr-1914.21920
Visual Studio 2017 15.913-Nov-1814.161916
Visual Studio 2017 15.814-Aug-1814.151915
Visual Studio 2017 15.707-May-1814.141914
Visual Studio 2017 15.605-Mar-1814.131913
Visual Studio 2017 15.504-Dec-1714.121912
Visual Studio 2017 15.409-Oct-1714.111911
Visual Studio 2017 15.314-Aug-1714.111911
Visual Studio 2017 15.210-May-1714.11910
Visual Studio 2017 15.105-Apr-1714.11910
Visual Studio 201707-Mar-1714.11910
Visual Studio 201520-Jul-15141900
Visual Studio 201317-Oct-13121800
Visual Studio 201215-Aug-12111700
Visual Studio 201002-Jul-05101600
Visual Studio 200830-Jun-0591500
Visual Studio 200527-Jun-0581400

Git Command Reference

Git Config

# Set name and email you want attached to your commit transactions
git config --global user.name "[name]"
git config --global user.email "[email address]"

# Enables helpful colorization of command line output
git config --global color.ui auto

# Configure kdiff3 as the merge tool on Windows.
git config --global merge.tool kdiff3
git config --global mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
git config --global mergetool.kdiff3.trustExitCode false

Git Branch

git branch -d localBranchName               # Delete branch locally
git push origin --delete remoteBranchName   # Delete branch remotely
git remote prune origin                     # Delete all local branches not on remote
git push --set-upstream origin BRANCH_NAME  # Push local branch to remote; will create a pull request.
git branch --sort=-committerdate            # Print branches ordered by the commit dates.
git branch -vv                              # Show the last local and remote commit on each branch.

Git Checkout

git checkout @{-N}  # Go back N branches.
git checkout -      # Go to the last branch (shorthand to  for the previous command)

Git Diff

git diff                                   # changed but not staged.
git diff --staged                          # staged and ignore local changes.
git diff --cached                          # same as staged.
git difftool                               # use external diff tool.
git diff $startCommit..$endCommit -- FILE  # Compare changes in one file from startCommit to endCommit.
git diff $startCommit..$endCommit          # Compare two commits.

Git Commit

git commit                  # open external editor for commit message
git commit -m "message"
git commit -a -m "message"  # ignore staging and commit all modified files
git commit --amend          # Fix last commit message.

# Add a file missed in the last commit.
git add missed_file.txt
git commit --amend

# Remove a file after committing:
git reset --soft HEAD~1
git reset accidently_added_file.jpg
rm accidently_added_file.jpg
git commit

Git Stash

git stash        # Store the current changes in stash.
git stash pop    # Apply the stashed changes and then drop it.
git stash drop   # Remove the stashed changes.
git stash apply  # Apply the stashed changes and leave them in the stash.

Git Log

git log
git log -p -2 (--patch)
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"

Git Reset

git reset --hard HEAD        # Reset all files to the HEAD of the branch:
git checkout HEAD -- FILE    # reset a single file
git reset --soft HEAD~1      # Revert committed changes.

Git Ignore Patterns

The rules for the patterns you can put in the .gitignore file are as follows:

Miscellaneous


Microsoft Terminal

This is another relatively new open source tool from Microsoft. I absolutely love it and can’t work without it. It is not exactly a terminal but provides tabs to open different terminals installed on Windows. It supports Windows console, PowerShell, all installed distributions in WSL, and Azure in a single window. If you work with embedded systems, use multiple build servers, or multiple remote machines this is a must to have tool.

This is how the Microsoft Terminal looks likes:

Customizing Profiles

Microsoft terminal has concept of profiles corresponding to each type of console it can open. By default it will detect and enable all detected consoles: Windows command prompt, PowerShell, Azure Cloud Shell, and all WSL distributions. If you don’t need to use a console, then it is possible to hide them from the Settings JSON by toggling the hidden key to false. It is also possible to change the name terminal shows in the tab list. The full set of settings provided for profiles are available at https://aka.ms/terminal-profile-settings.

Color Schemes

Microsoft terminal offers some color schemes out of the box and it is possible to create our own or customize an existing scheme by editing the JSON file. The documentation for color schemes is located at https://aka.ms/terminal-color-schemes. I am happy with the default color scheme but if you like to change, there are tons of color schemes available from https://windowsterminalthemes.dev.

Keyboard Shortcuts

Working with terminal is pretty useless without using keyboard short cuts and Microsoft terminal allows to bind keyboard shortcuts for almost all tasks. The full documentation is located at https://aka.ms/terminal-keybindings. The keyboard shortcuts are called actions and there are two types: without arguments and with arguments. Some of the important short cuts that I use are:

        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" },
        
        { "command": "commandPalette", "keys": "ctrl+shift+p" },
        { "command": "toggleFullscreen", "keys": "ctrl+enter" },
        { "command": "openNewTabDropdown", "keys": "ctrl+space" },
        { "command": "openSettings", "keys": "ctrl+," },
        
        { "command": "duplicateTab", "keys": "ctrl+shift+d" },
        
        { "command": "closePane", "keys": "ctrl+w" },
        { "command": { "action": "moveFocus", "direction": "down" }, "keys": "alt+down" },
        { "command": { "action": "moveFocus", "direction": "left" }, "keys": "alt+left" },
        { "command": { "action": "moveFocus", "direction": "right" }, "keys": "alt+right" },
        { "command": { "action": "moveFocus", "direction": "up" }, "keys": "alt+up" },
        { "command": { "action": "moveFocus", "direction": "previous" }, "keys": "ctrl+alt+left" }

The most interesting of these shortcuts is the command palette. It can be launched from the default key bindings of Control + Shift + P. It can search and execute any command available in the terminal.


Power toys I cannot work without

I believe every skilled worker needs to have a collection of good quality tools to achieve optimal productivity. For me, a big part of my job is to write code for software development as well as for algorithmic development, testing, debugging, and frequently for learning. As such, I have collected a number of invaluable tools for myself. In this post I am going to share the tools I use for my work.


Microsoft Power Toys

Microsoft powertoys is about about two years old and I have been using it for few months only. It is not a single tool but a collection of different tools and it is very actively maintained and developed on GitHub. If contains the following tools:

The tool I use most among these is Powertoys Run. Infact, I came across Microsoft PowerToys because I was looking for a replacement for Launchy which I have been using for more than a decade but it is not longer actively maintained. I have blogged about the transition here. The idea behind Launchy/Powertoys Run is simple. Press a keyboard shortcut (Alt+Space), and it pop a single floating text control, already focused to accept the input and you can type in a name of the program you want to run or a file or folder you are looking for. Powertoys Run can located any executable indexed by Windows search. So, if you maintain a folder of portable tools like I do, simply make sure that the folder is in Windows Search Index. It is a major productivity booster for me.

Power Toys Run

Other than Powertoys run, I use keyboard manager to remap some keys on my laptop to make them more suitable for programming/text editing. Previously, I used to use SharpKeys but now I exclusively use keyboard manager.

I have not actively used other tools but color picker looks good and in future I might replace Just Color Picker with it.


Center/Right Align a Widget in QTreeWidget

Qt comes with a QTreeWidget class which can be used to show data in a tree view. It can be used to show hierarchical data using either Model/View framework or by manually creating the hierarchy. The QTreeWidget supports multi-columns for each row and also allows editing of the individual cells. However, sometimes we need to present a QWidget in a cell to allow user to interact with data. For example, we might want a user to choose a Boolean value for a cell and instead of asking the user to type Yes/No or True/False, we can present the user with a checkbox. This eliminates human errors in type the values. Often, there is also a need to embed a QPushButton within a cell to allow user to run some action.

Let’s dig in a single example on how to embed a QWidget in a cell of a QTreeWidget.

// Create a tree widget with three columns.
QTreeWidget* treeWidget = new QTreeWidget();
treeWidget->setColumnCount(3);
QStringList columnNames;
columnNames << "Column 1" << "Column 2" << "Column 3";
treeWidget->setHeaderLabels(columnNames);

// Add a top level tree widget item.
QTreeWidgetItem* item = new QTreeWidgetItem();
treeWidget->addTopLevelItem(item);

// Add a check box to the second column.
QCheckBox* checkBox = new QCheckBox("Click Me!");
treeWidget->setItemWidget(item, 1, checkBox1);

// Add tree widget to the parent widgets layout.
this->setLayout(new QVBoxLayout());
this->setContentsMargins(0, 0, 0, 0);
this->layout()->addWidget(treeWidget);

The above code produces the output as shown in the below image. The check box is left aligned and QTreeWidget does not offer a way to center or right align it out of the box. A standard solution offered online is to inherit from QTreeWidgetItem and take control of painting the item directly.

QTreeWidget by default aligns inserted widget to the left.

Inheriting from QTreeWidgetItem is unnecessarily complicated and there is no need to do it. A simpler way is to put the checkbox in another widget and use a horizontal layout with stretch before and after the checkbox! This trick can be used to right align as well by omitting the stretch before the checkbox. Here is the code:

// Create a check box.
QCheckBox* checkBox = new QCheckBox("Click Me!");

// Put the check box in a wrapping widget with appropriate layout.
QWidget* checkBoxWrapper = new QWidget;
QHBoxLayout* layout = new QHBoxLayout();
layout->addStretch();
layout->addWidget(checkBox);
layout->addStretch();
layout->setContentsMargins(0, 0, 0, 0);
checkBoxWrapper->setLayout(layout);

// Add it to the tree widget.
mTreeWidget->setItemWidget(item, 1, checkBoxWrapper);

This code is going to produce the following output:

Center aligned widget by using a wrapping widget with a horizontal layout.

The complete Visual Studio 2019 solution for this demo can be downloaded here.


Display FPS for VTK on Python

In the last post, I discussed how to get started with VTK on Python. In this post, I will show how to add support to show frames per second (FPS). The idea to calculate FPS is straight forward: keep track of the number of frames (N) that were rendered in last T seconds. Then fps defined a N/T fps.

To calculate FPS we will add an observer to the EndEvent command of the vtkRenderer. In the callback function, we will count the number of frames rendered in the last T seconds and calculate FPS. Here is the complete code of the FpsObserver:

import vtk
from timeit import default_timer as timer

class FpsObserver:
	def __init__(self, renderer, x=0, y=0):
		self.mRenderer = renderer
		self.mRenderer.AddObserver(vtk.vtkCommand.EndEvent, self)
		
		self.ActorPosX = x
		self.ActorPosY = y
		
		self.mFrameCount    = 0         # Number of frames collected since last FPS was calculated.
		self.mStartTime     = timer()   # The last time FPS was calculated.
		self.mFpsUpdateRate = 1         # How often to update FPS in seconds.
		
		self._createFpsTextActor()
	
	def setPosition(self, x, y):
		self.ActorPosX = x
		self.ActorPosY = y
		self.mFpsActor.SetPosition(self.ActorPosX, self.ActorPosY)
	
	def __call__(self, caller, event):
		if event == "EndEvent":
			self.mFrameCount = self.mFrameCount + 1
			
			if timer() - self.mStartTime > self.mFpsUpdateRate:
				_currentTime     = timer()
				_duration        = _currentTime - self.mStartTime
				
				_fps = self.mFrameCount/_duration
				print("fps={:.3f}".format(_fps))
				self.mFpsActor.SetInput("FPS: {:.2f}".format(_fps))
				
				self.mStartTime  = _currentTime
				self.mFrameCount = 0
				
	def _createFpsTextActor(self):
		self.mFpsActor = vtk.vtkTextActor()
		self.mFpsActor.GetTextProperty().SetFontFamilyAsString("Georgia")
		self.mFpsActor.GetTextProperty().SetFontSize(20)
		self.mFpsActor.GetTextProperty().SetColor([1, 1, 1])
		self.mFpsActor.SetPosition(self.ActorPosX, self.ActorPosY)
		self.mRenderer.AddActor(self.mFpsActor)

To use FpsObserver, we just need to initialize it as self.mFpsObserver = FpsObserver.FpsObserver(self.mRenderer). That’s it, this will display the FPS for last one seconds!


Getting Started with VTK for Python

The visualization toolkit (VTK) is a open source library displaying scientific data. VTK is maintained by Kitware, the same company which gave us CMake. VTK is written in C/C++ but it comes with Python bindings and can be installed from https://pypi.org/project/vtk/. In this post, I am going to show how to start using VTK from Python using PyQt5.

Qt has two package for using with Python: PySide2 and PyQt5. PySide2 is the official module for Python but for a long time there was no official module and only PyQt5 was available. You can refer to https://www.learnpyqt.com/blog/pyqt5-vs-pyside2/ to understand the differences (they are mostly same) between two modules. I am going to use PyQt5 but the VTK module itself supports both Qt modules.

VTK provides a QVTKRenderWindowInteractor class which inherits from QWidget, QGLWidget, or any other custom class inherited from QWidget. We will add QVTKRenderWindowInteractor to a QMainWindow and use vtkRenderer to render a Hello, World sphere. To decouple user interface (Qt) and rendering (VTK) I will create a VtkWindow class and use it from a MainWindow which is purely for VTK.

Lets first create the MainWindow:

from PyQt5 import QtCore, QtWidgets
import sys
import VtkWindow

class MainWindow(QtWidgets.QMainWindow):
	def __init__(self, parent=None):
		super(MainWindow, self).__init__(parent)
		self.setWindowState(QtCore.Qt.WindowMaximized)
		
		self.mVtkWindow = VtkWindow.VtkWindow()
		self.setCentralWidget(self.mVtkWindow)
		
if __name__ == '__main__':
	app = QtWidgets.QApplication(sys.argv)
	window = MainWindow()
	window.show()
	app.exec_()

If we comment lines 10 and 11 and run the MainWindow.py, it will display a blank Qt Window. Now lets see how to add VTK support to it by adding a VtkWindow class:

from PyQt5 import QtWidgets
import vtk
import vtkmodules.qt
vtkmodules.qt.QVTKRWIBase = "QGLWidget"
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor

# VtkWindow must be derived from QFrame: https://vtk.org/Wiki/VTK/Examples/Python/Widgets/EmbedPyQt
class VtkWindow(QtWidgets.QFrame):
	def __init__(self, parent=None):
		super(QtWidgets.QWidget, self).__init__(parent)
		
		# Create a VTK widget and add it to the QFrame.
		self.setLayout(QtWidgets.QVBoxLayout())
		self.mVtkWidget = QVTKRenderWindowInteractor(self)
		self.layout().addWidget(self.mVtkWidget)
		self.layout().setContentsMargins(0, 0, 0, 0)
		
		# Get the render window and set an interactor.
		self.mRenderWindow = self.mVtkWidget.GetRenderWindow()
		self.mInteractor   = self.mRenderWindow.GetInteractor()
		self.mInteractor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
		self.mInteractor.Initialize()
		
		# Create a new renderer and set the background color.
		self.mRenderer = vtk.vtkRenderer()
		self.setBackgroundColor([0.5, 0.5, 0.5])
		self.mRenderWindow.AddRenderer(self.mRenderer)
		
		# Set the Vtk Window title.
		self.mTitleActor = None
		self.setTitle("pyVtkLib Demo")
		
	# Called when QFrame is resized.
	def resizeEvent(self, newSize):
		textSize = [0, 0]
		self.mTitleActor.GetSize(self.mRenderer, textSize)
		
		width  = int( (self.width() - textSize[0]) / 2.0)
		height = self.height() - textSize[1]
		self.mTitleActor.SetPosition(width, height - 10)
		
	def setBackgroundColor(self, color):
		self.mRenderer.SetBackground(color)
		
	def setTitle(self, title):
		if not self.mTitleActor:
			self.mTitleActor = vtk.vtkTextActor()
			self.mTitleActor.GetTextProperty().SetFontFamilyAsString("Georgia")
			self.mTitleActor.GetTextProperty().SetFontSize(30)
			self.mTitleActor.GetTextProperty().SetColor([1, 0, 0])
			self.mTitleActor.SetInput(title)
			self.mTitleActor.SetPosition(0, 0)
			self.mRenderer.AddActor(self.mTitleActor)
		else:
			self.mTitleActor.SetInput(title)
	

VTK module for Python comes with a QVTKRenderWindowInteractor class which by default inherits from QWidget for PyQt5. In lines 4-5, we first change it to to use QGLWidget so that rendering will be done using OpenGL instead of software renderer. Next, we create a class called VtkWindow which inherits from QWidget so that it can be use from Qt UI. Note, that it is recommended to inherit from QFrame and not QWidget as QVTKRenderWindowInteractor cannot be reparented. More discussion on this topic can be found at EmbedPyQt example on VTK website. Next, we create an instance of QVTKRenderWindowInteractor and add it to VtkWindow class through a QVBoxLayout.

After that it is usual VTK stuff of creating a vtkRenderingWindow, vtkRenderWindowInteractor, and vtkRenderer. I prefer to use vtkInteractorStyleTrackballCamera which I find far more intuitive than the default vtkInteractorStyleJoystickCamera.

I render scene title at the top-middle of the screen and in order to place it here I listen to QFrame::resizeEvent to determine te current width and height of the QFrame.

Run the MainWindow.py from a terminal and it will display a windows with text pyVtkLib Demo printed in the middle-center of the window. In the next tutorial I will show how to measure and show frames per second to the VtkWindow.

The code from this tutorial and any other future enhancements I will do will be available from saurabhg17/pyVtkLib repository at GitHub.


Goodbye Launchy!

I have been using Launchy for last 15 years since it was first released in 2005. I has been an awesome companion since then and saved me tons of time. Simple press Alt + Space and enter the name of the program you want execute and voila! It allowed skins and specifying directories to include in search and filter the folders based on extensions. Launchy had everything I could have asked. However, it doesn’t work with Windows 10 store apps and it was last updated in 2010 so there are no new updated or patches since then.

Recently, Microsoft released Power Toys as opensource tool on Github. One of the included tools is PowerToys Run. It has the same core functions plus more other added goodies such as “Open containing folder”, “Run as administrator”, and “Open path in console” which I fund quite useful. My only complaint with PowerToys Run is that it doesn’t allow customs paths and instead uses Windows Search Index.