Mastering Qt 5 by Unknown
Author:Unknown
Language: eng
Format: epub
Publisher: Packt Publishing
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(); virtual QImage process(const QImage& 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(); QImage process(const QImage& 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() { } QImage FilterOriginal::process(const QImage& 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:
QImage FilterGrayscale::process(const QImage& image) { // QImage => cv::mat cv::Mat tmp(image.height(), image.width(), CV_8UC4, (uchar*)image.bits(), image.bytesPerLine()); cv::Mat resultMat; cv::cvtColor(tmp, resultMat, CV_BGR2GRAY); // cv::mat => QImage QImage resultImage((const uchar *) resultMat.data, resultMat.cols, resultMat.rows, resultMat.step, QImage::Format_Grayscale8); return resultImage.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
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.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7792)
Autodesk Civil 3D 2024 from Start to Finish by Stephen Walz Tony Sabat(6624)
Mathematics for Game Programming and Computer Graphics by Penny de Byl(6517)
Taking Blender to the Next Level by Ruan Lotter(6290)
Express Your Creativity with Adobe Express by Rosie Sue(6115)
Hands-On Unity 2022 Game Development - Third Edition by Nicolas Alejandro Borromeo(5719)
Hands-On Unity 2022 Game Development by Nicolas Alejandro Borromeo(4799)
Adobe Illustrator for Creative Professionals by Clint Balsar(3686)
Unreal Engine 5 Character Creation, Animation, and Cinematics by Henk Venter & Wilhelm Ogterop(3666)
Going the Distance with Babylon.js by Josh Elster(3645)
Mastering Graphics Programming with Vulkan by Marco Castorina & Gabriel Sassone(3546)
Squeaky Clean Topology in Blender by Michael Steppig(3481)
Drawing Shortcuts: Developing Quick Drawing Skills Using Today's Technology by Leggitt Jim(2926)
Unreal Engine 5 Character Creation, Animation, and Cinematics by Henk Venter
 Wilhelm Ogterop(2877)
Rapid Viz: A New Method for the Rapid Visualization of Ideas by Kurt Hanks & Larry Belliston(2710)
The 46 Rules of Genius: An Innovator's Guide to Creativity (Voices That Matter) by Marty Neumeier(2664)
Learn Qt 5: Build modern, responsive cross-platform desktop applications with Qt, C++, and QML by Nicholas Sherriff(2371)
Fusion 360 for Makers by Lydia Sloan Cline(2225)
Hands-On Neural Networks with Keras by Niloy Purkait(2173)
