## [CV]检测灰色块

matlab 代码放到 Github 上了: detect-gray-square

## PCA的实现

PCA，全称是Principal component analysis，中文叫做主成分分析，是一种常用的数据处理手段。

PCA可以保证，在降维之后，数据表示的信息损失最小。

“损失最小”具体怎么定义？

SVD分解, matlab

```    sub_input_data = (input_data - repmat(mean(input_data),count,1))/sqrt(count-1);
[U,S,V] = svd(sub_input_data);
% First out_dim columns as PCA bases
pcaV = V(:,1:out_dim);
output_data = input_data * pcaV;
```

EIG分解, matlab

```    mean_input_data = mean(input_data);
sub_input_data = input_data - repmat(mean_input_data, count,1);
mean_mat = sub_input_data' * sub_input_data ./ (count - 1);
cov_mat = mean_mat;
[V D] = eig(cov_mat);
% Last out_dim columns as PCA bases
pcaV = V(:,in_dim - out_dim + 1: in_dim);
output_data = input_data * pcaV;
```

OpenCV下可以用这个方法做EIG分解。

```cv::eigen(covMat, eigenValues, eigenVectors);
```

MatlabPCA

PCA之后：

## Draw ROC Curve

A piece of fairly simple Matlab script to draw the ROC Curve from an array of scores and an array of labels.

```function [Tps, Fps] = ROC(scores, labels)

%% Sort Labels and Scores by Scores
sl = [scores; labels];
[d1 d2] = sort(sl(1,:));

sorted_sl = sl(:,d2);
s_scores = sorted_sl(1,:);
s_labels = round(sorted_sl(2,:));

%% Constants
counts = histc(s_labels, unique(s_labels));

Tps = zeros(1, size(s_labels,2) + 1);
Fps = zeros(1,  size(s_labels,2) + 1);

negCount = counts(1);
posCount = counts(2);

%% Shift threshold to find the ROC
for thresIdx = 1:(size(s_scores,2)+1)

% for each Threshold Index
tpCount = 0;
fpCount = 0;

for i = [1:size(s_scores,2)]

if (i >= thresIdx)           % We think it is positive
if (s_labels(i) == 1)   % Right!
tpCount = tpCount + 1;
else                    % Wrong!
fpCount = fpCount + 1;
end
end

end

Tps(thresIdx) = tpCount/posCount;
Fps(thresIdx) = fpCount/negCount;

end

%% Draw the Curve

% Sort [Tps;Fps]
x = Tps;
y = Fps;

% Interplotion to draw spline line
count = 100;
dist = (x(1) - x(size(x,2)))/100;
xx = [x(1):-dist:x(size(x,2))];

% In order to get the interpolations, we remove all the unique numbers
[d1 d2] = unique(x);
uni_x = x(1,d2);
uni_y = y(1,d2);
yy = spline(uni_x,uni_y,xx);

% No value should exceed 1
yy = min(yy, 1);

plot(x,y,'x',xx,yy);
```

Hope it helps.

For a sample input:

```>> scores = rand(1,20)*100

scores =

Columns 1 through 7

43.8744   38.1558   76.5517   79.5200   18.6873   48.9764   44.5586

Columns 8 through 14

64.6313   70.9365   75.4687   27.6025   67.9703   65.5098   16.2612

Columns 15 through 20

11.8998   49.8364   95.9744   34.0386   58.5268   22.3812

>> labels = round(rand(1,20))

labels =

Columns 1 through 12

1     0     1     1     1     1     1     0     0     0     1     0

Columns 13 through 20

1     0     1     0     0     0     1     0

>> ROC(scores,labels);
```

Gives an output like:

Fork it on Github: DrawROC on Github

## [Libsvm]libsvm for matlab compilation error on mac

Error:

```>> make
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 5
mex -O -largeArrayDims -I..\ -c ..\svm.cpp
```

Fix:

1. Under folder ./libsvm-3.1/matlab
2. Open file make.m
3. replace backslash(\) to slash(/)
4. save and close file
5. execute the make command inside matlab

Error:

```>> make
i686-apple-darwin10-gcc-4.2.1: svm.obj: No such file or directory
i686-apple-darwin10-gcc-4.2.1: svm_model_matlab.obj: No such file or directory

mex: link of ' "svmtrain.mexmaci64"' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 7
mex -O -largeArrayDims -I../ svmtrain.c svm.obj svm_model_matlab.obj
```

Fix:

1. Open the make.m file again
2. Replace all the occurrences of .obj to .o
3. Save and close
4. Try again!

Hope these help :]