I made a Qt application that allows you to procedurally generate maps using opencv4 for image processing.
First I would like to make an introduction on how to integrate opencv in a Qt application. It is not that well explained on the web, so let me make a quick tutorial about this.
So here are the exact steps I executed for opencv to work with my Qt project.
Install opencv 4.4.0
First you need to install opencv on your machine. Here is the link of the best tutorial I found : https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/
The tutorial is for opencv 4.0.0 . As the version 4.4.0 of opencv is released today, you should replace all the command lines containing opencv-4.0.0 by opencv-4.4.0
Once You have completed the build process, don’t forget the sudo make install, as it is the command line that adds opencv to your libraries in your /usr/local folder.
Then, there is a step to verify that you will be able to import the libraries in your project. In the build folder inside your opencv folder, run this command:
$ pkg-config –libs opencv4
You should then see all the libraries in your terminal. If there is an error it could mean that the installation process did not went as expected. Then try to redo all the steps described in the tutorial above.
Link opencv to your Qt project
The next step is to link your opencv libraries to your Qt project. The most convenient way to do this is with PKG_CONFIG.
So add the following lines in your .pro file :
QT_CONFIG -= no-pkg-config unix: CONFIG += link_pkgconfig unix: PKGCONFIG += opencv4
You can do that manually or by right clicking on your project folder>add library>system package>type:”opencv4″
Also add the line : LIBS += -L/usr/local/lib
If you go in your /usr/local/lib directory you can see all the libraries opencv librairies (.so files). If you run into problems with certain functions for example you can still try to add manually the librairies (each .so file).
Note : the first time you run your program you may want to uncheck “Shadow build” in Projects>Build Settings
Start putting opencv code in your Qt project
Here is the link of the little app I made using opencv properties for image processing :
First to use opencv we need to include the opencv modules with this lines :
Then there are two classes you can see. One is the mainWindow embedding all the widgets of the center and the right. The other is the widget on the left that allows to do all the modifications we want on the settings of the map generator.
I won’t enter into the details of procedural map generation as it is not our concern right now, but here is what there is to know about this code. The function generatePerlinNoise(int nOctaveCount, float fScalingBias) takes several parameters as arguments and returns a noise array generated with linear interpolation. That give this array some coherence (it is not just white noise) and with it we can start make a 2d map that has coherence, creating mountains or biomes. It looks like it was generated by nature to our eyes.
Then the function updateMap() convert this generated noise into a 2d image. Here we are using some opencv code. We start by instantiating a cv::Mat object full of zeros that has the same size than the generated noise array. Then we iterate all the pixels in the image to change its value if the noise reach certain steps (here we distinguish 6, but it could be more or less). The type of an image pixel can seem odd to you. It is the cv::Vec4b you can see at several places in the function. It only means that it has 4 channels, three for color and one for alpha value. Here it could also work if it was three channels as we don’t change the fourth channel.
cv::Mat mapImg = cv::Mat::zeros(nOutputWidth, nOutputHeight, CV_8UC4);
for (it = mapImg.begin<cv::Vec4b>(); it!=mapImg.end<cv::Vec4b>(); ++it)
int pixel_bw = (int)((pow(fPerlinNoise2D[i], eN))*6);
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
*generatedMap = Mat2QImage(mapImg);
The function Mat2Qimage takes care of converting a cv::Mat opencv image to a QImage, which is pretty convenient to display it in the application.
I wish you luck in your projects! See you 😉
PS : to go further with map generation look at this link which is an interactive web page to test the effects of several algorithms : https://www.redblobgames.com/maps/terrain-from-noise/