<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=OpenCV_a_Ubuntu_16.04</id>
	<title>OpenCV a Ubuntu 16.04 - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=OpenCV_a_Ubuntu_16.04"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=OpenCV_a_Ubuntu_16.04&amp;action=history"/>
	<updated>2026-09-23T03:14:33Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=OpenCV_a_Ubuntu_16.04&amp;diff=256921&amp;oldid=prev</id>
		<title>Joan: /* 1a prova */</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=OpenCV_a_Ubuntu_16.04&amp;diff=256921&amp;oldid=prev"/>
		<updated>2017-12-15T17:02:50Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;1a prova&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Instal.lació i configuració=&lt;br /&gt;
He seguit aquest tutorial i ha funcionat correctament:&lt;br /&gt;
*https://www.learnopencv.com/install-opencv3-on-ubuntu/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
In this post, we will provide step by step instructions for installing OpenCV 3 (C++ and Python) on Ubuntu 16.04.&lt;br /&gt;
&lt;br /&gt;
Step 1: Update packages&lt;br /&gt;
	&lt;br /&gt;
sudo apt-get update&lt;br /&gt;
sudo apt-get upgrade&lt;br /&gt;
&lt;br /&gt;
Step 2: Install OS libraries&lt;br /&gt;
	&lt;br /&gt;
Remove any previous installations of x264&amp;lt;/h3&amp;gt;&lt;br /&gt;
sudo apt-get remove x264 libx264-dev&lt;br /&gt;
 &lt;br /&gt;
We will Install dependencies now&lt;br /&gt;
 &lt;br /&gt;
sudo apt-get install build-essential checkinstall cmake pkg-config yasm&lt;br /&gt;
sudo apt-get install git gfortran&lt;br /&gt;
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev&lt;br /&gt;
 &lt;br /&gt;
# If you are using Ubuntu 14.04&lt;br /&gt;
sudo apt-get install libtiff4-dev&lt;br /&gt;
# If you are using Ubuntu 16.04&lt;br /&gt;
sudo apt-get install libtiff5-dev&lt;br /&gt;
 &lt;br /&gt;
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev&lt;br /&gt;
sudo apt-get install libxine2-dev libv4l-dev&lt;br /&gt;
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev&lt;br /&gt;
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev&lt;br /&gt;
sudo apt-get install libatlas-base-dev&lt;br /&gt;
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev&lt;br /&gt;
sudo apt-get install libvorbis-dev libxvidcore-dev&lt;br /&gt;
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev&lt;br /&gt;
sudo apt-get install x264 v4l-utils&lt;br /&gt;
 &lt;br /&gt;
# Optional dependencies&lt;br /&gt;
sudo apt-get install libprotobuf-dev protobuf-compiler&lt;br /&gt;
sudo apt-get install libgoogle-glog-dev libgflags-dev&lt;br /&gt;
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen&lt;br /&gt;
Step 3: Install Python libraries&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
sudo apt-get install python-dev python-pip python3-dev python3-pip&lt;br /&gt;
sudo -H pip2 install -U pip numpy&lt;br /&gt;
sudo -H pip3 install -U pip numpy&lt;br /&gt;
&lt;br /&gt;
We will use Virtual Environment to install Python libraries. It is generally a good practice in order to separate your project environment and global environment.&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
# Install virtual environment&lt;br /&gt;
sudo pip2 install virtualenv virtualenvwrapper&lt;br /&gt;
sudo pip3 install virtualenv virtualenvwrapper&lt;br /&gt;
echo &amp;quot;# Virtual Environment Wrapper&amp;quot;  &amp;gt;&amp;gt; ~/.bashrc&lt;br /&gt;
echo &amp;quot;source /usr/local/bin/virtualenvwrapper.sh&amp;quot; &amp;gt;&amp;gt; ~/.bashrc&lt;br /&gt;
source ~/.bashrc&lt;br /&gt;
  &lt;br /&gt;
############ For Python 2 ############&lt;br /&gt;
# create virtual environment&lt;br /&gt;
mkvirtualenv facecourse-py2 -p python2&lt;br /&gt;
workon facecourse-py2&lt;br /&gt;
  &lt;br /&gt;
# now install python libraries within this virtual environment&lt;br /&gt;
pip install numpy scipy matplotlib scikit-image scikit-learn ipython&lt;br /&gt;
  &lt;br /&gt;
# quit virtual environment&lt;br /&gt;
deactivate&lt;br /&gt;
######################################&lt;br /&gt;
  &lt;br /&gt;
############ For Python 3 ############&lt;br /&gt;
# create virtual environment&lt;br /&gt;
mkvirtualenv facecourse-py3 -p python3&lt;br /&gt;
workon facecourse-py3&lt;br /&gt;
  &lt;br /&gt;
# now install python libraries within this virtual environment&lt;br /&gt;
pip install numpy scipy matplotlib scikit-image scikit-learn ipython&lt;br /&gt;
  &lt;br /&gt;
# quit virtual environment&lt;br /&gt;
deactivate&lt;br /&gt;
######################################&lt;br /&gt;
Step 4: Download OpenCV and OpenCV_contrib&lt;br /&gt;
&lt;br /&gt;
We will download opencv and opencv_contrib packages from their GitHub repositories.&lt;br /&gt;
Step 4.1: Download opencv from Github&lt;br /&gt;
	&lt;br /&gt;
git clone https://github.com/opencv/opencv.git&lt;br /&gt;
cd opencv &lt;br /&gt;
git checkout 3.3.1 &lt;br /&gt;
cd ..&lt;br /&gt;
Step 4.2: Download opencv_contrib from Github&lt;br /&gt;
&lt;br /&gt;
git clone https://github.com/opencv/opencv_contrib.git&lt;br /&gt;
cd opencv_contrib&lt;br /&gt;
git checkout 3.3.1&lt;br /&gt;
cd ..&lt;br /&gt;
Step 5: Compile and install OpenCV with contrib modules&lt;br /&gt;
Step 5.1: Create a build directory&lt;br /&gt;
	&lt;br /&gt;
cd opencv&lt;br /&gt;
mkdir build&lt;br /&gt;
cd build&lt;br /&gt;
Step 5.2: Run CMake&lt;br /&gt;
&lt;br /&gt;
cmake -D CMAKE_BUILD_TYPE=RELEASE \&lt;br /&gt;
-D CMAKE_INSTALL_PREFIX=/usr/local \&lt;br /&gt;
-D INSTALL_C_EXAMPLES=ON \&lt;br /&gt;
-D INSTALL_PYTHON_EXAMPLES=ON \&lt;br /&gt;
-D WITH_TBB=ON \&lt;br /&gt;
-D WITH_V4L=ON \&lt;br /&gt;
-D WITH_QT=ON \&lt;br /&gt;
-D WITH_OPENGL=ON \&lt;br /&gt;
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \&lt;br /&gt;
-D BUILD_EXAMPLES=ON ..&lt;br /&gt;
Step 5.3: Compile and Install&lt;br /&gt;
	&lt;br /&gt;
# find out number of CPU cores in your machine&lt;br /&gt;
nproc&lt;br /&gt;
# substitute 4 by output of nproc&lt;br /&gt;
make -j4&lt;br /&gt;
sudo make install&lt;br /&gt;
sudo sh -c 'echo &amp;quot;/usr/local/lib&amp;quot; &amp;gt;&amp;gt; /etc/ld.so.conf.d/opencv.conf'&lt;br /&gt;
sudo ldconfig&lt;br /&gt;
&lt;br /&gt;
Step 5.4: Create symlink in virtual environment&lt;br /&gt;
&lt;br /&gt;
Depending upon Python version you have, paths would be different. OpenCV’s Python binary (cv2.so) can be installed either in directory site-packages or dist-packages. Use the following command to find out the correct location on your machine.&lt;br /&gt;
1&lt;br /&gt;
	&lt;br /&gt;
find /usr/local/lib/ -type f -name &amp;quot;cv2*.so&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It should output paths similar to one of these (or two in case OpenCV was compiled for both Python2 and Python3):&lt;br /&gt;
	&lt;br /&gt;
############ For Python 2 ############&lt;br /&gt;
## binary installed in dist-packages&lt;br /&gt;
/usr/local/lib/python2.6/dist-packages/cv2.so&lt;br /&gt;
/usr/local/lib/python2.7/dist-packages/cv2.so&lt;br /&gt;
## binary installed in site-packages&lt;br /&gt;
/usr/local/lib/python2.6/site-packages/cv2.so&lt;br /&gt;
/usr/local/lib/python2.7/site-packages/cv2.so&lt;br /&gt;
  &lt;br /&gt;
############ For Python 3 ############&lt;br /&gt;
## binary installed in dist-packages&lt;br /&gt;
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so&lt;br /&gt;
/usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so&lt;br /&gt;
## binary installed in site-packages&lt;br /&gt;
/usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so&lt;br /&gt;
/usr/local/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so&lt;br /&gt;
&lt;br /&gt;
Double check the exact path on your machine before running the following commands&lt;br /&gt;
&lt;br /&gt;
En el meu cas, em dóna:&lt;br /&gt;
/usr/local/lib/python2.7/dist-packages/cv2.so&lt;br /&gt;
/usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
############ For Python 2 ############&lt;br /&gt;
cd ~/.virtualenvs/facecourse-py2/lib/python2.7/site-packages&lt;br /&gt;
ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so&lt;br /&gt;
  &lt;br /&gt;
############ For Python 3 ############&lt;br /&gt;
cd ~/.virtualenvs/facecourse-py3/lib/python3.6/site-packages&lt;br /&gt;
ln -s /usr/local/lib/python3.6/dist-packages/cv2.cpython-36m-x86_64-linux-gnu.so cv2.so&lt;br /&gt;
&lt;br /&gt;
En el meu cas:&lt;br /&gt;
cd ~/.virtualenvs/facecourse-py3/lib/python3.5/site-packages&lt;br /&gt;
ln -s /usr/local/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so&lt;br /&gt;
&lt;br /&gt;
Step 6: Test OpenCV3&lt;br /&gt;
&lt;br /&gt;
We will test a red eye remover application written in OpenCV to test our C++ and Python installations. Download RedEyeRemover.zip and extract it into a folder.&lt;br /&gt;
Step 6.1: Test C++ code&lt;br /&gt;
&lt;br /&gt;
Move inside extracted folder, compile and run.&lt;br /&gt;
&lt;br /&gt;
# compile&lt;br /&gt;
# There are backticks ( ` ) around pkg-config command not single quotes&lt;br /&gt;
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes&lt;br /&gt;
# run&lt;br /&gt;
./removeRedEyes&lt;br /&gt;
Step 6.2: Test Python code&lt;br /&gt;
Activate Python virtual environment&lt;br /&gt;
	&lt;br /&gt;
############ For Python 2 ############&lt;br /&gt;
workon facecourse-py2&lt;br /&gt;
 &lt;br /&gt;
############ For Python 3 ############&lt;br /&gt;
workon facecourse-py3&lt;br /&gt;
Quick Check&lt;br /&gt;
	&lt;br /&gt;
# open ipython (run this command on terminal)&lt;br /&gt;
ipython&lt;br /&gt;
# import cv2 and print version (run following commands in ipython)&lt;br /&gt;
import cv2&lt;br /&gt;
print cv2.__version__&lt;br /&gt;
# If OpenCV3 is installed correctly,&lt;br /&gt;
# above command should give output 3.3.1&lt;br /&gt;
# Press CTRL+D to exit ipython&lt;br /&gt;
Run RedEyeRemover demo&lt;br /&gt;
	&lt;br /&gt;
python removeRedEyes.py&lt;br /&gt;
&lt;br /&gt;
Now you can exit from Python virtual environment.&lt;br /&gt;
	&lt;br /&gt;
deactivate&lt;br /&gt;
&lt;br /&gt;
Whenever you are going to run Python scripts which use OpenCV you should activate the virtual environment we created, using workon command.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=1a prova=&lt;br /&gt;
Com es comenta en l'article, la primera prova amb C++ és un petit script per treure els ulls vermells d'una foto:&lt;br /&gt;
&lt;br /&gt;
'''removeRedEyes.cpp''':&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes&lt;br /&gt;
$ ./removeRedEyes&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 Copyright 2017 by Satya Mallick ( Big Vision LLC )&lt;br /&gt;
 http://www.learnopencv.com&lt;br /&gt;
**/&lt;br /&gt;
&lt;br /&gt;
#include &amp;lt;opencv2/opencv.hpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using namespace std;&lt;br /&gt;
using namespace cv;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
void fillHoles(Mat &amp;amp;mask)&lt;br /&gt;
{&lt;br /&gt;
    /* &lt;br /&gt;
     This hole filling algorithm is decribed in this post&lt;br /&gt;
     https://www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/&lt;br /&gt;
     */&lt;br /&gt;
     &lt;br /&gt;
    Mat maskFloodfill = mask.clone();&lt;br /&gt;
    floodFill(maskFloodfill, cv::Point(0,0), Scalar(255));&lt;br /&gt;
    Mat mask2;&lt;br /&gt;
    bitwise_not(maskFloodfill, mask2);&lt;br /&gt;
    mask = (mask2 | mask);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char** argv )&lt;br /&gt;
{&lt;br /&gt;
    // Read image&lt;br /&gt;
    Mat img = imread(&amp;quot;red_eyes2.jpg&amp;quot;,CV_LOAD_IMAGE_COLOR);&lt;br /&gt;
&lt;br /&gt;
    // Output image&lt;br /&gt;
    Mat imgOut = img.clone();&lt;br /&gt;
    &lt;br /&gt;
    // Load HAAR cascade&lt;br /&gt;
    CascadeClassifier eyesCascade(&amp;quot;haarcascade_eye.xml&amp;quot;);&lt;br /&gt;
    &lt;br /&gt;
    // Detect eyes&lt;br /&gt;
    std::vector&amp;lt;Rect&amp;gt; eyes;&lt;br /&gt;
    eyesCascade.detectMultiScale( img, eyes, 1.3, 4, 0 |CASCADE_SCALE_IMAGE, Size(100, 100) );&lt;br /&gt;
&lt;br /&gt;
    &lt;br /&gt;
    // For every detected eye&lt;br /&gt;
    for( size_t i = 0; i &amp;lt; eyes.size(); i++ )&lt;br /&gt;
    {&lt;br /&gt;
    &lt;br /&gt;
        // Extract eye from the image.&lt;br /&gt;
        Mat eye = img(eyes[i]);&lt;br /&gt;
        &lt;br /&gt;
        // Split eye image into 3 channels.&lt;br /&gt;
        vector&amp;lt;Mat&amp;gt;bgr(3);&lt;br /&gt;
        split(eye,bgr);&lt;br /&gt;
        &lt;br /&gt;
        // Simple red eye detector&lt;br /&gt;
        Mat mask = (bgr[2] &amp;gt; 150) &amp;amp; (bgr[2] &amp;gt; ( bgr[1] + bgr[0] ));&lt;br /&gt;
&lt;br /&gt;
        // Clean mask -- 1) File holes 2) Dilate (expand) mask&lt;br /&gt;
        fillHoles(mask);&lt;br /&gt;
        dilate(mask, mask, Mat(), Point(-1, -1), 3, 1, 1);&lt;br /&gt;
&lt;br /&gt;
        &lt;br /&gt;
&lt;br /&gt;
        // Calculate the mean channel by averaging&lt;br /&gt;
        // the green and blue channels&lt;br /&gt;
&lt;br /&gt;
        Mat mean = (bgr[0]+bgr[1])/2;&lt;br /&gt;
        mean.copyTo(bgr[2], mask);&lt;br /&gt;
        mean.copyTo(bgr[0], mask);&lt;br /&gt;
        mean.copyTo(bgr[1], mask);&lt;br /&gt;
&lt;br /&gt;
        // Merge channels&lt;br /&gt;
        Mat eyeOut;&lt;br /&gt;
        cv::merge(bgr,eyeOut);&lt;br /&gt;
&lt;br /&gt;
        // Copy the fixed eye to the output image.&lt;br /&gt;
        eyeOut.copyTo(imgOut(eyes[i]));&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // Display Result&lt;br /&gt;
    imshow(&amp;quot;Red Eyes&amp;quot;, img);&lt;br /&gt;
    imshow(&amp;quot;Red Eyes Removed&amp;quot;, imgOut);&lt;br /&gt;
    waitKey(0);&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
{{Autor}}, desembre 2017&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>