I hope you have a folder which you run cmake command to OpenCV? In my case, I created a folder name release to put builds. open Terminal on that location (or go to that place using Terminal). Now run below command.
sudo make uninstall
After this, remove opencv and opencv_contrib folders. If above command doesn't work don't worry. Now we are going to remove symlinks manually(I suggest follow this and check to avoid tons of error while installing new one). Be careful when you are removing them. Always read fully before remove
Run below command and it will prompt you all the files that have OpenCV name.
sudo find / -name "opencv" -exec rm
-i {} \;
read carefully and type y and enter. If you don't want to remove type n and enter.
After above manual remove is complete please run
pkg-config --modversion opencv
If it's doesn't print a version number now OpenCV has completely removed.
Now we need to get OpenCV Release codes. I suggest using OpenCV 3.4.1
Download Source code (zip) using below command
now we need to move them to /opt folder. This process you have to go through a terminal as a Superuser.
After Extract them open Terminal and example commands will be like below.
sudo mv opencv-3.4.1 /opt
sudo mv opencv_contrib-3.4.1 /opt
Now go to opencv-3.4.1 and create release folder
cd /opt/opencv-3.4.1
mkdir release
cd release
Now you have to build the OpenCV using below commands
sudo cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib-3.4.1/modules /opt/opencv-3.4.1/
Make sure you have correct versions on both OpenCV here I use 3.4.1 Please follow next commands.
sudo make -j4
sudo make install
sudo ldconfig
Now run below command to check installation is completed or not.
pkg-config --modversion opencv
Disclaimers - This answer provided with good deeds. If you won't be careful when manual OpenCV file removing step, you might damage your IDE or relevant software.