-4 python python-2.7
我正在尝试完成这两个功能,但我不知道从哪里开始,如果有人可以告诉我该怎么做?
def padReflectBorder(image, N):
"""This function pads the borders of the input image by reflecting the
image across the boundaries.
N is the number of rows or columns that should be added at each border;
i.e., the output size should have 2N more rows and 2N more columns than
the input image.
The values in the input image should be copied to fill the middle of the
larger array, and the borders should be filled by reflecting the array
contents as described in the documentation for cv2.copyMakeBorder().
This function should be equivalent to the library call:
cv2.copyMakeBorder(image, N, N, N, N, borderType=cv2.BORDER_REFLECT_101)
Note: BORDER_REFLECT_101 means that the values in the image array are
reflected across the border. Ex. gfedcb|abcdefgh|gfedcba
NOTE: You MAY NOT use any calls to numpy or opencv library functions, but
you MAY use array broadcasting and "advanced" numpy indexing
techniques for this function.
Parameters
----------
image : numpy.ndarray(dtype=np.uint8)
A grayscale image represented in a numpy array.
N : int
An integer strictly greater than zero and less than the smallest
dimension of the input image representing the number of padding pixels
to add at each border.
Returns
-------
numpy.ndarray(dtype=np.uint8)
A copy of the input array with 2N additional rows and columns filled
with the values of the input image reflected over the borders.
"""
def crossCorrelation2D(image, kernel):
"""This function uses native Python code & loops to compute and return the
valid region of the cross correlation of an input kernel applied to each
pixel of the input array.
NOTE: Lectures 2-05, 2-06, and 2-07 address this concept.
Recall that for an image F and kernel h, cross correlation is defined as:
G(i,j) = sum_u=-k..k sum_v=-k..k h[u,v] F[i+u,j+v]
For N = kernel.shape[0] // 2, this function should be equivalent to:
cv2.filter2D(image, cv2.CV_64F, kernel)[N:-N, N:-N]
See http://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html#filter2d
for details.
Your code must operate on each pixel of the image and kernel individually
for each step of the computation. (We know this is inefficient, but we want
to make sure that you understand what is really happening within the more
efficient library functions that are available.)
NOTE: You MAY NOT use any numpy, scipy, or opencv library functions,
broadcasting rules, or "advanced" numpy indexing techniques, nor may
you use the operator functions or convolution functions listed in the
note at the top. You MUST manually loop through the image at each
pixel. (Yes, we know this is slow and inefficient.)
NOTE: You MAY assume that kernel will always be a square array with an odd
number of elements.
Parameters
----------
image : numpy.ndarray(dtype=np.uint8)
A grayscale image represented in a numpy array.
kernel : numpy.ndarray
A kernel represented in a numpy array of size (k, k) where k is an odd
number strictly greater than zero.
Returns
-------
output : numpy.ndarray(dtype=np.float64)
The output image. The size of the output array should be smaller than
the original image size by k-1 rows and k-1 columns, where k is the
size of the kernel.
"""
Run Code Online (Sandbox Code Playgroud)
小智 9
这看起来像是家庭作业.事实上,我知道它是,因为我是编写它的TA.
看起来您错过了crossCorrelation2D函数上文档字符串的引用部分,其中显示"请参阅讲座2-05,2-06和2-07".从那里开始可能是一个好主意.这应该有助于您了解每个功能的目的,并帮助您准备完成任务.
对于这两个函数,编写一些读取图像并调用文档字符串中提到的库函数的代码会很有帮助.由于函数应该等同于那些库调用,cv2.copyMakeBorder和cv2.filter2D函数的输出应该说明您期望生成的内容.
完成这些步骤后,您应该能够自己编写函数.您可以编写一些单元测试来检查代码,并提交给评分服务器以获取有关您工作的反馈.
如果你仍然遇到麻烦,你可以在我们的课程论坛和闲谈频道中向同学寻求帮助,或者看看我和其他助教.
祝你好运!
| 归档时间: |
|
| 查看次数: |
912 次 |
| 最近记录: |