Mastering Qt 5 by Guillaume Lazar & Robin Penea

Mastering Qt 5 by Guillaume Lazar & Robin Penea

Author:Guillaume Lazar & Robin Penea [Lazar, Guillaume]
Language: eng
Format: azw3, mobi, pdf
Publisher: Packt Publishing
Published: 2016-12-15T05:00:00+00:00


Implementing your OpenCV filters

Now that your development environment is ready, we can begin the fun part! We will implement three filters using OpenCV:

FilterOriginal: This filter does nothing and returns the same picture (lazy!)

FilterGrayscale: This filter converts a picture from color to grayscale

FilterBlur: This filter smoothes the picture

The parent class of all these filters is Filter. Here is this abstract class:

//Filter.h class Filter { public: Filter(); virtual ~Filter(); virtualQImage process(constQImage& image) = 0; }; //Filter.cpp Filter::Filter() {} Filter::~Filter() {}

As you can see, process() is a pure abstract method. All filters will implement a specific behavior with this function. Let's begin with the simple FilterOriginal class. Here is FilterOriginal.h:

class FilterOriginal : public Filter { public: FilterOriginal(); ~FilterOriginal(); QImageprocess(constQImage& image) override; };

This class inherits Filter and we override the process() function. The implementation is also really simple. Fill FilterOriginal.cpp with the following:

FilterOriginal::FilterOriginal() : Filter() { } FilterOriginal::~FilterOriginal() { } QImageFilterOriginal::process(constQImage& image) { return image; }

No modification is performed; we return the same picture. Now that the filter structure is clear, we can create FilterGrayscale. The .h/.cpp files are close to FilterOriginalFilter, so let's jump to the process() function of FilterGrayscale.cpp:

QImageFilterGrayscale::process(constQImage& image) { // QImage => cv::mat cv::Mattmp(image.height(), image.width(), CV_8UC4, (uchar*)image.bits(), image.bytesPerLine()); cv::MatresultMat; cv::cvtColor(tmp, resultMat, CV_BGR2GRAY); // cv::mat =>QImage QImageresultImage((constuchar *) resultMat.data, resultMat.cols, resultMat.rows, resultMat.step, QImage::Format_Grayscale8); returnresultImage.copy(); }

In the Qt framework, we use the QImage class to manipulate pictures. In the OpenCV world, we use the Mat class, so the first step is to create a correct Mat object from the QImage source. OpenCV and Qt both handle many image formats. An image format describes the data bytes organization with information such as the following:

Channel count: A grayscale picture only needs one channel (white intensity), while a color picture requires three channels (red, green, and blue). You will even need four channels to handle the opacity (alpha) pixel information.

Bit depth: The number of bits used to store a pixel color.

Channel order: The most common orders are RGB and BGR. Alpha can be placed before or after the color information.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.