Sayfalar

15 Haziran 2015 Pazartesi

Clean Code

I read the “Clean Code : A Handbook of Agile Software Craftsmanship” book. It is realy good book , and I’m very satisfied. For me and who want to write clean code I selected fundamental and important tips from book,  here the list for writing clean code ;

-LeBlanc’s Law : Later equals never.(Yeah ! I know it is not about code code , but all of us know why i put it here...)

Meaningful Names

- The word “variable” should never appear in a variable.
- Single-letter names can only be used as local variables inside short methods.
- Classes and objects should  have noun or noun phrase names.
- Function names should verb or verb phrase names.
- Say what you mean. Mean what you say.

Functions

-The first rule of functions is that they should be small. The second rule of functions is that they shoul be smaller than that.
- Functions should do one thing. They should do it well. They should do it only.
- The ideal number of arguments for a function  is zero. More than three arguments requires very special justification.
-Flag arguments are ugly.
-Functions should either do something or answering something, but not both.
-Prefer exceptions to returning error codes.

Comments

-“ Don’t comment bad code , rewrite it. ” Brian W.K and P.J. Plaugher
-TODOs are jobs that the programmers thinks should be done, but for some reason can’t do at moment.

Formatting

-Every programmer has his own favorite formatting rules , but if he works in a team , then the team rules.

Objects and Data Structres

-Objects hide their  daha behind abstractions and expose functions that operate on that data. Data structre expose their data and have no meaningful functions.

Classes

-They should be small.
-Class or module should have one, and only one reason to change.
-In a clean system, we organize our classes so as to reduce the risk of change.

Unit Tests

-Write unit tests first , before writing production code.
-If you don’t keep your tests clean, you will lose them. And without them, you lose the very thing that keeps your production code flexible.
-Cleant test codes should follow the FIRST rule.(Fast, Independent, Repeatable, Self-Validating, Timely).


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.