Showing posts with label Programing. Show all posts
Showing posts with label Programing. Show all posts

Tuesday, December 23, 2008

Serious problem

[vnl_quaterion]

I made a mistake when coding with quaterion related sections!!

the paper format [R X Y Z]

vnl coding format [X Y Z R]

damn!!!

That means I have to compare the version between vnl_quaterion and VW::QUATERION

[vnl_matrix_fixed] "fill" member related

The source of the problem is that VXL uses explicit instantiation of Templates as opposed to ITK that uses implicit instatiation. This means that in vnl you can only use types that have already been explicity instantiated in the library. If you need to use a new combination, then

you have to add the corresponding explicit instantiation. The vnl_fixed_matrix is only instantiated for a

certain number of template parameters. You will find them under: Insight/Utilities/vxl/core/vnl/Templates They are

vnl_matrix_fixed+double.1.1-.cxx
vnl_matrix_fixed+double.1.2-.cxx
vnl_matrix_fixed+double.1.3-.cxx
vnl_matrix_fixed+double.2.1-.cxx
vnl_matrix_fixed+double.2.2-.cxx
vnl_matrix_fixed+double.2.3-.cxx
vnl_matrix_fixed+double.2.4-.cxx
vnl_matrix_fixed+double.2.6-.cxx
vnl_matrix_fixed+double.3.1-.cxx
vnl_matrix_fixed+double.3.2-.cxx
vnl_matrix_fixed+double.3.3-.cxx
vnl_matrix_fixed+double.3.4-.cxx
vnl_matrix_fixed+double.3.5-.cxx
vnl_matrix_fixed+double.4.1-.cxx
vnl_matrix_fixed+double.4.2-.cxx
vnl_matrix_fixed+double.4.3-.cxx
vnl_matrix_fixed+double.4.4-.cxx
vnl_matrix_fixed+double.6.6-.cxx
vnl_matrix_fixed+float.1.2-.cxx
vnl_matrix_fixed+float.1.3-.cxx
vnl_matrix_fixed+float.2.1-.cxx
vnl_matrix_fixed+float.2.2-.cxx
vnl_matrix_fixed+float.3.1-.cxx
vnl_matrix_fixed+float.3.3-.cxx
vnl_matrix_fixed+float.3.4-.cxx
vnl_matrix_fixed+float.3.5-.cxx
vnl_matrix_fixed+float.4.3-.cxx
vnl_matrix_fixed+float.4.4-.cxx
vnl_matrix_fixed+int.2.2-.cxx
vnl_matrix_fixed+int.3.4-.cxx
vnl_matrix_fixed+vnl_bignum.3.3-.cxx
vnl_matrix_fixed+vnl_rational.3.3-.cxx
vnl_matrix_fixed_pairwise_ops.cxx
vnl_matrix_fixed_ref+double.3.3-.cxx
vnl_matrix_fixed_ref+double.3.4-.cxx
vnl_matrix_fixed_ref+float.3.3-.cxx
vnl_matrix_fixed_ref+float.3.4-.cxx

As you can see, there is no instantiation for a matrix of size over 6 x 6 which is what you seems to be trying to instantiate.

Read More...

Monday, December 8, 2008

Few problems u might possibly encounter when deriving class from base class

1. undefined reference to `vtable for XXXXXXX'

vtable means virtual table, all errors about undefined reference to vtable are caused due to virtual function in class!
This error might raised due to you declare the virtual functions which are inherited from base class in derived class without implementation.

e.g.

class Base
{
public:


Base();
virtual ~Base();

virtual void foo() = 0;
};

class Dclass : Base
{
public:

Dclass();

~Dclass();


void foo();

};

Dclass::Dclass() : Base()
{}

Dclass::~Dclass()
{}

void Dclass::foo() // will cause error if not be implemented!!!
{}

e.g


if you declare some virtual functions in xxx.h file, you must implement them(those virtual functions) after ctor implementation. or it will cause undefined reference to `vtable for XXXXXX'

Read More...

Wednesday, August 6, 2008

Integral table computation

The raw data:

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

 

The integral data:

E

F

A

B

G

C

D

H

I

 
     The integral rule is that each element in the integral table is sum of upper-left elements( including itself ). e.g,
  • A = a + b + f + g
  • B = a + b + c + f + g + h
  • C = a + b + f + g + k + l
  • D = a + b + c + f + g + h + k + l + m

We can, therefore, simply archive the integral table by 4 "for loop"

for(int j = 0; j < height; j++) 
{
for(int i = 0; i < width; i++)
{
integral[j][i] = 0;
for(int y = 0; y <= j; y++)
{
for(int x = 0; x <= i; x++)
{
integral[j][i] += rawdata[y][x];
}
}
}
}


but it is too inefficiency and consuming too many operations when computing in such simple way . Thus, we can examine the integral table, it can be found that D could be get from the 3 nearest upper-left element A, B, C. So D = m + B + C - A. In addition we must consider the boundary problem, when processing the first row it's just the sum of the current raw data and the left integral data, e.g, F = d + E. The same as above, when processing the first colume it's just the sum of the current raw data and the upper integral data, e.g, H = p + G. Therefore, the computation can be archieved by using only 2 "for loop"



for(int j = 0; j < height; j++) 
{
for(int i = 0; i < width; i++)
{
if((j == 0) && (i == 0))// process the first col and row element
integral[j][i] = rawdata[j][i];
else if((j == 0) && (i != 0))// process the first row element
integral[j][i] = rawdata[j][i] + integral[j][i-1];
// F = d + E
else if((j != 0) && (i == 0))// process the first col elements
integral[j][i] = rawdata[j][i] + integral[j-1][i];
// H = p + G
else // process the normal element
integral[j][i] = rawdata[j][i] + integral[j-1][i] +
integral[j][i-1] - integral[j-1][i-1];
// D = m + B + C - A
}
}


[ Download ]: test file (C::B project)

Read More...

Sunday, May 18, 2008

Building Boost C++ lib for MINGW and coding in Code::Blocks

boost + splash_new blank

Boost C++ Libraries is one of the most highly regarded and expertly designed C++ library projects in the world. It provides many efficient algorithms to help App developers to improve their program. However, the Boost's official website offers only source files and Build Tools ( Boost JAM ), we must build the suitable Lib according to the compiler ( like GNU GCC Compiler, Borland C++ Compiler or VC++ Compiler ). Althought, there're many pre-builded destro providered by some unofficial website, most of them are for VC++ Compiler.

Here's a tutorial for building Boost C++ Lib for MINGW GCC Compiler.

  • First of all, Download Boost from the Boost website. You will need the actual Boost package (source) and the Boost Jam package (binary NTx86). Unpack the first package to any location on you harddisk and unpack the file "bjam.exe'' from the second package to a directory contained in your search path (e.g. "C:\MinGW\bin'' or "C:\Program Files\CodeBlocks\MinGW\bin").
  • Then open a shell (cmd.exe) and change into the directory you have unpacked the Boost package to. Type the following command:
    C:\....\boost_1_35_0\>bjam --toolset=gcc --prefix=C:\Boost install
  • While building, it'll take 30 mins up to couple hours depends on your machine. So be patiant.
  • Then Boost C++ Lib will be installed in "C:\Boost\include\boost-version"

After that. we can use Boost to code for MINGW GCC Compiler in Code::Blocks IDE. Before coding, add the Boost "include" and "lib" into the compiler search dictionary.

  • Open the Code::Blocks IDE, then click on "settings""compiler and debugger...", change to "search dictionary" page and add "C:\Boost\include\boost-1_35" into "compiler" page.sshot-1
  • Then add "C:\Boost\lib" into  "Linker" page.

Creat a new Console Application project for C++,  click "Project" → "Build options...". Add the lib you need in your project (e.g. C:\Boost\lib\boost_signals-mgw34-mt-1_35.lib)

sshot-1

Then code following code in main.cpp

// Boost.Signals library

// Copyright Douglas Gregor 2001-2003. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

#include
#include
#include
#include

struct HelloWorld
{
void operator()() const
{
std::cout << "Hello, World!" << std::endl;
}
};

int
test_main(int, char* [])
{
// Signal with no arguments and a void return value
boost::signal sig;

// Connect a HelloWorld slot
HelloWorld hello;
sig.connect(hello);

// Call all of the slots
//sig();
return 0;
}


Compilation will be completed.

If you have any question, it's great to post in comment :-)


Read More...

Monday, September 24, 2007

int to string code in wxwidgets

I've been looking for a method that can cast int type to string type
Here are some solution you can use

For console mode by standard C
1. int sprintf( char *buffer, const char *format [, argument] ... );
ex:


char *str;
int num = 66;

sprintf(str, “%d”, num);
printf(”str = %s\n”, str);


2. char *_itoa( int value, char *string, int radix ); 
ex:

#include <stdlib.h>
char buffer[20];
int i = 1234;
_itoa( i, buffer, 10 );
string s(buffer);



3. Using boost library to cast. Must download boost C++ libraries first.
ex:

#include <boost\lexical_cast.h>
int i;
std::string str = boost::lexical_cast&ltstd::string>(i);



4. In wxwidgets GUI application from here
ex:

 long number; // this can be any number type such as int or double ,etc...
 wxString stringnumber = wxString::Format(wxT("%d"), (int)number);

Read More...

Tuesday, June 5, 2007

Color space conversion for IplImage

In my application, the color image is captured from my USB webcam.


IplImage* frame = cvQueryFrame( capture ); // depth 8 channel 3
IplImage* blackwhite = cvCreateImage( cvGetSize(frame), frame->depth, 1 ); //depth 8 channel 1
IplImage* differentdepth = cvCreateImage( cvGetSize(frame), IPL_DEPTH_32F, 1); //depth 32 channel 1

cvCvtColor(frame, blackwhite, CV_RGB2GRAY); // to convert greyscale




Reference:
IplImage* cvCreateImage( cvSize, Ipl depth flag, channel number);

Read More...

Wednesday, May 23, 2007

Day record - 2007 May 23



Finally, I made it. I found out a little GOD DAMN BUG!!
That is the type of IplImage image data format is "char".That means if it is bigger than 128, while changed to "int", it will appear as a negative one. In computer vision, the range of each pixel's value is 0 ~ 255. In order to process correctly, each pixel value must be converted from (char*) to (unsigned char*) by cast for IplImage format( openCV ). Ex. unsigned char* im = (unsigned char*) IplImage->imageData;

Read More...

Tuesday, May 15, 2007

Day record - 2007 May 15



convert wxImage to IplImage by "WxImageToIplImage(const wxImage* wx_Image)"
Image processing(as the picture show above)
convert IplImage to wxImage by "IplImageToWxImage(const IplImage* cv_Image)"
Load the wxImage(processed) into my test application and show it.


/*********************************
Convert wxImage to IplImage format.
*********************************/
IplImage* GUIFrame::WxImageToIplImage(const wxImage* wx_Image)
{
if (wx_Image == NULL) return NULL ;
int nWidth = wx_Image->GetWidth () ;
int nHeight = wx_Image->GetHeight () ;
IplImage* cv_Image = NULL ;
cv_Image = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);
char* pcvImgData = cv_Image->imageData ;
memcpy((void*)pcvImgData, (void*)wx_Image->GetData(), nWidth*nHeight*3) ;
cvCvtColor(cv_Image, cv_Image, CV_BGR2RGB); //the 2nd argument return CvArr* type
return (IplImage*)cv_Image; //must cast to IplImage* type for executing correctly.
}

/*********************************
Convert IplImage to wxImage format.
*********************************/
wxImage* GUIFrame::IplImageToWxImage(const IplImage* cv_Image)
{
if (cv_Image == NULL) return NULL ;
int nWidth = cv_Image->width ;
int nHeight = cv_Image->height;
wxImage* wx_Image = new wxImage() ;
IplImage* cv_ImageBuf = cvCreateImage (cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3) ;
cvCvtColor((CvArr*)cv_Image, cv_ImageBuf, CV_BGR2RGB);
wx_Image->SetData((unsigned char*)cv_ImageBuf->imageData, nWidth, nHeight, false) ;
return wx_Image ;
}

Read More...

Monday, May 14, 2007

Day record - 2007 May 14



Build GUI interface for a really poor webcam.

  • Camera selection dialog.
  • Camera resource, format and control menu.
A simple callback function to paint 10 by 10(pixels) grid lines on each single frame.
  • callback(IplImage* image)

Read More...

Wednesday, April 25, 2007

A tutorial of WxSmith in Code::Blocks

http://wiki.codeblocks.org/index.php/WxSmith_Tutorial_%26_Pointers

Read More...

Compile WxWidgets with Code::Blocks by MinGW

First step:

Second step:
  • download WxWidgets 2.6.2(might in .7z or setup file format) and then install it to the dictionary C:\wxWidgets-2.6.2, or you can choose the other dictionary.
  • And then we need to compile WxWidgets with Code::Blocks by MINGW
Third step:
  • In command prompt, enter the following commands
  • C:\cd wxwidgets-2.6.2/build/msw
  • C:\mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=1 USE_XRC=1 UNICODE=1 VENDOR=cb
  • C:\cd ..\..\contrib\build\stc
  • C:\mingw32-make -f makefile.gcc BUILD=release SHARED=1 MONOLITHIC=1 USE_XRC=1 UNICODE=1 VENDOR=cb
about the compilation option, I compile 2 edition
BUILD=release UNICODE=1
BUILD=debug UNICODE=1 (change "release" to "debug" in third step and do it again)

Finally you can find there's a folder " \gcc_dll\ " build in C:\wxwidgets-2.6.2\lib, and then copy the dll files "wxmsw26u_gcc_cb.dll" and "wxmsw26ud_gcc_cb.dll" located in C:\wxwidgets-2.6.2\lib\gcc_lib into C:\windows\system32

Read More...

Tuesday, April 24, 2007

Errors Logged about CODE::BLOCKS

I found a powerful C++ IDE called "Code::Blocks" recently. It's a cross platform IDE and the most important thing is "It's FREE and easy to use". In it's website, the author supplies the patch to improve the program and fix the bug every night which is called "Nightly Builds". However, I found there's a little problem after updating the Code::Blocks with the newest Nightly Builds. A warning sliding window shows a message "Errors Logged" when I start Code::Blocks. And now I found the solution to solve this program.

The solution of Code::Blocks with MinGW in WinXPP

  1. Download the newest Nightly Builds(Ex. CB_20070419_rev3876_win32.7z) and unzip it to the folder which Code::Blocks located in.
  2. Download the wxWidgets dll wxmsw26u_gcc_cb_wx2.6.3p2.7z and unzip it to the system folder C:\Windows\system32\
After the steps described above you will find there's no errors when you start the Code::Blocks. :-)

Read More...

Saturday, November 25, 2006

Understanding of Gaussian noise


void GrayImage::addGaussianNoise (double mean, double variance)
{
  cout << " Adding Gaussian noise with mean = " << mean << "and variance = "
  << variance << " to the image..." ;

  cout.flush();
  srand(1);
  #define unit_random() (1.0*rand()/RAND_MAX)
  #define TWO_PI 6.28318530717958647688
  double temp, u1, u2;
  int ix, iy;
  int num = width*height;
  int tempint;
  u1=0.0;
  for(int i = 0; i < num/2; i++) //奇偶數分開且同時處理
                   故只需要一半的點數
  {
    while (u1 == 0.0) u1 = unit_random(); //避免log(u1)為Error
    u2 = unit_random();
    temp = sqrt(-2.0*variance*log(u1)); //part of Box-Muller Eq.
    ix = (2*i)/width; //偶數列位置,如 p[even][*] 偶數處理部份開始
    iy = (2*i)%width; //偶數行位置,如 p[*][even]
    tempint = p[ix][iy] + (int) (temp * cos(TWO_PI*u2) + mean);
    if (tempint > 255)
      p[ix][iy] = 255;
    else if (tempint < 0)
      p[ix][iy] = 0;
    else
      p[ix][iy] = (unsigned char) tempint; //轉成char型態
                         //是因為tempint為浮點數
    ix = (2*i+1)/width;   //奇數列位置,如 p[odd][*] 奇數處理部份開始
    iy = (2*i+1)%width;  //奇數列位置,如 p[*][odd]
    tempint = p[ix][iy] + (int) (temp * sin(TWO_PI*u2) + mean);
    if (tempint > 255)
      p[ix][iy] = 255;
    else if (tempint <>
      p[ix][iy] = 0;
    else
      p[ix][iy] = (unsigned char) tempint; //轉成char型態
                          是因為tempint為浮點數
    u1 = 0.0; //將u1重新設定為0
  }
  u1=0.0;
  if( num & 1 ) // If num is odd //最後一點之處理
  {
    while (u1 == 0.0) u1 = unit_random();
    u2 = unit_random();
    temp = sqrt(-2.0*variance*log(u1));
    ix = (num-1)/width;
    iy = (num-1)%width;
    tempint = p[ix][iy] + (int) (temp * cos(TWO_PI*u2) + mean);
    if (tempint > 255)
      p[ix][iy] = 255;
    else if (tempint < 0)
      p[ix][iy] = 0;
    else
      p[ix][iy] = (unsigned char) tempint;
    u1 = 0.0;
  }
  cout << "done!" << endl;
}



NOTE:
ix = (2*i+1)/width;
iy = (2*i+1)%width;

ix = (num-1)/width;
iy = (num-1)%width; 有所不同之處需自行體會一番。

Read More...