기본 콘텐츠로 건너뛰기

글

라벨이 Vision인 게시물 표시

[OpenCV] OpenCV 2.1 Cheat Sheet C++

OpenCV_CheatSheet.pdf [OpenCV] OpenCV 2.1 Cheat Sheet C++

[MATLAB]Polynomial curve fitting - polyfit

polyfit - Polynomial curve fitting Syntax p = polyfit(x,y,n) [p,S] = polyfit(x,y,n) [p,S,mu] = polyfit(x,y,n) Description p = polyfit(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits the data, p(x(i)) to y(i) , in a least squares sense. The result p is a row vector of length n+1 containing the polynomial coefficients in descending powers: [p,S] = polyfit(x,y,n) returns the polynomial coefficients p and a structure S for use with polyval to obtain error estimates or predictions. Structure S contains fields R , df , and normr , for the triangular factor from a QR decomposition of the Vandermonde matrix of x , the degrees of freedom, and the norm of the residuals, respectively. If the data y are random, an estimate of the covariance matrix of p is (Rinv*Rinv')*normr^2/df , where Rinv is the inverse of R . If the errors in the data y are independent normal with constant variance, polyval produces error bounds that contain at least 50...

Pearson 상관계수(sample correlation coefficient)

Pearson 의 상관계수(sample correlation coefficient)는 계산에 가장 편리한 것 중의 하나로, 두 변수 x, y 가 선형관계라면 다음과 같이 계산된다. r 값의 범위 -1 ≤ r ≤ +1 이며, r=1 은 두 변수 사이에 완전한 상관관계에 있는 경우이다. r 값이 0 일 때(xy = 0 일때 생긴다) 두 변수는 완전히 독립이다. 일반적으로 0.90 ≤ r ≤ 0.95 : 보통 0.95 ≤ r ≤ 0.99 : 양호 0.99 ≤ r : 훌륭한 직선성을 나타냄

[OpenCV]CvMat의 element 접근법

// 3x3 행렬 생성. element 자료형은 double형에 1 channel CvMat* matrix = cvCreateMat(3, 3, CV_64FC1); double element; // 1번째행 2번째열 데이터 접근하는 법. matrix->data.db[1*3+2] = element; element = matrix->data.db[1*3+2]; 또는 element = cvmGet(matrix, 1, 2); cvmSet(matrix, 1, 2, element); 그렇다면 matrix->data.db[1*3+1]에서 1*3+1에 대해서 알아보자. 일부러 3x3행렬의 중앙에 있는 element를 지정해 놓았다. 그 이유는 1*3+2의 의미를 확실히 외워라는 의미이다. | 1 2 3 | | 4 5 6 | 다음은 3x3 행렬로 각각의 element가 가지는 값을 표시해놓았다. | 7 8 9 | 1*3+1에서 먼저 1이 나타내는 것은 두번째 행(row)임을 나타내는 것이다. 열(column)이 절대 아님. 그렇다면 3이 나타내는 것은 당연히 이 행렬에서 한 행이 가지는 element의 수이다. 다시 말해서 열의 크기를 나타낸다. 마지막으로 1은 열을 나타낸다. 2번째 열에 있다는 이야기이다. 다음의 예를 보자. | 1 2 3 | | 4 5 6 | | 7 8 9 | 3번째 행 1번째 열을 나타내는 식은? 2*3+0 cvmGet이나 cvmSet을 이용하는 것보다 직접 접근하는 것이 편하지만 만약 행과 열을 헷갈린다면 결과는 OTL일 것이다. 신경써서 사용하자. 출처 : http://devnetga.com/blogtt/5?TSSESSIONdevnetgacom=5c8bf4d129f674b65a0766b03f728026   CV_32FC1과 CV_32FC2의 구조를 비교해보자.  CvMat *mat1 = cvCreateMat(2, 3, CV_32F);  CvMat *mat2 = cvCreateMat(1, 3, CV_32FC2);...

[OpenCV]matrix operations

Matrix-matrix operations: CvMat *Ma, *Mb, *Mc; cvAdd(Ma, Mb, Mc); // Ma+Mb -> Mc cvSub(Ma, Mb, Mc); // Ma-Mb -> Mc cvMatMul(Ma, Mb, Mc); // Ma*Mb -> Mc Elementwise matrix operations: CvMat *Ma, *Mb, *Mc; cvMul(Ma, Mb, Mc); // Ma.*Mb -> Mc cvDiv(Ma, Mb, Mc); // Ma./Mb -> Mc cvAddS(Ma, cvScalar(-10.0), Mc); // Ma.-10 -> Mc Vector products: double va[] = {1, 2, 3}; double vb[] = {0, 0, 1}; double vc[3]; CvMat Va=cvMat(3, 1, CV_64FC1, va); CvMat Vb=cvMat(3, 1, CV_64FC1, vb); CvMat Vc=cvMat(3, 1, CV_64FC1, vc); double res=cvDotProduct(&Va,&Vb); // dot product: Va . Vb -> res cvCrossProduct(&Va, &Vb, &Vc); // cross product: Va x Vb -> Vc end{verbatim} Note that Va, Vb, Vc, must be 3 element vectors in a cross product. Single matrix operations: CvMat *Ma, *Mb; cvTranspose(Ma, Mb); // transpose(Ma) -> Mb (cannot transpose onto self) CvScalar t = cvTrace(Ma); // trace(Ma) -> t.val[0] double d = cvDet(Ma); // det(Ma) -> d cvInvert(Ma, Mb); // ...

[MATLAB]Circle Fit

  http://www.mathworks.com/matlabcentral/fileexchange/5557-circle-fit

[OpenCV]cvCompareHist - CV_COMP_INTERSECT

[code cpp] double CompareHistogram(IplImage* BufSrc, IplImage* BufRef) {  double dfScore;  int nHistoSize = 256;  float ranges[] = { 0, 256 };  float *hist_range[] = { ranges }; CvHistogram *histSrc = cvCreateHist(1, &nHistoSize, CV_HIST_ARRAY, hist_range);  CvHistogram *histRef = cvCreateHist(1, &nHistoSize, CV_HIST_ARRAY, hist_range);  cvCalcHist(&BufSrc, histSrc, 0, NULL );  cvCalcHist(&BufRef, histRef, 0, NULL );  cvNormalizeHist( histSrc, 100);  cvNormalizeHist( histRef, 100);     // Score = 0.0 ~ 100.0  dfScore = cvCompareHist( histSrc, histRef, CV_COMP_INTERSECT ); return dfScore; } [/code]

[OpenCV]cvDrawContours

cvDrawContours 함수는 contour 를 그려주는 함수이며 thickness 가 0 이상일 경우에는 외곽선만을 그려주고 0 미만일 경우에는 내부를 채워준다 .   원형 void cvDrawContours ( CvArr *img, CvSeq* contour,                                 CvScalar external_color, CvScalar hole_color,                                 int max_level, int thickness=1,                                 int line_type=8, CvPoint offset=cvPoint(0,0) );   패러미터 img contour 를 그릴 IplImage 구조체의 포인터를 넘겨준다 . 다른 그리기 함수와 마찬가지로 contour 는 ROI 로 클리핑된다 . contour CvSeq 구조체의 포인터를 넘겨준다 external_color 외부 contour 의 색상을 설정한다 . hole_color 내부 hole 의 색상을 설정한다 . max_level 그려질 contour 의 최대 단계를 설정한다 . 만약 0 이면 contour 만이 그려질 것이고 1 이면 모든 contour 를 같은 레벨에 그려준다 . 2 이면 다음 레벨까지 그려줄 것이다 . 만약 음수이면...

[OpenCV]자주쓰는 기능..

[code cpp] //Exec Notepad CFileFind ff; //if( ff.FindFile( _T("C:\\WINDOWS\\notepad.exe") ) && ff.FindFile( _T("C:\\TEST\\temp.txt") ) ) // WinExec( "C:\\WINDOWS\\notepad.exe C:\\PARMI\\temp.txt", SW_SHOW );if( ff.FindFile( _T("C:\\WINDOWS\\notepad.exe") ) && ff.FindFile( strFilePath ) ) {  CString strCmd;  strCmd.Format( _T("C:\\WINDOWS\\notepad.exe ") );  strCmd += strFilePath;  USES_CONVERSION;  WinExec( W2A(strCmd), SW_SHOW ); } [/code] [code cpp] //Show Image void cvShowImageWindow(CString title, CvArr* image) {  USES_CONVERSION;  cvNamedWindow( W2A(title), CV_WINDOW_AUTOSIZE);  cvShowImage( W2A(title), image);  HWND hWnd = ::FindWindow(NULL, title);  if (hWnd)   ::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); } [/code] [code cpp] //Save Image CString strFileName; std::string szFileName = CStringA( strFileName ); cvSaveImage( szFileName.c_str(), iplImage ); [/code]     [cod...

[OpenCV]accumulation_of_background

출처 : http://opencv.jp/sample/accumulation_of_background.html#background_sub   [code cpp] #include <cv.h> #include <highgui.h> #include <ctype.h> #include <stdio.h> int main (int argc, char **argv) {   int i, c, counter;   int INIT_TIME = 100;   int w = 0, h = 0;   double B_PARAM = 1.0 / 50.0;   double T_PARAM = 1.0 / 200.0;   double Zeta = 10.0;   CvCapture *capture = 0;   IplImage *frame = 0;   IplImage *av_img, *sgm_img;   IplImage *lower_img, *upper_img, *tmp_img;   IplImage *dst_img, *msk_img;   CvFont font;   char str[64];   // (1)コマンド引?によって指定された番?のカメラに?するキャプチャ構造?を作成する   if (argc == 1 || (argc == 2 && strlen (argv[1]) == 1 && isdigit (argv[1][0])))     capture = cvCreateCameraCapture (argc == 2 ? argv[1][0] - '0' : 0);   // (2)1フレ?ムキャプチャし,キャプチャサイズを取得する.   frame = cvQueryFrame (capture);   w = frame->width;   h = frame-...

Cramer rule - 크래머 공식

  크래머 공식 (Cramer's rule)은 선형연립방정식의 해를 행렬식 으로 표현하는 선형대수학 의 정리(theorem)이다. 이름은 가브리엘 크래머 (Gabriel Cramer) (1704 - 1752)에게서 유래한다.     방정식이 많은 경우의 실제 해의 계산에 있어서는 그리 유용하지 않지만, 피봇팅(pivoting)이 필요하지 않은 경우 작은 크기의 행렬에서는 가우스 소거법보다 훨씬 효율적이다. 크래머 공식은 연립방정식의 해를 외재적으로 표현하기 때문에 이론의 전개에 유용하다.   연립방정식이 다음과 같은 행렬 간의 곱으로 표현될 때. A x = c   식에서 정사각행렬 (square matrix) A 는 역행렬을 갖고, 벡터 x 는 ( x i ) 를, 벡터 c 는 ( c i ) 를 성분으로 갖는 열벡터이다.   정리는 다음과 같다. 식에서 A i 는 A 의 i 번째 열을 열벡터 c 로 대체한 행렬을 말한다.     예 [ 편집 ] 2x2 행렬에서 공식을 적용해 보면, 주어진 연립방정식이 다음과 같을 때, a x + b y = e c x + d y = f , 이 식은 로 쓸 수 있으며, 공식을 적용하면, 이 된다.         미분기하학에 적용 [ 편집 ]   크래머 공식은 미분기하 문제를 풀 때 매우 유용하다. 두 개의 방정식 , 이라 가정한다. 여기서, u 와 v 는 독립 변수이고, , 라 정의한다.   여기서 의 방정식을 찾는 것은 크래머 공식으로 해결할 수 있다.   첫째 , F,G,x,y 의 미분을 계산한다. dF, dG 에 dx 와 dy 를 대입하면   u 와...

선분과 한점의 수직 거리 구하기

Minimum Distance between a Point and a Line   Written by Paul Bourke October 1988   This note describes the technique and gives the solution to finding the shortest distance from a point to a line or line segment. The equation of a line defined through two points P1 (x1,y1) and P2 (x2,y2) is P = P1 + u ( P2 - P1 ) The point P3 (x3,y3) is closest to the line at the tangent to the line which passes through P3 , that is, the dot product of the tangent and line is 0, thus ( P3 - P ) dot ( P2 - P1 ) = 0 Substituting the equation of the line gives [ P3 - P1 - u( P2 - P1 )] dot ( P2 - P1 ) = 0 Solving this gives the value of u Substituting this into the equation of the line gives the point of intersection (x,y) of the tangent as x = x1 + u (x2 - x1) y = y1 + u (y2 - y1) The distance therefore between the point P3 and the line is the distance between (x,y) above and P3 . Notes The only special testing for a software implem...

OpenCV: fit line (line-fitting )

// fit-line for 2-D data (line-fitting) http://blog.weisu.org/2006/01/opencv-fit-line-line-fitting_14.html #include "stdafx.h" #include "cv.h" #include "highgui.h" #include <stdlib.h> void main ( int argc , char ** argv ) { FILE * fp ; int i ; int count = 48 ; // total number of points int tmp1 , tmp2 ; CvPoint left , right ; float * line ; line = new float [ 4 * count ]; IplImage * img = cvLoadImage ( "b.bmp" , 1 ); // just a black image cvNamedWindow ( "input_image" , CV_WINDOW_AUTOSIZE ); fp = fopen ( "xy.txt" , "r" ); // xy.txt have the (x, y) position CvPoint * points = ( CvPoint *) malloc ( count * sizeof ( points [ 0 ])); for ( i = 0 ; i < count ; i ++) { fscanf ( fp , "%d %d" , & tmp1 , & tmp2 ); points [ i ]. x = tmp1 ; ...