Sayfalar

25 Aralık 2014 Perşembe

SURF and KeyPoint matching

In my first experiment , I use the temple matching functions of OpenCV. But they are variant to scale,size and orientation , so I should use an Invariant method.  I try my chance with SURF detector. SURF actually very similar to SIFT detector. Both of them try to find the keypoints that could be used for object recognition or matching problems. In surf algorithm like SIFT method we should apply some steps ; ın SURF firstly we calculate Integrals than Hessian , than we calculate orientation then we create a descriptor.Details can be found here . In OpenCV there are ready functions to apply SURF detector and matching algorithm. My code Here ;

For using SURF detector we must add this headers  additionaly;

#include <opencv2/nonfree/features2d.hpp>
#include <opencv2/legacy/legacy.hpp>




int main( int argc, char** argv )
{
Mat frame;
Mat templ;// marker
Mat descriptors1;
Mat descriptors2;
Mat result;
Mat img_matches;

std::vector< DMatch > good_matches;

VideoCapture cam1("/home/maygun/Desktop/source.mp4");//our video
templ = imread("/home/maygun/Desktop/matrix.png"); // our object that we want to find in an image

cvtColor( templ,templ,cv::COLOR_BGR2GRAY );// transform color to gray image
cv::GaussianBlur(templ,templ,Size(5,5),0.5,0.5,0);

cv::FastFeatureDetector detector(21); // parameter as a min hessian
vector<KeyPoint> keypoints_frame; // keypoints for matching
vector<KeyPoint> keypoints_matrix;

cv::SurfDescriptorExtractor extractor;

std::vector<Point2f> obj;
std::vector<Point2f> scene;

BFMatcher matcher(NORM_L2);
vector< DMatch > matches;

detector.detect(templ, keypoints_matrix); // detect keypoint in object
extractor.compute(templ,keypoints_matrix,descriptors1); // create descriptors



namedWindow( "result", WINDOW_NORMAL );
while(1)
{
char b1 = cvWaitKey(33);
if (b1 == 27) // if esc exit
break;
cam1 >> frame;
cv::GaussianBlur(frame,frame,Size(5,5),0.5,0.5,0);

detector.detect(frame,keypoints_frame); // detector for each frame
extractor.compute(frame,keypoints_frame,descriptors2);

matcher.match(descriptors1,descriptors2,matches); // match images

cv::drawMatches(templ,keypoints_matrix,frame,keypoints_frame,matches,result,Scalar::all(-1),Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);

imshow("result",result);
}
  waitKey(0);
  return 0;
}


Here result ;




References ; http://docs.opencv.org/doc/tutorials/features2d/feature_detection/feature_detection.html

2 Aralık 2014 Salı

Temple Matching with OpenCV

If you want to find an object in an image , you can use temple matching methods. I try to detect marker in video , and I try these methods. But unfortunately , temple matching methods in OpenCV are not sufficient because they are not invariant to illumination condition and scale sizes.

 You can see in my code there is a matching method , there are a lot of options , their approach like this ;



















My Code ;
















Result ;



As you can see in result image a rectangle appear bottom but in the middle there is me and I pasted my marker on to my chest .In video sequence nearly 30 seconds I am moving very slowly but this method cannot find right place. In this week , I will try more accurate method for recognizing object and tracking.If I do any progress, i will share the results.




References; http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

24 Kasım 2014 Pazartesi

SIFT Algorithm

SIFT is very popular method in Computer Visioni created by Dr.Lowe.I prepared a presentation about this.Link are here ;

http://web.itu.edu.tr/~aygunme/sift.pdf

After the midterms maybe I can share an Octave or Matlab code.

2 Kasım 2014 Pazar

Octave and Image Tool

      Octave is alternative program for MATLAB, which is a GNU project and can run on Linux systems.It is free and it's useful for  a lot of operations like MATLAB.I use Octave for Computer Vision.Like Matlab , octave has same tool include some ready function for some compex perations..Installation of octave is very simple with sudo apt-get install octave is fine but for normal usage.If you want to use Image tool you must install 3.8 version or later.

Download version 3.8 later , octave and use this command for installing pure octave.

tar xvf octave-version.tar.gz
cd octave-version
./configure
make
sudo make install

make step is a little bit long so you can use make -j number , number here number of the your core so you can parallel this step ,and time is decrease.

Then we must install package image.But most of the package have pre-package, so you have to install another package , here dependencies image tools

image ->general
           ->signal->control

for install package Download  and give the command on octave pkg install package_name.tar.gz

You must start the control,then signal and general last install image tool.

Then you must load the image package with : pkg load image 

Then you can use a lot of ready image-tools functions.

6 Ekim 2014 Pazartesi

Smart Pointers

Smart Pointers


            Smart pointers come with C++11 standarts.They help us to handling about memory managment topics.In modern languages there are garbage collector that manage the object life times and object's memories.In c++ when we create a object we must be delete the object , otherwise the memory which is used this object will be useless.There are four smart pointer in C++11 , but there are another smart pointers in boot libraries.Since they are not standart I wont cover in this article.

       We must give the flag -std=C++11 in GCC and add the library <memory> when we use the smart pointers.

auto_ptr

     First smart_pointer is auto_ptr.It is not as smart as other smart pointers.When we create object we must be delete when we finished job about this object.


Obje* ptr = new Obje();
ptr->Fonksiyonlar();

delete ptr ;

         But when we forget the deleting there will be extra memory usage.
When define the auto_ptr ;
auto_ptr<T>ptr(new T); We use the this declaration.T is the type of the pointer and ptr is the name.With Ptr-> we can access the object elements and functions.When a auto_ptr assign a object and this object's life time over it deleted automaticlay. It have two useful functions : reset () and release (). reset is kill the object and create a new one , but release is only finished the ownership.

prt.reset(new T); // reset usage
T* p3= ptr.release() ; // ownership finish

delete p3; // delete the pointer 


We cannot use the auto_ptr for STL containers and we shouldn't assign a auto_ptr to another one.Because when assign the first one lose the ownership.
There are a idiom for not losing ownership it is const idiom we can only add the const.

const auto_ptr<Test> pt (new Test(2)); // like a normal auto_ptr

shared_ptr

It is for sharing a object .We can assign two pointers to a object without losing a ownership.We can assign two shared_ptr but never two shared_ptr points to each other because of strong reference counting they cannot be deleted.Declaration of shared_ptr is like auto_ptr
shared_ptr<T> sptr(new T); 
We can use the shared_ptr for the arrays but we must de declare a deletion operation like ;
shared_ptr<int []>p (new int[10],[](int* p) {delete [] p;});


       We a shared object points a object strong reference will one if another one do it will be 2.When the strong reference is zero object will deleted.It is good to sharing a object.But when cyclic reference happen the object cannot be deleted because strong reference never will be 0.For example;
void main()
{
std::shared_ptr<A> x=std::make_shared<A>();


x->ptr = x;// never deleted
}



We can handle this situation with using another smart_pointer weak_ptr

weak_ptr
       weak_ptr using for breaking cyclic reference.But we(programmers) must be investigare where the cyclic reference. weak_ptr cannot be exist without a shared_ptr.weak_ptr take a shared_ptr as a parameter when it is declareted.Like;

weak_ptr<T> wptr(sptr); // sptr : a shared_ptr


We cannot use the -> or . İn weak_ptr.If a weak_ptr assign a shared_ptr strong reference wont increase.So we can the break cyclic reference.
It have two functions : lock and expired ().expired return is expired or not.lock return a shared_ptr whic it assigned.

 We can solve the danglin pointers with using weak_ptr;

unique_ptr

      Last one is unique_ptr. It is like shared_ptr but but there is no sharing thing.a unique_ptr can assign to another one.We can use the unique_ptr for arrays or contaioners without create a delete operation.It have two function reset and release. Reset is reset all thing resource,ownership but release is only finish the ownership.Here a sample code;

26 Eylül 2014 Cuma

Multi-Threading In Android and Handler Class - I

Multi-Threading In Android and Handler Class - I 

Multi-Threading in Android

When I started research this topic first question about threading is why i need threads? There is always tags like fast,usable,multicore .etc. All of them good tags which i interest in all of type programming language :)With multi-thereading our app's will be more fast and more useable,but WHY ? Java,C or C++ are written in very past , and all of them create for single core processor but today we have a lot of processer even in mobile phones.And today we can use multi-cores with same ways in these languages.In Java multi-threads is a way to use this power.In android we can use regular threads as in Java but we must be consider about Android threads(UI Threads) and some classes.

In android there is a class which responsible to handle  topic about threads,and  it's name is Handler class:).With messages all of the threads comminicate with each other in this class.It is simple to create a handler

Handler handlerObject = new Handler(); 

We use the Runnablers for multi-threadings.They are type of thread whic can execute their body or call another functions etc.They include a abstract method void run() all of the threads must be have this method.

class ClassName implements Runnable 
{
Public void run() 
{
// Body of method
}
}

When we use the Handler in our android project we must be add import android.os.Handler; lines in aour projects.
And in our project we create a handler like on the top.

Handler maygun = new Handler(); 

And then we can create a Runnable

Runnable run = new Runnable() 
{
public void run()
{
// Our functions
  }
};


I will continue with Handler Mechanism and AsyncTask in next blog post.

26 Ağustos 2014 Salı

C++ 14 Announced

https://isocpp.org/blog/2014/08/we-have-cpp14

C++ 14 standard has been announced.C++ 14 have a lot of new capabilities and this capabilities is made C++ more powerful and modern language.Here a blog post for new features about C++14.

Clang 3.4.x and newest version of GCC support C++ 14 standard.

Here some of the features which really affect me  ;

N3532  - Dynamic Arrays 

N3534 - C++ Pipelines

N3554 - A Parallel Algorithms Library for C++

N3559 - Proposal for Generic (Polymorphic) Lambda Expressions


Dynamic Arrays will play huge role for memory management in C++ projects.

C++ Pipelines and Parallel Algorithm library is also very important , because all of the computer architecture in today have multiple core system and we must use all of the power for strong and fast software.

Lambda  came with C++11 but now it have more power in C++.


1 Ağustos 2014 Cuma

Haskell Day6

Basic I/O

Here is the most classic program ever :)

main = do
     print(“Hello Word”)

If we want to get input in Haskell we can use getline function for strings,integers or other basic input data. For example :


main= do
       putStrLn "enter age"
       age <- getLine
       putStrLn("Hello " ++ age ++ " boy")

Let do some fun thing with simple ı/o and some condition situations :)

main= do
          putStrLn "enter age"
          age <- getLine
          if read(age) > 50
                  then do print("You are too old man")
          else
                 do print("Yeah men you have a lot of time for Haskell !")


In line 2 we enter the value of the age.line 3 take the argument and equalize the value of the age variable. In line 4 with read function we can compare a string and a integer because Getline function get string and read function change it.then line in line 4 if do the condition things and “then do” parts is returning part.

29 Temmuz 2014 Salı

Haskell Day5

About Functions

Today I learned about function composition thing in Haskell. Firstly I start with simple composition example. In math we can very easily compose two or more functions .İ.e : there are two functions f(x) and g(x) and the composition function can be writed like t(x) = g o f or h(x) = f o g t(x) and h(x) will different functions. In Haskell we can combine function with using a functions as a parameter like:

f x = x + 2
g x = x * 3
a = f (g 4)
b =g(f 4)
main = do
           print(a)
           print(b)

Program will return 14 and 18 .We must use parentheses otherwise we can use “.”
f ( g x ) = = (f . g) x

Lets start with fundamental thing of structural programming : “decision part”.In Haskell we use If,Then and Else structure for making decision. For example there are three possibility about f(x).

f(x) can be positive or negative or zero so

mysig x =
             if x < 0 – deciding
                    then -1 – if firs condition ok what happened
                    else if x >0 – second condition
                          then 1 – if second condition ok what happened
                          else 0 – what happened if all of condition is wrong
main=print(mysig (0))

it will return 0.Also this function can be write like this

mysig x
         | x < 0 = -1
         | x > 0 = 1
         | otherwise = 0


It is so similar to functions in math.  

27 Temmuz 2014 Pazar

Haskell Day4

List and Tuples

Haskell have two important data storage system such as lists and tuples. They are very important thing for Haskell like functions. Lists are implemented by “[ ]” but Tuples with “( )”.The differences between lists and tuples are :
#Tuples size is constant but list are flexible so if we know size of our data we should use tuples for better performance.
#Tuples can be storage different type of data like a Truth value and a string can be in same tuples but list not allow this and return a error which about data types for example a name and a telephone number can be use in a same tuple but they cannot be in a list.

In list and tuples we define elements separating with comas.

list_ex = [1,2,3]
list1_ex = [“ahmet”,”mehmet”,”as”]
tuples_ex = (“john,”,1,True)
main = do
     print(list_ex)
     print(list1_ex)
     print(tuples_ex)


Some mistake coding example

list_ex = [1,”2”,3] – error 1 is a integer but “2” is a char so types are different

we can add elements into a list with using “:”.For example;

list_ex[1,2,3]
main = print(0:list_ex)


program will return 0,1,2,3  

26 Temmuz 2014 Cumartesi

Haskell Day 3

Learning Haskell Day3

Today i discover library of haskell.It is very easy to import a library on our codes. "import" keywords handle everything :)

For example i added System.Info library for using some ready function which implemented before.With this code we can learn system's info about OS or compiler etc.


import System.Info

main=do
print os
print compilerName
print compilerVersion

This code will return for me "linux"  "ghc" and "version versionbranch[7,6]"

Here some basic libraries for Haskell these libraries have some ready functions and data types for using if you interested link look down :)

http://www.haskell.org/ghc/docs/6.12-latest/html/libraries/base-4.2.0.1/

25 Temmuz 2014 Cuma

Haskell Day2


Learning Haskell Day2

         Today I learned about truth values and basic types. Truth values similar to other high-level programming languages. There are two variables such as “True” and “False”. If a condition is not right it will be false otherwise true. For example this program print False

sq x = x * x
main=print(sq 2 ==5 )

Or this one will return true

vol x y z = x*y*z
main=print(vol 3 4 5 == 60).

Of course we can do some boolean operation like “and” and “or”.For and we use && and if one of the condition is false it will be false.For or we use || it is enough to being true if one of them is true.For example:

main=do
      print(3==3 && 4==2)
      print(3==3 || 4==2)
      print((==) 3 5 )

the output like
false true false

first two line is seems normal but third condition is different. The == operator can bu used like a prefix operator even it take two arguments.

Types in Haskell very similar to other programming languages char strings... . If you want to learn a type of a variables on ghci you can use :type ot :t for example :t True will be return boolean.

24 Temmuz 2014 Perşembe

Haskell Day1

Learning Haskell Day1

#In Haskell comment line start with - - . I like it because I use a a lot of times this smile -.- :P an they are very similar.

#Let define a function and see the power of the functional programming :)In Haskell everything actually a function.For example we can define a function like defining a variable.

Fac 0 = 1
fac n = n* fac(n-1) - - defining a funciton
main = print(fac 45)

main program calculate factorial of 45.Of course it is not surprising thing if you familiar with any programming languages but time is very amazing. Even same programming languages cannot calculate the factorial of the 40 ,In Haskell it is very fast also.

Functions can take multiple arguments. But there must be at least one space between arguments. Passing arguments is also same. For example

volume x y z = x * y *z
main = print(volume 3 4 5)

this code take tree arguments and calculate the volume of a rectangular prism.

And finally thing about functions is where clauses. It is like normal where that in English grammar. But usage is a little bit different. I think best way is examples for explanation :).In this code we try to average of the two numbers.

avg a b = s / 2
       where s = a+b
main = print(avg 3 4)

if we don't use the where keyword s will be undefined and compiler give us a error. With where clauses we defined a variables which used before.




2 Haziran 2014 Pazartesi

Stacks

Stack is a data structure which have system called LIFO(Last in First out).
We can imagine like there are over and over dirty plates and if
we want to clean this plates we must start with last dirty plates
and this plate came last to this structure.I implement a integer stack





Stacks have three main functions;

pop :// delete item which is on top
push : // create new item and place to top
top : // which element is on top




23 Mayıs 2014 Cuma

Installation and Compiling Haskell on Ubuntu 13.10

First you should check the universe repository

 sudo gedit /etc/apt/sources.list  and add

deb http://us.archive.ubuntu.com/ubuntu saucy main universe

 line in sources.list if it is not in this file.

Then update : sudo apt-get update

Last install haskell :

sudo apt-get install haskell-platform

Now we try to write first haskell program and build it.


Firstly open a text editor and write

 main = putStrLn "Hello, World!"

Then save as first.hs  In terminal give command like this : ghc -o hello first.hs then ./hello

Welcome the Functional Programming World and Haskell.



26 Şubat 2014 Çarşamba

#Vim tips


Vim has a lot of cool things for developers some of them is very cool and useful there is some of them :)

:syntax on

with this command our codes are will be colorful and looks cool.





























:! gcc code.c -o app

with :! command we can run terminal code so if ve typed gcc commands we can compile the our code in wim.





:vsp

it separate the window vertically 2 pices.with ctlr+w you can switch
screen.if you use like :vsp newcode.c two files are seen on screen like



İf you use sp instead of vsp screen separate horizontally.


#if you have missing brackets {[( -or unmatched  -only you should do is bring cursor on bracket and typing "%" character vim will matched and you will able to fixing situation.