目录
内容说明
该博客内容主要为The Ancient Secrets of Computer Vision的作业说明
如果需要系统学习请到该网站自行观看和学习该课程的视频和课件
另外:代码都是乱写的。
hw0
1. Getting and setting pixels
float get_pixel(image im, int x, int y, int c);
计算某个pixel的值
要点:存储格式
当x,y不在范围内时用clamp方式显示pixel的值
float get_pixel(image im, int x, int y, int c)
{
// TODO Fill this in
if(x>im.w) x=im.w;
if(y>im.h) y= im.h;
if(x<0) x=0;
if(y<0) y=0;
//when the x or y is out of the image, use the clamp way to correct
int value;
value = x + im.w*y + im.w*im.h*c;//calculate the number of pixel
value = im.data[value];//read the value of pixel ?????
return value;
}
void set_pixel(image im, int x, int y, int c, float v);
就是按要求改变某一个pixel的值
要点同上
但是如果x,y不在范围内,则不作任何处理
2. Copying images
image copy_image(image im);
image copy_image(image im)
{
image copy = make_image(im.w, im.h, im.c);
// TODO Fill this in
copy.data=im.data;//copy the data
return copy;
}
3. Grayscale image
image rgb_to_grayscale(image im)
rgb转灰度的近似公式:Y’ = 0.299 R’ + 0.587 G’ + 0.114 B’
要点:就是给每个pixel赋值,值如上所示
image rgb_to_grayscale(image im)
{
assert(im.c == 3);
image gray = make_image(im.w, im.h, 1);
// TODO Fill this in
for(int x=1;x<im.w;x++)
{
for(int y=1;y<im.h;y++)
{
gray.data = 0.299 * get_pixel(im, x, y, 0)
+ 0.587 * get_pixel(im, x, y, 1)
+ 0.114 * get_pixel(im, x, y, 2);
//according to the formula
}
}
return gray;
}
4. Shifting the image colors
void shift_image(image im, int c, float v);
对通道c的每一个pixel都加上v值
要点:同上,改变一个通道的所有pixel
5. Clamping the image values
void clamp_image(image im);
除0-255,本文用0-1的浮点型存储data,当值>1时,数据溢出会变成非常小的值,比如您进行上面4的操作时,会有很奇怪的现象出现
要点:遍历值,将>1的值限制为1,<0的值限制为0
6. RGB to Hue, Saturation, Value
关于HSV
算法来源
RGB转化到HSV的算法void rgb_to_hsv(image im):
max=max(R,G,B);
min=min(R,G,B);
V=max(R,G,B);
S=(max-min)/max;
if (R = max) H =(G-B)/(max-min)* 60;
if (G = max) H = 120+(B-R)/(max-min)* 60;
if (B = max) H = 240 +(R-G)/(max-min)* 60;
if (H < 0) H = H+ 360;
7. HSV to RGB
HSV转化到RGB的算法:void hsv_to_rgb(image im):
if (s = 0)
R=G=B=V;
else
H /= 60;
i = INTEGER(H);
f = H - i;
a = V * ( 1 - s );
b = V * ( 1 - s * f );
c = V * ( 1 - s * (1 - f ) );
switch(i)
case 0: R = V; G = c; B = a;
case 1: R = b; G = v; B = a;
case 2: R = a; G = v; B = c;
case 3: R = a; G = b; B = v;
case 4: R = c; G = a; B = v;
case 5: R = v; G = a; B = b
8. A small amount of extra credit
void scale_image(image im, int c, float v);
和void shift_image(image im, int c, float v)类似,但是是通过HSV格式
hw1
1. Image resizing
float nn_interpolate(image im, float x, float y, int c);
最邻近元法的函数
image nn_resize(image im, int w, int h);
这个是用上面哪个方法创建一个新的图
改变图片内存大小
要点:计算映射函数参数ab,找到映射点最近的点的值,赋值
float nn_interpolate(image im, float x, float y, int c)
{
// TODO Fill in
float a,b;
a*(-0.5)+b=(-0.5);//obtain b
a*(x-0.5)+b=im.w-0.5;//obtain a
//m,n is the point of im
//M,N is the point of new
for()
{
for()
{
M=a*m+b;
N=a*n+b;
//find the nearest int of M,N
data[M,N]=data[nearest int];
}
}
return 0;
}
float bilinear_interpolate(image im, float x, float y, int c);
双线插值法的函数
image bilinear_resize(image im, int w, int h);
用上面方法插值得图片
要点同上面,只有data处不同
//find the nearest four int of M,N, namely V1,2,V3,V4
A1=(V4 .x - M) * (V4.y - N);//A2,A3,A4 are the same
data[M,N]=data[V1] * A1 + data[V2] * A2 + data[V3] * A3 + data[V4] * A4;
//according the formula q = V1 * A1 + V2 * A2 + V3 * A3 + V4 * A4
2. Image filtering with convolutions
2.1 Create your box filter
void l1_normalize(image im);
规范化函数
image make_box_filter(int w);
制作w x w的滤波器
void l1_normalize(image im)
{
// TODO
for()
{
for()
{
data[now]=1/(im.w*im.h*c);
// fill it in with all 1, and then normalize it
}
}
}
2.2 Write a convolution function
image convolve_image(image im, image filter, int preserve);
卷积函数
这里选择通道为1的滤波器
所以如果要输出RGB3个通道,需要分别进行操作
image convolve_image(image im, image filter, int preserve)
{
// TODO
if(preserve=1)for(c)//add a circle of channel
for(int x=filter.w/2;x<im.w-int(filter.w/2);x++)
{
for(int y=filter.w/2;y<im.h-int(filter.w/2);y++)
{
int listdata[]={};//list the value of pixel, totally w*w
int listfilter[]={};//list the value of fiter, totally w*w
im.data[x,y]=listdata[]*listfilter[];//Correspondence multiplication
}
}
return make_image(1,1,1);
}
2.3 Implement a Gaussian kernel
image make_gaussian_filter(float sigma);
??
//******************高斯卷积核生成函数*************************
void GetGaussianKernel(double **gaus, const int size,const double sigma)
{
const double PI=4.0*atan(1.0); //圆周率π赋值
int center=size/2;
double sum=0;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
gaus[i][j]=(1/(2*PI*sigma*sigma))*exp(-((i-center)*(i-center)+(j-center)*(j-center))/(2*sigma*sigma));
sum+=gaus[i][j];
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
gaus[i][j]/=sum;
cout<<gaus[i][j]<<" ";
}
cout<<endl<<endl;
}
return ;
}
3. Hybrid images
用上面的高斯滤波器得到低通图片,原图减去后得到高通,再混合
lfreq = convolve_image(im, f, 1)
hfreq = im - lfreq
reconstruct = lfreq + hfreq
4. Sobel filters
4.1 Make the filters
创建Sobel滤波器
4.2 One more normalization…
因为Sobel算子中有0,所以之前写的规范化不对,不能除以w * w
4.3 Calculate gradient magnitude and direction
image *sobel_image(image im);
它应该返回两个图像,梯度大小和方向?
卷积过后不是应该只有一个?
4.4 Make a colorized representation
image colorize_sobel(image im);
把边边变成彩色
hw2
1. Harris corner detection
1.1 Compute the structure matrix
image structure_matrix(image im, float sigma);
特征点的计算函数
返回一个同大小的image?
一个通道的矩阵不是都有四个数吗???
image structure_matrix(image im, float sigma)
{
image S = make_image(im.w, im.h, 3);
// TODO: calculate structure matrix for im.
for()
{
for()
{
x.data[now]=0.5*(-im.data[now-1]+im.data[now+1];//the derivatives Ix
y.data[now]=0.5*(-im.data[now-im.w]+im.data[now+im.w]);//the derivatives Iy
//create the eigen
//Calculate weighted sum of nearby measures
S.data[now]=
}
}
return S;
}
image make_1d_gaussian(float sigma);
不会高斯滤波
1.2 Computer cornerness from structure matrix
image cornerness_response(image S);
计算特征值,要后一个的特征值大才说明响应大啊
//caiculate eigenvalues
//2nd eigenvalue:det(s)-0.6*trace(s)=,namely q1*q2/(q1+q2)
//不会用代码写
1.3 Non-maximum suppression
image nms_image(image im, int w);
1.4 Complete the Harris detector