Thursday, September 12, 2013

How to redefine / re-map keyboard in Windows

  In real life you may have some very inconvenient keyboard, or some applications may prefer
very frequently used keys for a very weak finger (such as Cntl in Emacs v.s Cntl is usually assign to you little finger).

  In Mac OSX you could easily reassign the keys in system administrator. But for Windows the original system does not provide such interface. There were some tool on the website you could easily find and use to modify the mapping. But if you want to map whatever you want and control everything by yourself. You may want to have a look at this blog.

  The following two links has provide useful introduction on how to modify the mapping through  Windows register. But they may not be very fully correctly. That's why I post this blog.
 
  Links:
  1. Download scan code reference from:
     http://msdn.microsoft.com/en-us/library/windows/hardware/gg463372.aspx
  2. Follow the instruction to change the register keys:
     http://www.howtogeek.com/howto/windows-vista/disable-caps-lock-key-in-windows-vista/

  Detailed Process:
 
  1. Find the related scan code file from the previous link 1 around page 33
     For example:
     Right Alt(scan 1 make) => E0 38
     Left Cntl(scan 1 make) => 1D    
  2. According to the instruction on previous link 2:
     Make from Right Alt => left Cntl should be (Scancode Map):
    
     0000 0000 0000 0000 0200 0000 1D00 38E0
     0000 0000
    
     There are two points need to notice:
     1. E0 38 should be reverse order as: 38 E0
     2. 1D    is actually short for 00 1D. Then revert the order in 8bit into 1D 00

  3. Generalizations
     The following three mappings:
     1. Right Alt => Left Cntl
     2. Right Win => Left Cntl
     3. Insert    => Nothing (0000)
    
     0000 0000 0000 0000 0400 0000 1D00 38E0
     1D00 5CE0 0000 52E0 0000 0000

Thursday, May 9, 2013

Progress bar for Console program

During processing a bunk of computation, people always eager to know how far from finish. The progress bar is exactly design for it.

Console program does not support those fancy GUI, therefore something like below might be preferred.

[======>                    ] 37%

The following code should be able to help you accomplish it.
Generally speaking, you could call this function as below:
PS:
You could also get access to this code at: https://gist.github.com/lbl1985/5552400
It is also part of my toolbox_c at: https://github.com/lbl1985/toolbox_c

Wednesday, May 8, 2013

Could not open Video in Windows with OpenCV precompiled dlls

Issue as described in the title. 
The reason is simple, the system need opencv_ffmpeg2**.dll to read video. Just add the bin folder into system path, or directly copy this .dll file into the folder where the executable file generated.

Thursday, April 18, 2013

armadillo to other data format conversion

Armadillo is a C++ linear algebra library, especially for computer vision application.

Armadillo is more similar to MATLAB with decent performance. (check http://nghiaho.com/?p=1726 for comparison).

We may want to cooperate Armadillo with OpenCV, STL or FFTW. In this blog the conversion between STL and OpenCV will be described.

1. armadillo to STL vector
2. Armadillo to OpenCV, Since OpenCV data is saved in row-wised, and armadillo data is saved in column-wised, therefore, when we convert the data, we may need to do transformation at first. The following is the sample code.
3. OpenCV to Armadillo

4. STL 2D vector (vector<vector<T> >) to Opencv cv::Mat 2D matrix
Hope these could be helpful for some one is seeking the answer~~



Tuesday, April 2, 2013

Read ViPer data into MATLAB

ViPer as a ground truth labeling software and protocol has been widely used in computer vision community, such as UCF Youtube dataset. However, since the ground truth information has been stored in XML format, for those who are not familiar with XML and using MATLAB for experiments, this protocol may raise some difficulty.

This blog will give an example of how to read the XML ground truth information into MATLAB.

The example xml file will be the 'v_biking_13_05.xgtf' from UCF Youtube dat set, with bounding box annotation (http://www.cs.ucf.edu/~liujg/YouTube_Action_dataset.html).

1.  download viper-gt.jar from http://viper-toolkit.sourceforge.net, click ViPER Light Distribution (Recommended) [May 25, 2005] under Software section.

In MATLAB type in the following command
>> javaaddpath(' ViPER_Folder/viper-gt.jar')

2. Type in the following commands to obtain an viper object, replace the filename as your v_biking_13_05.xgtf with the path.


>> docBuilder = DocumentBuilderFactory.newInstance();
>> docBuilder.setNamespaceAware(true);
>> docElement = docBuilder.newDocumentBuilder().parse(filename).getDocumentElement();
>> parser = viper.api.impl.ViperParser;
>> objviper = parser.parseDoc(docElement);


3. Obtain a string of bounding box information: (The getChild() may vary depend on different xgtf files, but at least for this example file it works)

str = char(objviper.getChild(1).getChild(0).getChild(1).getAttribute('Location').toString);


4. parse this str as a n x 4 matrix containing BoundingBox Information
The following code should be able to help you to do this job.


function BB = parseStr(str)

dq = strfind(str, '"'); % double quote locations
bn = strfind(str, ')'); % brace number
if ( length(dq) / 2 ~= length(bn) )
    error('dq should be equal to 2 * bn');
end
BB = [];
for i = 1 : length(bn)
    tmpBB = parseOneSeg(str( dq( (i-1) * 2 + 1) + 1 : bn(i) - 1) );
    BB = cat(1, BB, tmpBB);
end

end

function tmpBB = parseOneSeg(str)
dq = strfind(str, '"');
BB = parseBB(str(1 : dq - 1) );

strNum = str2num( str( strfind(str, '[') + 1 : strfind(str, ',') - 1) );
endNum = str2num( str( strfind(str, ', ') + 2 : length(str) ) );
tmpBB = repmat(BB, endNum - strNum, 1);
end

function BB = parseBB(str)
BB = zeros(1, 4);
sp = strfind(str, ' '); % space location
sp = [0 sp length(str)+1];
for i = 1 : 4
    BB(i) = str2num( str( sp(i)+1 : sp(i+1) - 1) );
end
end


5. Last you may want to visualize the bounding box
The following small script may help you to do that.


function readViper_visualize(BB, videoName)

video = VideoReader(videoName);

nFrame = video.NumberOfFrames;
nRow = size(BB, 1);
n = min(nFrame, nRow);

% BB = BB(:, [3 4 1 2]);

for i = 1 : n
    I = read(video, i);
    imshow(I);
    rectangle('Position', BB(i, :), 'EdgeColor', 'r', 'LineWidth', 3);
    title(['Frame ' num2str(i)]);
end



Tuesday, February 26, 2013

OpenCV combine two images

In computer vision community, developer always face one basic task to visualize the correspondences between two frames, and the easier solution might be combine two images into one single image.

In this blog, I will give a convenient and easy to understand solution to solve this problem. Of course, so far, this is just a simple starting point, you could extent it to make more robust.

The code would be as follow:


struct myexception : public std::exception{
    std::string s;
    myexception(std::string ss):s(ss){}
    virtual const char* what() const throw() {return s.c_str();}
    ~myexception() throw(){}
};



// combine two identical size images into one big image
void combineTwoImages(cv::Mat &dst, const cv::Mat img1, const cv::Mat img2){
    if (img1.cols != img2.cols || img1.rows != img2.rows) {
        throw myexception("img1 and img2 should be identical size");
    }
    int rows = img1.rows;
    int cols = img1.cols;
    dst = cvCreateMat(rows, 2 * cols, img1.type());
    cv::Mat tmp = dst(cv::Rect(0, 0, cols, rows));
    img1.copyTo(tmp);
    tmp = dst(cv::Rect(cols, 0, cols, rows));
    img2.copyTo(tmp);
}


As you could notice, this code would only work for identical size case, you would extent it to handle two arbitrary size images as well.

PS: cv::Rect(x, y, _width, _height)

Hope this would be helpful.

Install PCL and first project in XCode

PCL (Point Cloud Library) is getting more popular these days. It's good for Kinect users and might be also helpful for visualization usages as well.

I used to attempt to install the PCL, couple months ago, however at that time it's not very successful. This time, it works. For my record and for others who may interested, I will list the details below.

1. Following the instruction in: http://pointclouds.org/documentation/tutorials/compiling_pcl_macosx.php#compiling-pcl-macosx
till installing Qhull.
2. Install and download libusb, openNI and sense from the following webpage ( do not install libusb from macports):
http://pointclouds.org/downloads/macosx.html
3. Build PCL as the instruction in Step 1 webpage.

The first project in Xcode:

0.1 Switch Compiler for C/C++/Objective-C from Apple LLVM compiler 4.2 -> LLVM GCC 4.2

1. adding the following entries into "Header Search Paths"
1.1 /usr/local/include/pcl-1.7
1.2 /opt/local/include
1.3 /opt/local/include/eigen3
1.4 /opt/local/include/vtk-5.10



2. Adding "New Group", named: boost, right-click new created "boost" folder, and Add File to project:
locate file: /opt/local/lib/libboost_system-mt.dylib

3. Adding "New Group", named: pcl, right-click new created "pcl" folder, and Add File to project:
locate file: ($whereBuildPCL)/build/lib/libpcl_io.1.7.0.dylib, and ($whereBuildPCL)/build/lib/libpcl_common.1.7.0.dylib

4. input the following script into main.cpp


#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

int main(int argc, const char * argv[])
{
    pcl::PointCloud<pcl::PointXYZ> cloud;
    // fill in the cloud data
    cloud.width = 5;
    cloud.height = 1;
    cloud.is_dense = false;
    cloud.points.resize(cloud.width * cloud.height);
    
    for (size_t i = 0; i < cloud.points.size(); ++i){
        cloud.points[i].x = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud.points[i].y = 1024 * rand() / (RAND_MAX + 1.0f);
        cloud.points[i].z = 1024 * rand() / (RAND_MAX + 1.0f);
    }
    
    pcl::io::savePCDFileASCII("test_pcd.pcd", cloud);
    std::cerr << "Saved " << cloud.points.size() << " data points to test_pcd.pcd." << std::endl;
    
    for (size_t i = 0; i < cloud.points.size(); ++i)
        std::cerr << "   " << cloud.points[i].x << " " << cloud.points[i].y << " " << cloud.points[i].z << std::endl;
    
    return 0;
}

5. Find out the output .pcd file 
5.1 Open Organizer: (Window->Organizer)
5.2 find your Projects, right click the right arrow next to your "Derived Data"
5.3 Build/Products/Debug/test_pcd.pcd


Saturday, February 23, 2013

OpenCV: compare whether two Mat is identical

As MATLAB users could always conveniently compare two matrix, in order to decide whether the two matrix is identical or not.
However, in OpenCV this easy task is not straight forward as you first thought.
The underlying small openCV script could help you to address this simple task.

// return true if two matrix is identical, false otherwise


bool BasicUtil::matIsEqual(const cv::Mat mat1, const cv::Mat mat2){
    // treat two empty mat as identical as well
    if (mat1.empty() && mat2.empty()) {
        return true;
    }
    // if dimensionality of two mat is not identical, these two mat is not identical
    if (mat1.cols != mat2.cols || mat1.rows != mat2.rows || mat1.dims != mat2.dims) {
        return false;
    }
    cv::Mat diff;
    cv::compare(mat1, mat2, diff, cv::CMP_NE);
    int nz = cv::countNonZero(diff);
    return nz==0;
}


PS: I'm currently building a C++ toolbox in order to replace some basic MATLAB functionality for computer vision users. I will release it once I built a considerable functionalities.

prettify