Powerful and Beautiful Erlang

erlang
Erlang是什么

Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability. Some of its uses are in telecoms, banking, e-commerce, computer telephony and instant messaging. Erlang's runtime system has built-in support for concurrency, distribution and fault tolerance.

这是官方的定义,说白了Erlang无非就是一个编程语言,它内置高并发、容错、扩展、分布式等特性。

都有谁在使用Erlang

  • SimpleDB, a distributed database that is part of Amazon Web Services
  • ejabberd, an Extensible Messaging and Presence Protocol (XMPP) instant messaging server
  • Facebook Chat system was running on ejabberd based servers, before switching to Java.
  • RabbitMQ, an implementation of Advanced Message Queuing Protocol (AMQP)
  • GitHub, a web-based hosting service for software development projects that use the Git version control system. Erlang is used for RPC proxies to ruby processes.
  • WhatsApp, mobile messenger
  • T-Mobile uses Erlang in its SMS and authentication systems.
  • Yahoo! uses it in its social bookmarking service, Delicious, which has more than 5 million users and 150 million bookmarked URLs.

以上数据摘抄自Wikipedia,其实国内也有很多公司使用,例如淘宝、腾讯、百度等。

Erlang到底能做什么
如果你问我这个问题,其实我想说的是Any。任何一个语言都能做任何事,只是它擅长不擅长而已。Erlang所擅长的就是高并发、高稳定性、分布式编程,当然其弱点在于计算能力,不过这个可以通过NIF来弥补。

Erlang美在哪
首先,Erlang是一个函数式语言,语法代码都很简洁明了。

-module(hello).
-export([hello_world/0]).

hello_world() -> io:fwrite("hello, world\n").

其次,Erlang天生就适合分布式开发,很多适合你都忘记你是在做分布式开发。

Node@whatsapp.com ! "Hello Whatsapp" % 没错,就这么简单地向远程机器打了个招呼

read more

Image Feature Extraction and Matching with OpenCV

目前图像识别仍然是一个重要的研究课题,主要用到的图像匹配方法有颜色直方图、模版匹配、特征匹配等。特征匹配是目前主流的也是准确率较高的匹配方式,例如Google、百度等主流搜索引擎的图像搜索。
下面是使用OpenCV实现的一个简单的图像特征匹配程序

main.cpp

#include <cstdio>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "feature.h"

using namespace cv;
using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        cout << "wrong usage!" << endl;
        cout << "usage: ./bin queryImage trainImage" << endl;
        return -1;
    }

    string detectorType = "SURF";
    string extractorType = "SIFT";
    string detectors[] = {"SIFT", "SURF", "SURF", "MSER", "STAR", "HARRIS", "FAST"};
    string extractors[] = {"SIFT", "SURF", "SIFT", "SIFT", "SIFT", "SIFT", "SIFT"};
    string matchType = "FlannBased";
    string queryImagePath = argv[1];
    string trainImagePath = argv[2];

    Mat queryImage = imread(queryImagePath, CV_LOAD_IMAGE_GRAYSCALE);
    Mat trainImage = imread(trainImagePath, CV_LOAD_IMAGE_GRAYSCALE);
    if (queryImage.empty() || trainImage.empty())
    {
        cout<<"read failed"<< endl;
        return -1;
    }

    for(int i = 0; i < 7; ++i)
    {
        string detectorType = detectors[i];
        string extractorType = extractors[i];

        Feature feature(detectorType, extractorType, matchType);

        vector<KeyPoint> queryKeypoints, trainKeypoints;
        feature.detectKeypoints(queryImage, queryKeypoints);
        feature.detectKeypoints(trainImage, trainKeypoints);

        Mat queryDescriptor, trainDescriptor;

        feature.extractDescriptors(queryImage, queryKeypoints, queryDescriptor);
        feature.extractDescriptors(trainImage, trainKeypoints, trainDescriptor);

        vector<DMatch> matches;
        feature.bestMatch(queryDescriptor, trainDescriptor, matches);

        double max_dist = 0;
        double min_dist = 100;

        for(int i = 0; i < queryDescriptor.rows; ++i)
        {
            double dist = matches[i].distance;
            if(dist < min_dist) min_dist = dist;
            if(dist > max_dist) max_dist = dist;
        }
        //printf("-- Min dist: %f \n", min_dist);
        //printf("-- Max dist: %f \n", max_dist);

        vector< DMatch > good_matches;
        for(int i = 0; i < queryDescriptor.rows; ++i)
        {
            if( matches[i].distance <= max(2*min_dist, 0.02) )
            {
                good_matches.push_back(matches[i]);
            }
        }
        feature.saveMatches(queryImage, queryKeypoints, trainImage, trainKeypoints, good_matches, "./");
    }
	  printf("Done\n");
    return 0;
}

read more

My PHP Android Develop Problem List

The following is some problems what I encountered in the PHP and Android development and solutions.

1. How to upload file to PHP server from Android.

Java code:

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
try{
	HttpPost httppost = new HttpPost("url");
	FileBody imageBody = new FileBody(new File("file path"));
	StringBody typeBody = new StringBody("image", ContentType.TEXT_PLAIN);
	HttpEntity reqEntity = MultipartEntityBuilder.create()
		.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
		.addPart("file", imageBody)
		.addPart("filetype", typeBody).build();
	httppost.setEntity(reqEntity);
	Log.i("DEBUG", "executing request " + httppost.getRequestLine());
	HttpResponse httpResponse = httpclient.execute(httppost);
	HttpEntity resEntity = httpResponse.getEntity();
	if (resEntity != null) {
		String strResult = EntityUtils.toString(resEntity, "UTF-8");
                Log.i("DEBUG", strResult);
	}
			
}catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

read more