domenica 28 gennaio 2018

Iterate over a vector - STL C++

just to feed your curiosity

Generate a vector, and try to iterate over it in 3 different ways:

  • using iterator STL;
  • using Range C++11;
  • using indices.


the code above includes also time points, to collect some statistics

// using iterator STL
std::cout << std::endl << "using iterator STL" << std::endl;
std::chrono::steady_clock::time_point t_time1 = std::chrono::steady_clock::now();
for (std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
        //std::cout << *it << ", ";
*it = 1;
}
std::chrono::steady_clock::time_point t_time2 = std::chrono::steady_clock::now();

//Using Range C++11
std::cout << std::endl << "Using Range C++11" << std::endl;
std::chrono::steady_clock::time_point t_time3 = std::chrono::steady_clock::now();
for (auto & value : v) {
//std::cout << value << ", ";
value = 1;
}
std::chrono::steady_clock::time_point t_time4 = std::chrono::steady_clock::now();

//Using indices
std::cout << std::endl << "Using indices" << std::endl;
std::chrono::steady_clock::time_point t_time5 = std::chrono::steady_clock::now();
for (std::vector<int>::size_type ii = 0; ii != v.size(); ii++) {
//std::cout << v[ii] << ", ";
v[ii] = 1;
}
std::chrono::steady_clock::time_point t_time6 = std::chrono::steady_clock::now();


Change freely the size of the vector, then, if you try to understand how much time is spent to perform an assignment on the i-th element... (considering a vector with more or less 1000000 values)


Chrono outcomes
 Vector Random Content integer with size 10000030
Vector iterator STL..: 5.34919  sec.us
Range c++11..........: 2.36629  sec.us
vector indices.......: 0.518377 sec.us

sabato 4 marzo 2017

Install caffe and caffe for matlab on windows 10 for deep learning

ok, not so straightforward...

March, 2017

This brief tutorial helps you to successfully install Caffe on Windows OS and to run the MatLab wrapper on your machine.
Caffe is a very useftul deep learning framework http://caffe.berkeleyvision.org/
I used it to build a LSTM system, cutting the net to a specific layer.

1. First

Refer to this webpage, as noticed by the official webpage of the caffe project:
https://github.com/BVLC/caffe/tree/windows

2. Install pre-requisites

But, if you are a developer, or a dev-geek you have all the software into the checklist, and here you cannot find any suggestion about them:

  • Git!! 
  • Cmake, same as git, you got it previously.
  • Python, 2.X or 3.X or Anaconda, Miniconda etc. 
  • MatLab
  • Cuda and cuDnn

  • MSVC 13 or 15, ok, maybe this is not your IDE for C++, so install it!


3. Follow the instruction on the readme of the caffe windows

Care about the build_win configuration file. There you have to modify the flags with your preference about your personal installation. To install caffe on MatLab enable the correct flag.
if NOT DEFINED BUILD_MATLAB set BUILD_MATLAB=1

4. How to allow caffe working in MatLab

After the installation process (inside the MSVC build the project INSTALL of the caffe solution)
you can try to run the demo on the caffe folders, you could encounter this issue: MatLab does not recognize and accept folder name +caffe.
Copy all the DLLs and other compiled-linekd files from the build caffe folder where you have the mex caffe file.
To solve it you could make a new folder matcaffe, creating a copy of +caffe and renaming it.
When you add the folder to MatLab path using addpath link to the matcaffe folder.
However, internally MatLab use the +caffe folder. 

lunedì 30 gennaio 2017

open command window in windows


if you need to open the DOS shell, prompt, command line, from the file explorer application.
Enter into the folder and ...

Windows Vista/7/8.x/10

In the newer versions of Windows, you do not have to install anything. Simply hold down the Shift key and right-click a folder. The context menu will contain an entry, ‘Open command window here.”

from http://www.techsupportalert.com/content/how-open-windows-command-prompt-any-folder.htm


martedì 13 dicembre 2016

How to read a image sequence with OpenCV


This rough post contains information about the stored image sequence management for still images processing in OpenCV. This post is a reminder, not a tutorial.

Simple assumptions


  • Your image sequence contains images with file name like this: image000001.png. Normally you store images with this sequential file name convention (consider also the case of auto file naming)
  • Your images are in png format (It is only for this example)
  • Your folder absolute path is /folder1/folder2/folder3/ (please consider that Unix and Win paths are different, so use carefully the \ or / chars)


ImageList file

you can use the image list file creator inside the opencv samples, to build a xml file containing the list of the images inside the target folder.
Then you can import the list of images as a vector inside your code

VideoCapture

0. some variables
cv::Mat image_frame;
std::string str_sequenceFileName = "/folder1/folder2/folder3/image%06d.png ";

1. create a videocapture object and then open it, check if is opened and eventually send an error on the shell.

cv::VideoCapture videoCapture;
videoCapture.open(str_sequenceFileName); // open the default camera
if (!videoCapture.isOpened()) {  // check if we succeeded
std::cerr << "[EE] opening video capture OFFLINE device" << std::endl;
return -1;
}
}

2. use the videocapture object instance to grab the current frame. the videocapture grabs the frame from the image sequence folder. You can use it directly inside your main image processing loop.

videoCapture >> image_frame;

Glob

0. some variables
cv::String str_sequenceFolder = "/folder1/folder2/folder3"
cv::String glob_folder = str_sequenceFolder + "/*.png";
std::vector<cv::String> imageFileNamesList;
imageFileNamesList.clear();
cv::Mat image_frame;

1. fill the vector with the image names inside the sequence folder, to obtain the image names list vector

cv::glob(glob_folder, imageFileNamesList);

2. sort the vector (maybe it is not useful)

std::sort(imageFileNamesList.begin(), imageFileNamesList.end());

3. read the current image from the sequence folder

for (int n_frameNumber=0; n_frameNumber < imageFileNamesList.size(); ++n_frameNumber) 
{
    image_frame = cv::imread(imageFileNamesList[n_frameNumber]);
    //some image processing
}

mercoledì 8 giugno 2016

String with padded number in c++

How to use a padded number in a string in c++??

ok, I need to use it to number a image sequence.

Here the include statements
#include <iomanip> // for setfill etc
#include <string>     // for string
#include <sstream>  // for stringstream

Some variables:

std::stringstream sStream;
std::string m_frameName;
int m_frameNumber;

Finally the key piece of code

sStream << std::setfill('0') << std::setw(6) << m_frameNumber;sStream >> m_frameName;

std::cout << "=========" << std::endl << "Frame no. " << m_frameNumber <<  std::endl;
std::cout << "[DBG] " << "m_frameName: " << m_frameName << std::endl;


venerdì 12 febbraio 2016

Ubuntu 14.04 LTS on Dell inspiron 15 7000 series



subtitle: a lot of problems





ok, this is the page of the hardware configuration certification in ubuntu
http://www.ubuntu.com/certification/catalog/component/dmi/4755/dmi%3ADellSystemInspiron157000Series7537/


...but...

WI FI

when you finish the installation stage, u could have no wireless connection
due to problems with Intel wireless drivers

on the Dell pc you have this wireless card
Intel® Wireless 7265
Intel® Wireless 3165 (starting from firmware XX.XX.13.0 and kernel 4.1)

https://wireless.wiki.kernel.org/en/users/Drivers/iwlwifi


a solution is to use this trick:


First, verify that you have these two files; iwlwifi-7265D-13.ucode and iwlwifi-7265-13.ucode:
ls /lib/firmware | grep 7265
If so, we are going to make copies but rename them:
cd /lib/firmware
sudo cp iwlwifi-7265D-13.ucode  iwlwifi-3165-9.ucode
sudo cp iwlwifi-7265-13.ucode  iwlwifi-3165-13.ucode

FROM: http://askubuntu.com/questions/672700/how-can-i-install-intel-dual-band-wireless-ac-3165-drivers

This solution works for me.


Kernel 4.X

you could have problems with this kernel
in my case the nouveau drivers crash on startup 


FREEZING


sometimes the OS freezes,

I don't be sure that this is the solution...

If you have installed the Ubuntu distro on a PC with Microsoft Windows pre-installed maybe the fast startup settings could be put on the RAM some configuration files, then when you start linux it could be find a incoerent state...
 
try to disable the hybernate and sleep, and fast startup in windows OS


I tryed also to install other kernels or other versions of  ubuntu and mint, but I cannot have more than half an hour to spend in installations...

mercoledì 28 ottobre 2015

Uninstall MatLab Linux

come disintstallare matlab su linux?

segui qui!

http://www.mathworks.com/matlabcentral/answers/102428-how-do-i-uninstall-matlab-products-on-a-unix-or-linux-machine


venerdì 8 maggio 2015

Beamer Themes Matrix

La matrice dei temi di Beamer, per avere sotto controllo le varie combinazioni in modo efficace e immediato!

https://www.hartwork.org/beamer-theme-matrix/

The Beamer themes matrix...

lunedì 12 maggio 2014

Installare packages LaTex ubuntu 14.04

Per installare i packages di LaTex su ubuntu 14.04 che provengono da CTAN si può usare tlmgr

Procedura  per installare il pacchetto changes come esempio.

1. Installazione di texlive-base che nella versione di ubuntu 14.04 contiene tlmgr
$ apt-get install texlive-base

 Usage:
    tlmgr [*option*]... *action* [*option*]... [*operand*]...

2. se non inizializzi tlmgr  allora compare questo errore:
$ tlmgr install changes
(running on Debian, switching to user mode!)
cannot setup TLPDB in /home/nomeutente/texmf at /usr/bin/tlmgr line 5336.

per inizializzare tlmgr, con creazione della directory appropriata

$ tlmgr init-usertree
(running on Debian, switching to user mode!)

3. Ricordarsi di installare anche il pacchetto ubuntu xzdec

$ sudo apt-get install xzdec





4. A questo punto si può installare il package di  LaTex


$ tlmgr install changes

tlmgr: package repository http://ctan.mirror.garr.it/mirrors/CTAN/systems/texlive/tlnet
[1/1, ??:??/??:??] install: changes [5k]
tlmgr: package log updated: /home/nomeutente/texmf/web2c/tlmgr.log
running mktexlsr ...
done running mktexlsr.

5. se si vuole installare con il download del pacchetto da CTAN fare riferimento qui:

en.wikibooks.org/wiki/LaTex/Installing_Extra_Packages

martedì 22 aprile 2014

Raspberry configuration

nel file config.txt ci sono le configurazioni base che vengono utilizzate all'avvio dal Raspberry Pi.

sudo nano /boot/config.txt

per abilitare l'utilizzo del composite quando l'hdmi è staccato ci sono due flag che possono tornare utili, controllare se sono abilitati.

hdmi_force_hotplug

Pretends HDMI hotplug signal is asserted so it appears a HDMI display is attached
hdmi_force_hotplug=1 Use HDMI mode even if no HDMI monitor is detected

hdmi_ignore_hotplug

Pretends HDMI hotplug signal is not asserted so it appears a HDMI display is not attached
hdmi_ignore_hotplug=1 Use composite mode even if HDMI monitor is detected 
 
 
 
Riferimento:
http://raspberrypi.stackexchange.com/tags/config.txt/info
http://elinux.org/R-Pi_ConfigurationFile#How_to_edit_from_the_Raspberry_Pi
http://elinux.org/RPiconfig




giovedì 13 febbraio 2014

uso dei limiti per variabili numeriche

da guardare il resto dei limiti dentro limits.h o limits, 
riferimento:
http://stackoverflow.com/questions/15889253/maximum-value-for-unsigned-int

C

#include <limits.h>
unsigned int max_unsigned_int_size = UINT_MAX;

C++

#include <limits>
unsigned int max_unsigned_int_size = std::numeric_limits<unsigned int>::max();
 
 

venerdì 22 novembre 2013

Riviste Computer Vision


Breve lista in continuo aggiornamento di riviste su computer vision, pattern recognition o altro di interessante

Pattern Analysis and Machine Intelligence, IEEE Transactions on
http://ieeexplore.ieee.org/xpl/RecentIssue.jsp?punumber=34
2012 if 4.795

International Journal of Computer Visionhttp://www.springer.com/computer/image+processing/journal/11263
2012 Impact Factor 3.623

Machine Vision and Applications
http://www.springer.com/computer/image+processing/journal/138
2012 Impact Factor 1.103

Pattern Recognition Letters
http://www.journals.elsevier.com/pattern-recognition-letters/
2011 IF 2.501
http://journalinsights.elsevier.com/journals/0167-8655/impact

Pattern Recognitionhttp://www.journals.elsevier.com/pattern-recognition/
2011 if 2.632
http://journalinsights.elsevier.com/journals/0031-3203/impact_factor

Machine Learning
2012 if 1.454
http://www.springer.com/computer/ai/journal/10994

martedì 24 settembre 2013

da variabile a testo in c++

Per evitare che queste semplici informazioni vadano perse

per stampare il valore delle variabili numeriche dentro a una stringa:

http://stackoverflow.com/questions/5290089/how-to-convert-a-number-to-string-and-vice-versa-in-c

altrimenti ottimo articolo di cplusplus.com
http://www.cplusplus.com/articles/D9j2Nwbp/

static_cast<std::ostringstream*>( &(std::ostringstream() << it->id << "D") )->str()

martedì 17 settembre 2013

Usare due schermi

come usare un monitor esterno con xrandr

script per gestire due schermi, di cui uno esterno da far diventare principale:
#!/bin/bash
xrandr --output DVI-D-0 --pos 0x0 --primary --size 1920x1080 --output LVDS-0 --right-of DVI-D-0 --auto


xrandr gestisce gli schermi
LVDS-0 è lo schermo del pc portatile
DVI-D-0 è lo schermo del pc al lavoro

con xrandr si possono esplorare gli schermi collegati e fare uno script per le diverse tipologie di connessioni.


Conversioni per sequenze di immagini

conversion from images sequence to video and viceversa.

FFMPEG

from video to image sequence

ffmpeg -i foo.avi -r 1 -s WxH -f image2 foo-%03d.jpeg
 
viceversa, from images to video

ffmpeg -f image2 -i outimage%03d.png -r 12 -qscale 0 foo.avi
 

-sameq does not mean same quality, you have to use -qscale 0 

pay attention to the difference between linux and win operating systems.

reference:
http://www.ffmpeg.org/ffmpeg.html

venerdì 17 maggio 2013

Opencv Mat e GpuMAt

Come si passa da una all'altra, e come associarle, si non sono i termini corretti, ma non è un blog didattico, serve solo per tenere scritte alcune cose base.


cv::Mat image = cv::imread(imageName, CV_LOAD_IMAGE_COLOR);
cv::gpu::GpuMat gpu_image(image);
cv::imshow("image", image);

altro modo (http://stackoverflow.com/questions/9318388/opencv-gpumat-usage)

Mat src;
src = cv::imread("...");
GpuMat dst;
dst.upload(&src);

secondo approccio (http://stackoverflow.com/questions/6965465/how-to-convert-gpumat-to-cvmat-in-opencv)


explicit conversion: Mat -> GPUMat
Mat myMat;
GpuMat myGpuMat;
myGpuMat.upload(myMat); //Via a member function
//Or
GpuMat myGpuMat(myMat) //Via a constructor
 //Use myGpuMat here...

implicit conversion: GpuMat -> Mat
GpuMat myGpuMat;
 Mat myMat = myGpyMat; //Use myMat here...

giovedì 16 maggio 2013

Installa OpenCV ubuntu 12.04

Brevemente per passi come installare opencv su ubuntu 12.04
brevemente perchè questo post è sicuramente da migliorare

Per esempio usiamo ubuntu 12.04
con partizione dati /media/data/qualcosa
con opencv 2.4.5

questa procedura potrebbe andare bene per ogni distro linux

1. scaricare l'ultima versione di opencv dalla pagina ufficiale www.opencv.org

2. il file per linux è un tar.gz, opencv-2.4.5.tar.gz salvarlo in una cartella del filesystem, tenendo conto che questa cartella non va rimossa, meglio se inserita in una parte del filesystem in cui si ha accesso in lettura/scrittura con utente normale, la cartella estratta potrebbe essere /media/data/qualcosa/opencv-2.4.5

3. all'interno della cartella scompattata creare la cartella build, in cui si compila opencv
cd /media/data/qualcosa/opencv-2.4.5
mkdir build
All'interno della cartella build bisogna configurare il makefile, si fa tramite l'utility cmake, deve essere installata nel sistema la versione > 2.8

4. selezionare con 
ccmake ..
le impostazioni per la generazione del makefile che verrà utilizzato per compilare le librerie

5. una volta selezionate tutte le opzioni desiderate premere c per configurare

6. finito premere g per generare il makefile

7. make 

8. sudo make install

9. le librerie sono installate nel sistema operativo

10. sudo ldconfig per configurare pkg config

11. si può controllare la giusta configurazione di pkg config mediante 
pkg-config --libs --cflags opencv

per installare la versione aggiornata, prima di eseguire tutti i passi,
andare nella cartella build delle vecchie librerie e fare
sudo make uninstall

venerdì 3 maggio 2013

Colori in OpenGL

Breve tabella di colori base in OpenGL


glColor3f(0.0, 0.0, 0.0);      /* black */
glColor3f(1.0, 0.0, 0.0);      /* red */
glColor3f(0.0, 1.0, 0.0);      /* green */
glColor3f(1.0, 1.0, 0.0);      /* yellow */
glColor3f(0.0, 0.0, 1.0);      /* blue */
glColor3f(1.0, 0.0, 1.0);      /* magenta */
glColor3f(0.0, 1.0, 1.0);      /* cyan */
glColor3f(1.0, 1.0, 1.0);      /* white */

Riferimento:
OpenGL Guide (Libro Rosso)


Black           glColor3f(0.0, 0.0, 0.0)
Red             glColor3f(1.0, 0.0, 0.0)
Green           glColor3f(0.0, 1.0, 0.0)
Yellow          glColor3f(1.0, 1.0, 0.0)
Blue            glColor3f(0.0, 0.0, 1.0)
Magenta         glColor3f(1.0, 0.0, 1.0)
Cyan            glColor3f(0.0, 1.0, 1.0)
Dark gray       glColor3f(0.25, 0.25, 0.25)
Light gray      glColor3f(0.75, 0.75, 0.75)
Brown           glColor3f(0.60, 0.40, 0.12)
Pumpkin orange  glColor3f(0.98, 0.625, 0.12)
Pastel pink     glColor3f(0.98, 0.04, 0.7)
Barney purple   glColor3f(0.60, 0.40, 0.70)
White           glColor3f(1.0, 1.0, 1.0)

Riferiemnto:
Libro Blu

lunedì 25 marzo 2013

Installare Olivetti d-copia 3501 MF su Ubuntu

o linux in generale, con CUPS.


  1. localhost:631 nella barra degli indirizzi del browser (ovviamente deve essere installato cups con apt-get install ...) 
  2. se è una stampante di rete, bisogna selezionarla tra le stampanti trovate
  3. usare i driver da creare corrispondenti: Olivetti D-Copia 3501MF –> Sharp AR-M351U
Per la stampante/fotocopiatrice Olivetti D-Copia 3501MF infatti sono compatibili questi driver: Sharp AR-M351U. Nel caso in cui si cercano quelli olivetti non si trovano.

giovedì 13 dicembre 2012

Windows application with console

se stai usando una windows application come entry point per l'applicazione e vuoi anche una console

#include <iostream>
#include <string>
#include <cstdio>
#include <windows.h>
#include <io.h>
#include <fcntl.h>

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if (AllocConsole()) {
int ifd = _open_osfhandle((intptr_t)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
int ofd = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);

*stdin = *_fdopen(ifd, "r");
*stdout = *_fdopen(ofd, "w");

std::cout<<"I made a console window";
std::cin.get();

fclose(stdout);
fclose(stdin);
}
}

Reference:
http://www.daniweb.com/software-development/cpp/threads/347901/is-it-possible-to-use-a-console-app-and-windows-one-at-the-same-time