Monday, March 19, 2018

SSIM to Measure Image Similarity

There are situations where we need to measure the similarity between two images. For example, when we have an original image and few other images with lower quality, we might need to identify when one is mostly similar to the original image. Bit-wise comparison of the image pixels is not applicable in this kind of scenarios. We need something more sophisticated. Structural similarity index (SSIM) is such a method. It provides a value between 1 and 0 when comparing two images. If two images are exactly similar the SSIM becomes 1. If they are totally different, SSIM becomes 0.

Following Python program implements SSIM to compare between to images. Since we need some extra python libraries for this code to run, we should install following libraries on Ubuntu Linux.

sudo apt install python-skimage

sudo apt install python-opencv

SSIM calculation code is as follows.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from skimage.measure import structural_similarity as ssim
import cv2

print("Reading files...")
first = cv2.imread("image1.png")
second = cv2.imread("image2.png")

print("Resizing files...")
first = cv2.resize(first, (2576,1125))
second = cv2.resize(second, (2576,1125))

print("Converting files to grayscale...")
first = cv2.cvtColor(first, cv2.COLOR_BGR2GRAY)
second = cv2.cvtColor(second, cv2.COLOR_BGR2GRAY)

print("Calculating SSIM value...")
s = ssim(first, second)

print("first vs second", s)

~*******************~


Thursday, March 1, 2018

Merging Video Files on Linux Terminal

When we need to merge multiple video files into a single file, there's an interesting tool called mkvtoolnix we can us. We can install it using the Linux software repository as follows.

sudo apt install mkvtoolnix

Let's say we have three mp4 video files which we need to merge and build a single mkv file. We can do it in the following way.

mkvmerge -o full-video.mkv part-1.mp4 \+ part-2.mp \+ part-3.mp

Meanwhile, I learned that there's a GUI version of the tool called mkvtoolnix-gui which I should explore later. For the moment, we can install it using the following command.

sudo apt install mkvtoolnix-gui

Cheers!

Kazam Screen Recorder

Recently, when I wanted to create a video demonstration which involves recording my computer screen and the input from my microphone. As an Ubuntu GNU/Linux user, I've tried few different screen recorder software but, the results were not very encouraging. However, in my recent attempt, I found a nice piece of software which could meet my requirements. It's called Kazam screencaster program. I just thought to leave a note about it here so that I can find it when I need it the next time.

It is available on the Ubuntu software repositories and we can install it with the following command.

sudo apt update
sudo apt install kazam