Thứ Bảy, 31 tháng 1, 2015

Người giỏi và người dở hơi?

(Nguyễn Ngọc Thuần)

Người giỏi biến công việc nhàm chán thành tác phẩm, sau đó thưởng thức chúng. Bọn dở hơi biến tác phẩm thành công việc sau đó cặm cụi làm.

Người giỏi đi trễ nhưng sẵn sàng giải quyết và hoàn thành công việc bất kể giờ giấc nào, bất kể giá nào. Bọn dở hơi đi rất đúng giờ nhưng lãng công.

Người giỏi quản lí trên thành quả. Bọn dở hơi điểm danh.

Người giỏi giao việc và tin tưởng, bọn dở hơi liên tục để ý rình mò nhưng không có phát kiến.

Người giỏi đánh giá thành công và thất bại. Bọn dở hơi bói móc và truyền cảm hứng lo sợ.

Người giỏi biết tưởng thưởng người khác và lơ đi lỗi lầm của kẻ khác. Bọn dở hơi bới móc ra lỗi và liên tục cướp công.

Người giỏi nhân hậu trong mọi công việc. Bọn dở hơi tàn nhẫn trong mọi mối quan hệ.

Người giỏi im lặng. Bọn dở hơi nguyền rủa.

Người giỏi trung thực, trung dung. Bọn dở hơi nịnh nọt, xu thời.

Người giỏi nói ra ý của mình. Bọn dở hơi nói ý của lãnh đạo, hoặc mượn lãnh đạo để nói ý của mình.

Khi một lãnh đạo ra đi. Người giỏi tưởng nhớ. Bọn dở hơi chửi rủa.

Người giỏi nhìn về khó khăn của tương lai và khắc phục nó. Bọn dở hơi nhìn vào thành công quá khứ, và liên tục nhai lại.

Người giỏi tĩnh lặng. Bọn dở hơi chạy lăng xăng.

Người giỏi có vẻ biết rất ít nhưng biết chắc. Bọn dở hơi cái gì cũng có vẻ biết.

Người giỏi sử dụng người giỏi hơn mình và biết cách dùng. Bọn dở hơi luôn muốn sử dụng người dở hơn hắn và trù dập.

Người giỏi tin vào mình. Bọn dở hơi muốn người khác tôn vinh.

Khi cho một tí quyền hành. Người giỏi tìm giải pháp. Bọn dở hơi tìm vây cánh và triệt tiêu sự phát triển.

Người giỏi đề cao sáng tạo. Bọn dở hơi đề cao sự làm lụng.

Người giỏi biến một công nhân thành nghệ sĩ. Bọn dở hơi cố biến một nghệ sĩ thành công nhân.

Người giỏi yêu cuộc sống. Bọn dở hơi hoang mang và cố chiếm đoạt cuộc sống của người khác.

Người giỏi nhìn vào chính mình. Bọn dở hơi rình mò người khác và bới móc.

Người giỏi biến việc lớn thành việc nhỏ. Bọn dở hơi thổi phồng việc nhỏ thành việc lớn.

Thứ Bảy, 13 tháng 12, 2014

Danh mục Lớp và hàm trong OpenCV

OpenCV 2.4 Cheat Sheet (C++)
The OpenCV C++ reference manual is here: http: // docs. opencv. org . Use Quick Search to find descriptions of the particular functions and classes

1.1. Lớp từ khóa trong thư viện OpenCV


Key (từ khóa)
Classes (Lớp)
Point_
Template 2D point class - Lớp điểm ảnh 2D
Point3_
Template 3D point class - Lớp điểm ảnh 3D
Size_
Template size (width, height) class - Lớp kích thước ảnh (rộng, cao)
Vec
Template short vector class
Matx
Template small matrix class
Scalar
4-element vector
Rect
Rectangle
Range
Integer value range
Mat
2D or multi-dimensional dense array (can be used to store matrices, images, histograms, feature descriptors, voxel volumes etc.)
SparseMat
Multi-dimensional sparse array
Ptr
Template smart pointer class

1.2. Cơ bản về ma trận

Function
Meaning
Mat image(240, 320, CV_8UC3);
image.create(480, 640, CV_8UC3);
Mat A33(3, 3, CV_32F, Scalar(5));
Mat B33(3, 3, CV_32F); B33 = Scalar(5);
Mat C33 = Mat::ones(3, 3, CV_32F)*5.;
Mat D33 = Mat::zeros(3, 3, CV_32F) + 5.;

double a = CV_PI/3;
Mat A22 = (Mat_(2, 2) << cos(a), -sin(a), sin(a), cos(a));
float B22data[] = {cos(a), -sin(a), sin(a), cos(a)};
Mat B22 = Mat(2, 2, CV_32F, B22data).clone();

randu(image, Scalar(0), Scalar(256)); // uniform dist
randn(image, Scalar(128), Scalar(10)); // Gaussian dist

(without copying the data)
Mat image_alias = image;
float* Idata=new float[480*640*3];
Mat I(480, 640, CV_32FC3, Idata);
vector iptvec(10);
Mat iP(iptvec); // iP _ 10x1 CV_32SC2 matrix
IplImage* oldC0 = cvCreateImage(cvSize(320,240),16,1);
Mat newC = cvarrToMat(oldC0);
IplImage oldC1 = newC; CvMat oldC2 = newC;

(with copying the data)
Mat newC2 = cvarrToMat(oldC0).clone();
vector ptvec = Mat_(iP);


A33.at(i,j) = A33.at(j,i)+1;
Mat dyImage(image.size(), image.type());
for(int y = 1; y < image.rows-1; y++) {
Vec3b* prevRow = image.ptr(y-1);
Vec3b* nextRow = image.ptr(y+1);
for(int x = 0; x < image.cols; x++)
for(int c = 0; c < 3; c++)
dyImage.at(y,x)[c] =
saturate_cast(nextRow[x][c] - prevRow[x][c]);
}
Mat_::iterator it = image.begin(), itEnd = image.end();
for(; it != itEnd; ++it)
(*it)[1] ^= 255;

1.3. Matrix Manipulations: Copying, Shuffling, Part Access

Function
Meaning
src.copyTo(dst)
Copy matrix to another one
src.convertTo(dst,type,scale,shift)
Scale and convert to another datatype
m.clone()
Make deep copy of a matrix
m.reshape(nch,nrows)
Change matrix dimensions and/or number of channels without copying data
m.row(i), m.col(i)
Take a matrix row/column
m.rowRange(Range(i1,i2))
m.colRange(Range(j1,j2))
Take a matrix row/column span
m.diag(i)
Take a matrix diagonal
m(Range(i1,i2),Range(j1,j2)), m(roi)
Take a submatrix
m.repeat(ny,nx)
Make a bigger matrix from a smaller one
flip(src,dst,dir)
Reverse the order of matrix rows and/or columns
split(...)
Split multi-channel matrix into separate channels
merge(...)
Make a multi-channel matrix out of the separate channels
mixChannels(...)
Generalized form of split() and merge()
randShuffle(...)
Randomly shuffle matrix elements

Ví dụ 1. Smooth image ROI in-place
Mat imgroi = image(Rect(10, 20, 100, 100));
GaussianBlur(imgroi, imgroi, Size(5, 5), 1.2, 1.2);
Ví dụ 2. Somewhere in a linear algebra algorithm
m.row(i) += m.row(j)*alpha;
Ví dụ 3. Copy image ROI to another image with conversion
Rect r(1, 1, 10, 20);
Mat dstroi = dst(Rect(0,10,r.width,r.height));
src(r).convertTo(dstroi, dstroi.type(), 1, 0);

1.4. Simple Matrix Operations

OpenCV implements most common arithmetical, logical and other matrix operations, such as

Function
Meaning
add(), subtract(), multiply(), divide(), absdiff(), bitwise_and(), bitwise_or(),  itwise_xor(), max(), min(), compare()
Correspondingly, addition, subtraction, element-wise multiplication ... comparison of two matrices or a matrix and a scalar.

Ví dụ. Alpha compositing function:
void alphaCompose(const Mat& rgba1, const Mat& rgba2, Mat& rgba_dest)
{
Mat a1(rgba1.size(), rgba1.type()), ra1;
Mat a2(rgba2.size(), rgba2.type());
int mixch[]={3, 0, 3, 1, 3, 2, 3, 3};
mixChannels(&rgba1, 1, &a1, 1, mixch, 4);
mixChannels(&rgba2, 1, &a2, 1, mixch, 4);
subtract(Scalar::all(255), a1, ra1);
bitwise_or(a1, Scalar(0,0,0,255), a1);
bitwise_or(a2, Scalar(0,0,0,255), a2);
multiply(a2, ra1, a2, 1./255);
multiply(a1, rgba1, a1, 1./255);
multiply(a2, rgba2, a2, 1./255);
add(a1, a2, rgba_dest);
}
Function
Meaning
sum(), mean(), meanStdDev(), norm(), countNonZero(), minMaxLoc(),

various statistics of matrix elements.
exp(), log(), pow(), sqrt(), cartToPolar(), polarToCart()
the classical math functions.
scaleAdd(), transpose(), gemm(), invert(), solve(), determinant(), trace(), eigen(), SVD
the algebraic functions + SVD class.
dft(), idft(), dct(), idct(),
discrete Fourier and cosine transformations

For some operations a more convenient algebraic notation can be used, for Ví dụ:
Mat delta = (J.t()*J + lambda*Mat::eye(J.cols, J.cols, J.type())).inv(CV_SVD)*(J.t()*err);
implements the core of Levenberg-Marquardt optimization algorithm.

1.5. Image Processsing

1.5.1. Filtering

Function
Meaning
filter2D()
Non-separable linear filter
sepFilter2D()
Separable linear filter
boxFilter(),
Smooth the image with one of the linear or non-linear filters
GaussianBlur(),medianBlur(),
bilateralFilter()
Sobel(), Scharr()
Compute the spatial image derivatives
Laplacian()
compute Laplacian
erode(), dilate()
Morphological operations

                       
           
Ví dụ. Filter image in-place with a 3x3 high-pass kernel (preserve negative responses by shifting the result by 128):
filter2D(image, image, image.depth(), (Mat_(3,3) <-1 -1="" 128="" 9="" o:p="" point="">

1.5.2. Geometrical Transformations

Function
Meaning
resize()
Resize image
getRectSubPix()
Extract an image patch
warpAffine()  
Warp image afinely
warpPerspective()
Warp image perspectively
remap()
Generic image warping
convertMaps()
Optimize maps for a faster remap() execution

Ví dụ. Decimate image by factor of :
Mat dst; resize(src, dst, Size(), 1./sqrt(2), 1./sqrt(2));

1.5.3. Various Image Transformations


Function
Meaning
cvtColor()
Convert image from one color space to another
threshold(),
adaptivethreshold()
Convert grayscale image to binary image using a fixed or a variable threshold
floodFill()
Find a connected component using region growing algorithm
integral()
Compute integral image
distanceTransform()
build distance map or discrete Voronoi diagram for a binary image.
watershed(),grabCut()
marker-based image segmentation algorithms. See the samples
watershed.cpp and grabcut.cpp.

1.5.4. Histograms

Function
Meaning
calcHist()
Compute image(s) histogram
calcBackProject()
Back-project the histogram
equalizeHist()
Normalize image brightness and contrast
compareHist()
Compare two histograms

Ví dụ. Compute Hue-Saturation histogram of an image:
Mat hsv, H;
cvtColor(image, hsv, CV_BGR2HSV);
int planes[]={0, 1}, hsize[] = {32, 32};
calcHist(&hsv, 1, planes, Mat(), H, 2, hsize, 0);

1.5.5. Contours

See contours2.cpp and squares.cpp samples on what are the contours and how to use them.

1.6. Data I/O

XML/YAML storages are collections (possibly nested) of scalar values, structures and heterogeneous lists.

1.6.1. Writing data to YAML (or XML)

// Type of the file is determined from the extension
FileStorage fs("test.yml", FileStorage::WRITE);
fs << "i" << 5 << "r" << 3.1 << "str" << "ABCDEFGH";
fs << "mtx" << Mat::eye(3,3,CV_32F);
fs << "mylist" << "[" << CV_PI << "1+1" << "{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
fs << "mystruct" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";
const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));
fs << "]" << "}";
Scalars (integers, floating-point numbers, text strings), matrices, STL vectors of scalars and some other types can be written to the file storages using << operator.

1.6.2. Reading the data back

// Type of the file is determined from the content
FileStorage fs("test.yml", FileStorage::READ);
int i1 = (int)fs["i"]; double r1 = (double)fs["r"];
string str1 = (string)fs["str"];
Mat M; fs["mtx"] >> M;
FileNode tl = fs["mylist"];
CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 3);
double tl0 = (double)tl[0]; string tl1 = (string)tl[1];
int m = (int)tl[2]["month"], d = (int)tl[2]["day"];
int year = (int)tl[2]["year"];
FileNode tm = fs["mystruct"];
Rect r; r.x = (int)tm["x"], r.y = (int)tm["y"];
r.width = (int)tm["width"], r.height = (int)tm["height"];
int lbp_val = 0;
FileNodeIterator it = tm["lbp"].begin();
for(int k = 0; k < 8; k++, ++it)
lbp_val |= ((int)*it) << k;

Scalars are read using the corresponding FileNode's cast operators. Matrices and some other types are read using operator. Lists can be read using FileNodeIterator's.

1.6.3. Writing and reading raster images

imwrite("myimage.jpg", image);
Mat image_color_copy = imread("myimage.jpg", 1);
Mat image_grayscale_copy = imread("myimage.jpg", 0);
The functions can read/write images in the following formats:
BMP (.bmp), JPEG (.jpg, .jpeg), TIFF (.tif, .tiff),
PNG (.png), PBM/PGM/PPM (.p?m), Sun Raster (.sr), JPEG 2000 (.jp2).
Every format supports 8-bit, 1 or 3-channel images. Some formats (PNG, JPEG 2000) support 16 bits per channel.

1.6.4. Reading video from a file or from a camera

VideoCapture cap;
if(argc > 1) cap.open(string(argv[1])); else cap.open(0);
Mat frame; namedWindow("video", 1);
for(;;) {
cap >> frame; if(!frame.data) break;
imshow("video", frame); if(waitKey(30) >= 0) break;
}

1.7. Simple GUI (highgui module)

Function
Meaning
namedWindow(winname,flags)
Create named highgui window
destroyWindow(winname)
Destroy the specified window
imshow(winname, mtx)
Show image in the window
waitKey(delay)
Wait for a key press during the specified time interval (or forever). Process events while waiting. Do not forget to call this function several times a second in your code.
createTrackbar(...)
Add trackbar (slider) to the specified window
setMouseCallback(...)
Set the callback on mouse clicks and movements in the specified window

See camshiftdemo.cpp and other OpenCV samples on how to use the GUI functions.

1.8. Camera Calibration, Pose Estimation and Depth Estimation

Function
Meaning
calibrateCamera()
Calibrate camera from several views of a calibration pattern.
findChessboardCorners()
Find feature points on the checker board calibration pattern.
solvePnP()
Find the object pose from the known projections of its feature
points.
stereoCalibrate()
Calibrate stereo camera.
stereoRectify()
Compute the rectification transforms for a calibrated stereo
camera.
initUndistortRectifyMap()
Compute rectification map (for remap()) for each stereo camera
head.
StereoBM, StereoSGBM
The stereo correspondence engines to be run on rectified stereo
pairs.
reprojectImageTo3D()
Convert disparity map to 3D point cloud.
findHomography()
Find best-fit perspective transformation between two 2D point
sets.