写在前面
作为一个计算机视觉门前那座山后面的大河对面的门外汉,抱着一腔热血选择了从事计算机视觉领域的相关研究,目前还是一个小小小小白,这篇文档是斯坦福计算机视觉相关课程cs131的配套练习的答案,由于本小小小小白能力和天赋的欠缺,所以一定会有很多的错误,恳请包涵,若有同行认真看过这篇博客,希请指正。
1 Calculus Review
(a) The height of a projectile at time t is given by
y(t)=V0t−5t2(1) (1) y ( t ) = V 0 t − 5 t 2
where v0 v 0 is a constant. Using this information, find the peak height (the max value of y) in terms of v0 v 0 .
Hint: Take the derivative of y(t) with respect to t, and then solve for the value of t that makes the derivative
equal 0. Use this to give the peak height (the max value of y) in terms of v0 v 0 .
answer:
对(1)求关于t的导数得
y′(t)=V0−10t(1.1) (1.1) y ′ ( t ) = V 0 − 10 t
故当 t=v010 t = v 0 10 时,(2)式为0,此时(1)式取得最大值。
The formula for the 2D Gaussian function above is
f(x,y)=e−(x−x0)2−(y−y0)22σ2(2) (2) f ( x , y ) = e − ( x − x 0 ) 2 − ( y − y 0 ) 2 2 σ 2
We will use calculus to find the peak (though the peak might be fairly obvious in this example, please answer
all steps below, to demonstrate that you can find it with calculus).
(b.i) Treat y as a constant, and give the derivative with respect to x.
(b.ii) Set the above derivative equal to 0 and solve for x. This maximizes the value of x for any given
y. Hint: find the value of x which makes one of the factors in the derivative zero. We can decide this is a
maximum and not a minimum by looking at the given plot. We could have also used the second derivative to confirm that there is negative curvature at that point.
(b.iii) Plug the maximizing x from part (b.ii) back into the original equation. Then find the derivative
of that expression with respect to y.
(b.iv) Set the above derivative equal to 0 and solve for y. Where is the peak of the 2D Gaussian function?
What is its value?
answer:
(b.i) we can see
∂f(x,y)∂x=−(x−x0)σ2e−(x−x0)2−(y−y0)22σ2(3) (3) ∂ f ( x , y ) ∂ x = − ( x − x 0 ) σ 2 e − ( x − x 0 ) 2 − ( y − y 0 ) 2 2 σ 2
(b.ii)when ∂f(x,y)∂x=0 ∂ f ( x , y ) ∂ x = 0 ,we can get
x=x0 x = x 0
we can easily get the max f(x,y) f ( x , y )
f(x0,y)=e−(y−y0)22σ2(4) (4) f ( x 0 , y ) = e − ( y − y 0 ) 2 2 σ 2
(b.iii)Derivatives for (4)
∂f(x0,y)∂y=−2(y−y0)e−(y−y0)22σ2(5) (5) ∂ f ( x 0 , y ) ∂ y = − 2 ( y − y 0 ) e − ( y − y 0 ) 2 2 σ 2
(b.iv)when ∂f(x0,y)∂y=0 ∂ f ( x 0 , y ) ∂ y = 0 ,we can solve the value of y,
y=y0 y = y 0
and we can obtain the max value of f(x,y) f ( x , y )
f(x,y)max=f(x0,y0)=1 f ( x , y ) m a x = f ( x 0 , y 0 ) = 1
2 Image Manipulation
Remember, you can use the MATLAB “doc” function to learn how to use a command (e.g. “doc rgb2gray”).
Also, the problems below can be easily solved without writing a single ‘for’ loop, if you use matrix math in
your MATLAB code (a.k.a. “vectorization”).
(a) The provided imageManip.m script reads in the provided u2dark.png photo and converts it to grayscale
using rgb2gray. Use the script and your own code to calculate the following statistics: What is the average
pixel value of the resulting grayscale image? What are the min and max values? (There are several ways to
calculate these quantities in MATLAB. Review the MATLAB tutorial if you need help.)
(b) We would like to bring the image to a more typical average brightness. Add an offset and apply a
scaling factor to all pixels, so that the minimum pixel value becomes 0 and the max pixel value becomes
255. (Cameras often do a similar function automatically.) Include the final image in your report, as well as
the MATLAB code you used to produce it.
(c) Next, we would like to double the contrast of the pixels in the middle brightness range. Specifically, take
your result from part (b) and replace each pixel’s intensity i with a new intensity i′ i ′ , where
i′=2∗(i−128)+128(2.1) (2.1) i ′ = 2 ∗ ( i − 128 ) + 128
Threshold i′ i ′ so that 0 ≤ i′ i ′ ≤ 255 (you can use the uint8 function). Include your MATLAB code and the
resulting contrast-boosted image in your report. Compare the image to part (b). What was the downside of
increasing contrast in this way, and why did it happen?
code is below
%reads in the image, converts it to grayscale, and converts the intensities
%from uint8 integers to doubles. (Brightness must be in 'double' format for
%computations, or else MATLAB will do integer math, which we don't want.)
dark = double(rgb2gray(imread('u2dark.png')));
gray=rgb2gray(imread('u2dark.png'));
%%%%%% Your part (a) code here: calculate statistics
[m,n]=size(dark);
s=;
for x=:m
for y=:n
s=s+dark(x,y); %求像素值总和 s , i(x,y)表示位于某个坐标下的像素值
end
end
%所有像素均值
a1=mean(mean(dark)); %第一种方法:先计算列向量均值,再求总均值。
a2=mean2(dark); %第二种方法:用函数mean2求总均值
a3=s/(m*n); %第三种方法:按公式计算,像素值总和除以像素个数。
a4=sum(sum(dark))/(m*n); %第四种方法:也是按公式计算,但是用sum来求像素值总和。
%求像素的最大值和最小值
max=;
min=;
for x=:m
for y=:n
if(dark(x,y)>max)
max=dark(x,y);
end
if(dark(x,y)<min)
min=dark(x,y);
end
end
end
%%%%%% Your part (b) code here: apply offset and scaling
ymax=;ymin=;
xmax = max; %求得InImg中的最大值
xmin = min; %求得InImg中的最小值
OutImg = round((ymax-ymin)*(dark-xmin)/(xmax-xmin) + ymin);
fixedimg=OutImg;
%displays the image
imshow(uint8(fixedimg));
%%%%%% Your part (c) code here: apply the formula to increase contrast,
% and display the image
contrasted = [];
3 Edge Detection
An “edge” is a place where image intensity changes abruptly. Edges can indicate the borders of objects.
(a) The intensity changes associated with vertical edges can be detected by calculating:
horizontal gradient at a pixel ≈ (the pixel) - (pixel on its left) for every pixel in the image. Open edgedetector.m and edit the DetectVerticalEdges() function to do this. Run the edgedetector() function to display your calculated gradients as an image.Compare the original image to the image of gradients. Verify that the vertical edges were detected and are visible as very bright or very dark areas in the gradient image. However, the gradient image also shows some tiny bright/dark spots that indicate “edges” in the water. What caused these tiny “edges?” Include your explanation and your gradient image in your report.
(b) We will now assess the effects of blurringthe gradient image. The simplest blurring operation is the box blur. In a box blur of width n, each blurred pixel is computed as follows:
blur(x,y)=1n2∑n−1i=0∑n−1j=0img(x+i,y+i)(3.1) (3.1) b l u r ( x , y ) = 1 n 2 ∑ i = 0 n − 1 ∑ j = 0 n − 1 i m g ( x + i , y + i )
In edgedetector.m, edit the BoxBlur() function to do this, and run the edgedetector() function to view the results. What was the effect of the blur on the large edges, compared to the tiny “noise” edges? Why? Include your explanation and your blurred edge image in your report.
code is below
% This function calls the functions below and displays their results.
% You don't need to edit it.
function edgedetector()
img = double(rgb2gray(imread('buoys.jpg')));
edges = DetectVerticalEdges(img);
blurred_edges = BoxBlur(edges);
figure('Name','Original Image')
imshow(img, []);
figure('Name','Edges')
imshow(edges, []);
figure('Name','Blurred Edges')
imshow(blurred_edges, []);
end
% Returns a matrix containing the horizontal component of the gradient at every
% image location.
function edges = DetectVerticalEdges(img)
% MATLAB images use matrix indices, so the order is (y,x), and the +y
% direction is downward.
width = size(img, );
height = size(img, );
[m,n]=size(img);
A=img(:,:n-);
B=img(:,:n);
edgevalue=B-A;
C=img(:,);
edgevalue=[C edgevalue];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%% Your part (a) code here. You can accomplish part (a) with
%%%%%%%%%% nested "for" loops, but an easier way is to use matrix
%%%%%%%%%% indexing to make a matrix of the "left" pixels and a matrix
%%%%%%%%%% of the "right" pixels, and subtract the two matrices.
%%%%%%%%%% REMEMBER: left/right position is the SECOND index in MATLAB.
edges = zeros(height, width-);
edges=edgevalue
%%%%%%%%%% End of your part (a) code.
end
% Applies a box blur to the input image and returns the result.
function blurred = BoxBlur(img)
img = double(img);
height = size(img, );
width = size(img, );
n=; % width of the blur
blurred = zeros(height-(n-),width-(n-));
% Loop through each pixel location in the result
for y=:height-(n-)
for x=:width-(n-)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Your part (b) code here. Calculate blurred(y,x).
blurred=blurred+img(y,x);
%%%%%%% End of part (b) code
end
end
% Usually we'll divide at the end so that we don't make the image
% brighter:
blurred = blurred / n^;
end
4 Transformation Matrices
Note: In Problem 3 above, you had to handle the fact that MATLAB images don’t use Cartesian coordinates.You don’t need to think about that detail for this problem; just use standard Cartesian coordinates.
(a) Recall that transformation matrices can be used to rotate, scale, and/or skew vectors. For example,
the rotation matrix
A=2√22√2−2√22√2(4.1) (4.1) A = 2 2 − 2 2 2 2 2 2
will rotate a vector 45 degrees counterclockwise (CCW). Points may be thought of as vectors from the origin, so we could equivalently say that it will rotate a point by 45 degrees CCW about the origin. For a vector
x0=x0y0(4.2) (4.2) x 0 = x 0 y 0
please describe (in words) the effect of the transformation
X1=AAX0(4.3) (4.3) X 1 = A A X 0
In other words, how does X1 relate to X0? Hint: when considering a series of matrix transformations, it is best to think of them as being applied one after the other, moving from right to left.
answer for a
X1 X 1 表示将 X0 X 0 旋转90°。
(b)
Continue to use X0 and A as defined above. Design a series of matrix multiplications which will first scale a vector X0 by 1.0 in the x direction and 0.5 in the y direction, and then rotate the result by 45 degrees counterclockwise, to produce a vector which we will call X2. Give a formula for X2 in terms of X0, the matrix A from above, and any other necessary matrices which you define.
Answer for b
1).首先将 X0 X 0 在 x x 方向上缩放一倍,通过构造的矩阵A1A1实现:
X′=A1X0(4.1) (4.1) X ′ = A 1 X 0
其中
A1=2001(4.2) (4.2) A 1 = 2 0 0 1
2).然后将变换后的矩阵在 y y 方向上缩放0.5倍,通过构造矩阵A2A2实现:
X1=A2X′=A2A1X0(4.3) (4.3) X 1 = A 2 X ′ = A 2 A 1 X 0
其中
A2=10012(4.4) (4.4) A 2 = 1 0 0 1 2
3).接下来构造一个矩阵 A3 A 3 ,实现将矩阵逆时针旋转90°:
X2=A3X1=A3A2A1X0(4.5) (4.5) X 2 = A 3 X 1 = A 3 A 2 A 1 X 0
其中
A3=2√2−2√22√22√2(4.6) (4.6) A 3 = 2 2 2 2 − 2 2 2 2
(c) Use matrix multiplication to produce a single 2x2 matrix T which will accomplish the rotating-andscaling transformation from (b) above. In other words, give a 2x2 matrix T such that X2 = T X0, with X2 as defined above. You may use Matlab to multiply matrices. The MATLAB script imageTransform.m was provided with the homework. Open it and add your transformation matrix where the comments indicate. Run the script to see MATLAB apply your transformation matrix to every pixel in the image. Include the transformed image in your report, as well as your matrix T. If the output is not what you expect, double-check your work from (b) and above.
未完待续。。。