Thursday, September 6, 2012

CvArr、Mat、CvMat、IplImage、BYTE转换


CvArr、Mat、CvMat、IplImage、BYTE转换(总结而来)

http://blog.csdn.net/wuxiaoyao12/article/details/7305848
 一、Mat类型:矩阵类型,Matrix。
    在openCV中,Mat是一个多维的密集数据数组。可以用来处理向量和矩阵、图像、直方图等等常见的多维数据。
    Mat有3个重要的方法:
         1、Mat mat = imread(const String* filename);            读取图像
         2、imshow(const string frameName, InputArray mat);      显示图像
         3、imwrite (const string& filename, InputArray img);    储存图像
    Mat类型较CvMat与IplImage类型来说,有更强的矩阵运算能力,支持常见的矩阵运算。在计算密集型的应用当中,将CvMat与IplImage类型转化为Mat类型将大大减少计算时间花费。
A.Mat -> IplImage
同样只是创建图像头,而没有复制数据。
例: // 假设Mat类型的imgMat图像数据存在
IplImage pImg= IplImage(imgMat); 
B.Mat -> CvMat
与IplImage的转换类似,不复制数据,只创建矩阵头。
例: // 假设Mat类型的imgMat图像数据存在
     CvMat cvMat = imgMat;

二、CvMat类型与IplImage类型:“图像”类型
       在openCV中,Mat类型与CvMat和IplImage类型都可以代表和显示图像,但是,Mat类型侧重于计算,数学性较高,openCV对Mat类型的计算也进行了优化。而CvMat和IplImage类型更侧重于“图像”,openCV对其中的图像操作(缩放、单通道提取、图像阈值操作等)进行了优化。
补充:IplImageCvMat派生,而CvMatCvArr派生即CvArr -> CvMat -> IplImage
            CvArr用作函数的参数,无论传入的是CvMatIplImage,内部都是按CvMat处理。
1.CvMat
A.CvMat-> IplImage
IplImage* img = cvCreateImage(cvGetSize(mat),8,1);
cvGetImage(matI,img);
cvSaveImage("rice1.bmp",img);
B.CvMat->Mat
与IplImage的转换类似,可以选择是否复制数据。
Mat::Mat(const CvMat* m, bool copyData=false);
在openCV中,没有向量(vector)的数据结构。任何时候,但我们要表示向量时,用矩阵数据表示即可。
但是,CvMat类型与我们在线性代数课程上学的向量概念相比,更抽象,比如CvMat的元素数据类型并不仅限于基础数据类型,比如,下面创建一个二维数据矩阵:
              CvMat* cvCreatMat(int rows ,int cols , int type);
这里的type可以是任意的预定义数据类型,比如RGB或者别的多通道数据。这样我们便可以在一个CvMat矩阵上表示丰富多彩的图像了。

2.IplImage
在类型关系上,我们可以说IplImage类型继承自CvMat类型,当然还包括其他的变量将之解析成图像数据。
IplImage类型较之CvMat多了很多参数,比如depth和nChannels。在普通的矩阵类型当中,通常深度和通道数被同时表示,如用32位表示RGB+Alpha.但是,在图像处理中,我们往往将深度与通道数分开处理,这样做是OpenCV对图像表示的一种优化方案。
IplImage的对图像的另一种优化是变量origin----原点。在计算机视觉处理上,一个重要的不便是对原点的定义不清楚,图像来源,编码格式,甚至操作系统都会对原地的选取产生影响。为了弥补这一点,openCV允许用户定义自己的原点设置。取值0表示原点位于图片左上角,1表示左下角。
dataOrder参数定义数据的格式。有IPL_DATA_ORDER_PIXEL和IPL_DATA_ORDER_PLANE两种取值,前者便是对于像素,不同的通道的数据交叉排列,后者表示所有通道按顺序平行排列。
IplImage类型的所有额外变量都是对“图像”的表示与计算能力的优化。
A.IplImage -> Mat
IplImage* pImg = cvLoadImage("lena.jpg");
Mat img(pImg,0); // 0是不複製影像,也就是pImgimgdata共用同個記憶體位置,header各自有
B.IplImage -> CvMat
1CvMat mathdr, *mat = cvGetMat( img, &mathdr );
法2CvMat *mat = cvCreateMat( img->height, img->width, CV_64FC3 );
  cvConvert( img, mat );
C.IplImage*-> BYTE*
BYTE* data= img->imageData;

CvMat和IplImage创建时的一个小区别:
1、建立矩阵时,第一个参数为行数,第二个参数为列数。
CvMat* cvCreateMat( int rows, int cols, int type );
2、建立图像时,CvSize第一个参数为宽度,即列数;第二个参数为高度,即行数。这 个和CvMat矩阵正好相反。
IplImage* cvCreateImage(CvSize size, int depth, int channels );
CvSize cvSize( int width, int height );

IplImage内部buffer每行是按4字节对齐的,CvMat没有这个限制

补充:
A.BYTE*-> IplImage*
img= cvCreateImageHeader(cvSize(width,height),depth,channels);
cvSetData(img,data,step);
//首先由cvCreateImageHeader()创建IplImage图像头,制定图像的尺寸,深度和通道数;
//然后由cvSetData()根据BYTE*图像数据指针设置IplImage图像头的数据数据,
//其中step指定该IplImage图像每行占的字节数,对于1通道的IPL_DEPTH_8U图像,step可以等于width

Wednesday, September 5, 2012

Resolving tbb_debug.dll in OpenCV 2.3.1


Resolving tbb_debug.dll in OpenCV 2.3.1

origin:

To resolve the tbb_debug.dll, for windows: 

Download tbb files at 
http://threadingbuildingblocks.org/download#stable-releases

You may choose to place the folder at ..\OpenCV2.3\build\common
 

Set up the following:

• Environment variables 
$(TBBROOT)bin\ia32\vc10

C/C++ Properties 
• General: add an additional include directory:
"$(TBBROOT)\include"

Linker Properties 
• General: add an additional library directory (shown for Visual
Studio 2010 32-bit library):
$(TBBROOT)lib\ia32\vc10

• Input: add an additional dependency 
tbb_debug.lib or tbb.lib


This should resolve the error message.


Sunday, September 2, 2012

04.10.10 - Installing OpenGL/Glut libraries in Ubuntu

from http://www.kiwwito.com
The first step is to install the development libraries of OpenGL/Glut in Ubuntu:
sudo apt-get install freeglut3 freeglut3-dev

For newer versions of Ubuntu (>= 11.10) you have to install another package because the linker does't link anymore

.
sudo apt-get install binutils-gold

Create a test file (test.c):


#include <GL/glut.h>

//Drawing funciton
void draw(void)
{
  //Background color
  glClearColor(0,1,0,1);
  glClear(GL_COLOR_BUFFER_BIT );
  //Draw order
  glFlush();
}

//Main program
int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  //Simple buffer
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
  glutInitWindowPosition(50,25);
  glutInitWindowSize(500,250);
  glutCreateWindow("Green window");
  //Call to the drawing function
  glutDisplayFunc(draw);
  glutMainLoop();
  return 0;
}

Compile the file linking the OpenGL/Glut libraries:
gcc -lGL -lglut test.c -o test

Wednesday, August 29, 2012

Switch


#include "iostream"
#include "iomanip"
using namespace std;

void main()
{
// Code 3, test switch fuction
bool quit = false;
char response;
cout << "Please input a, b, c,or q" << endl;
cin >> response;
while(quit == false)
{
cin >> response;
switch(response)
{
case 'a': cout <<"You chose 'a'" << setw(3) << endl; break;
case 'b': cout <<"You chose 'b'" << setw(3) << endl; break;
case 'c': cout <<"You chose 'c'" << setw(3) << endl; break;
case 'q': cout <<"You chose 'q', Program Will quit" << setw(3) << endl; quit = true; break;
default : cout <<"Please choose only a b c q" << endl;
}
};
while (getchar())
{
if (getchar())  break;
}///
//return 0;
}

Tuesday, August 28, 2012

IF 。。。。


IF.....


IF you can keep your head when all about you
Are losing theirs and blaming it on you,
If you can trust yourself when all men doubt you,
But make allowance for their doubting too;

If you can wait and not be tired by waiting,
Or being lied about, don't deal in lies,
Or being hated, don't give way to hating,
And yet don't look too good, nor talk too wise:
If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
If you can meet with Triumph and Disaster
And treat those two impostors just the same;

If you can bear to hear the truth you've spoken
Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to, broken,
And stoop and build 'em up with worn-out tools:

If you can make one heap of all your winnings
And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings
And never breathe a word about your loss;

If you can force your heart and nerve and sinew
To serve your turn long after they are gone,
And so hold on when there is nothing in you
Except the Will which says to them: 'Hold on!'

If you can talk with crowds and keep your virtue,
' Or walk with Kings - nor lose the common touch,
if neither foes nor loving friends can hurt you,
If all men count with you, but none too much;
If you can fill the unforgiving minute
With sixty seconds' worth of distance run,
Yours is the Earth and everything that's in it,
And - which is more - you'll be a Man, my son!

Friday, August 17, 2012

False positive and False negative

Type I error

type I error, also known as an error of the first kind, occurs when the null hypothesis (H0) is true, but is rejected. It is asserting something that is absent, a false hit. A type I error may be compared with a so called false positive (a result that indicates that a given condition is present when it actually is not present) in tests where a single condition is tested for. Type I errors are philosophically a focus of skepticism and Occam's razor. A Type I error occurs when we believe a falsehood.[1] In terms of folk tales, an investigator may be "crying wolf" without a wolf in sight (raising a false alarm) (H0: no wolf).
The rate of the type I error is called the size of the test and denoted by the Greek letter \alpha (alpha). It usually equals the significance level of a test. In the case of a simple null hypothesis \alpha is the probability of a type I error. If the null hypothesis is composite, \alpha is the maximum (supremum) of the possible probabilities of a type I error.

False positive error

false positive error, commonly called a "false alarm" is a result that indicates a given condition has been fulfilled, when it actually has not been fulfilled. In the case of "crying wolf" - the condition tested for was "is there a wolf near the herd?", the actual result was that there had not been a wolf near the herd. The shepherd wrongly indicated there was one, by calling "Wolf, wolf!".
A false positive error is a Type I error where the test is checking a single condition, and results in an affirmative or negative decision usually designated as "true or false".

Type II error

type II error, also known as an error of the second kind, occurs when the null hypothesis is false, but it is erroneously accepted as true. It is missing to see what is present, a miss. A type II error may be compared with a so-called false negative (where an actual 'hit' was disregarded by the test and seen as a 'miss') in a test checking for a single condition with a definitive result of true or false. A Type II error is committed when we fail to believe a truth.[1] In terms of folk tales, an investigator may fail to see the wolf ("failing to raise an alarm"; see Aesop's story of The Boy Who Cried Wolf). Again, H0: no wolf.
The rate of the type II error is denoted by the Greek letter \beta (beta) and related to the power of a test (which equals 1-\beta).
What we actually call type I or type II error depends directly on the null hypothesis. Negation of the null hypothesis causes type I and type II errors to switch roles.
The goal of the test is to determine if the null hypothesis can be rejected. A statistical test can either reject (prove false) or fail to reject (fail to prove false) a null hypothesis, but never prove it true (i.e., failing to reject a null hypothesis does not prove it true).

False negative error

false negative error is where a test result indicates that a condition failed, while it actually was successful. A common example is a guilty prisoner freed from jail. The condition: "Is the prisoner guilty?" actually had a positive result (yes, he is guilty). But the test failed to realize this, and wrongly decided the prisoner was not guilty.
A false negative error is a type II error occurring in test steps where a single condition is checked for and the result can either be positive or negative.

Example

As it is conjectured that adding fluoride to toothpaste protects against cavities, the null hypothesis of no effect is tested. When the null hypothesis is true (i.e., there is indeed no effect), but the data give rise to rejection of this hypothesis, falsely suggesting that adding fluoride is effective against cavities, a type I error has occurred.
A type II error occurs when the null hypothesis is false (i.e., adding fluoride is actually effective against cavities), but the data are such that the null hypothesis cannot be rejected, failing to prove the existing effect.
In colloquial usage type I error can be thought of as "convicting an innocent person" and type II error "letting a guilty person go free".
Tabularised relations between truth/falseness of the null hypothesis and outcomes of the test:
Null hypothesis (H0) is trueNull hypothesis (H0) is false
Reject null hypothesisType I error
False positive
Correct outcome
True positive
Fail to reject null hypothesisCorrect outcome
True negative
Type II error
False negative

Understanding Type I and Type II errors

From the Bayesian point of view, a type I error is one that looks at information that should not substantially change one's prior estimate of probability, but does. A type II error is one that looks at information which should change one's estimate, but does not. (Though the null hypothesis is not quite the same thing as one's prior estimate, it is, rather, one's pro forma prior estimate.)
Hypothesis testing is the art of testing whether a variation between two sample distributions can be explained by chance or not. In many practical applications type I errors are more delicate than type II errors. In these cases, care is usually focused on minimizing the occurrence of this statistical error. Suppose, the probability for a type I error is 1% , then there is a 1% chance that the observed variation is not true. This is called the level of significance, denoted with the Greek letter \alpha (alpha). While 1% might be an acceptable level of significance for one application, a different application can require a very different level. For example, the standard goal of six sigma is to achieve precision to 4.5 standard deviations above or below the mean. This means that only 3.4 parts per million are allowed to be deficient in a normally distributed process

Tuesday, August 14, 2012