Mastering Qt 5 by Lazar Guillaume & Penea Robin
Author:Lazar, Guillaume & Penea, Robin
Language: eng
Format: epub
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
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(7787)
Grails in Action by Glen Smith Peter Ledbrook(7704)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6611)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6601)
Running Windows Containers on AWS by Marcio Morales(6129)
Kotlin in Action by Dmitry Jemerov(5072)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(4940)
Combating Crime on the Dark Web by Nearchos Nearchou(4522)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4421)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4379)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4192)
The Age of Surveillance Capitalism by Shoshana Zuboff(3961)
Python for Security and Networking - Third Edition by José Manuel Ortega(3763)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3513)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3428)
Mastering Python for Networking and Security by José Manuel Ortega(3348)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3337)
Blockchain Basics by Daniel Drescher(3305)
Learn Wireshark by Lisa Bock(3304)
