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.

Friday, November 23, 2012

How to read Chinese on Nook 2

Even though I am a Kindle fan, this year nook simple touch is only $49.00 from Target comparing to the regular $99.00. My wife decided to buy me one for fun.

Since nook is not selling in Chinese market officially. The Chinese support is not natively.

1. Download Sigil from: http://code.google.com/p/sigil/
2. Open a .epub file. select "Styles", click the .css file.
3. MOdify the @font-face section from :
@font-face {
font-family:"cnepub";
src:url(res:///opt/sony/ebook/FONT/tt0011m_.ttf), url(res:///tt0011m_.ttf);
}
to: 
@font-face {
font-family:"cnepub";
src:url(res:///opt/sony/ebook/FONT/tt0011m_.ttf), url(res:///tt0011m_.ttf),
url(res:///system/fonts/DroidSansFallback.ttf);
}
4. Copy the modified .epub file into your nook.
5. Open the file in nook. Click the pages like 5/233, select "text" -> check "Publisher Defaults".
Then you will notice the Chinese characters are showing up!!

Reference webpages:
1. http://www.hi-pda.com/forum/viewthread.php?tid=857877&extra=page%3D1
2. http://www.hi-pda.com/forum/viewthread.php?tid=812236
3. http://www.hi-pda.com/forum/viewthread.php?tid=800133
4. http://www.by-smart.com/forum.php?mod=viewthread&tid=2000

Wednesday, November 21, 2012

Install Emacs key-stroke in VS 2010 professional

Emacs key stroke will save operator's time for leaving keyboard fundamental key positions. This is critical for improving programmers' productivity.

Visual Studio for Emacs users might be a pain at the first glance due to the keystroke differences. This article will list the necessary information of how to utilize Emacs keystrokes in VS2010 Pro.

1. Go webpage: http://visualstudiogallery.msdn.microsoft.com/09dc58c4-6f47-413a-9176-742be7463f92/
2. Click "Download" button
3. The download file will be "EmacsEmulations.vsix"
4. Close Visual Studio 2010 Pro
5. Right click and select "open with" -> "Visual Studio 2010"
6. It should be installed automatically.
7. Re start Visual Studio 2010 Pro
8. Click OK for installation permit.
9. Go to Tools->Options-> Environment->Keyboard; Select "Emacs" from "Apply the following additional keyboard mapping scheme:"

Please pay attention to the webpage in step 1. It also includes keystrokes information you may be interested.

PS: This Emacs extension is NOT applied for VS 2010 Express version.

prettify