Smoothing images------linear filter
smoothing, also called blurring, is a simple and frequently used image processing operation.
normalized box filter 归一化滤波
-
This filter is the simplest of all! Each output pixel is the mean of its kernel neighbors ( all of them contribute with equal weights)
-
The kernel is below:
-
Probably the most useful filter (although not the fastest). Gaussian filtering is done by convolving each point in the input array with a Gaussian kernel and then summing them all to produce the output array.
-
Just to make the picture clearer, remember how a 1D Gaussian kernel look like?
Assuming that an image is 1D, you can notice that the pixel located in the middle would have the biggest weight. The weight of its neighbors decreases as the spatial distance between them and the center pixel increases.
Note
Remember that a 2D Gaussian can be represented as :
where is the mean (the peak) and
represents the variance (per each of the variables
and
)
The median filter run through each element of the signal (in this case the image) and replace each pixel with the median of its neighboring pixels (located in a square neighborhood around the evaluated pixel).
Eroding and Dilating
Goal:In this tutorial you will learn how to:
Apply two very common morphology operators:Dilation and Erosion.for this purpose,you will use the following Opencv functions:
Erode腐蚀
dilate膨胀
morphological
生词本Morphological Operations
-
In short: A set of operations that process images based on shapes. Morphological operations apply astructuring element to an input image and generate an output image.
-
The most basic morphological operations are two: Erosion and Dilation. They have a wide array of uses, i.e. :
- Removing noise
- Isolation of individual elements and joiningdisparate elements in an image.
- Finding of intensity bumps or holes in an image寻找图像中的明显的极大值区域或极小值区域。
-
We will explain dilation and erosion briefly, using the following image as an example:
dilation
生词本
anchor point
生词本analogously
生词本Erosion
-
This operation is the sister of dilation. What this does is to compute a local minimum over the area of the kernel.
-
As the kernel
is scanned over the image, we compute the minimal pixel value overlapped by
and replace the image pixel under the anchor point with that minimal value.
-
Analagously to the example for dilation, we can apply the erosion operator to the original image (shown above). You can see in the result below that the bright areas of the image (the background, apparently), get thinner, whereas the dark zones (the “writing”( gets bigger.
Morphological Gradient 形态梯度
top hat高帽 black hat黑帽
effectuate
生词本getStructuringElement
Returns a structuring element of the specified size and shape for morphological operations.
-
C++:
Mat
getStructuringElement
(int
shape, Size
ksize, Point
anchor=Point(-1,-1)
)
-
Python:
cv2.
getStructuringElement
(shape, ksize
[, anchor
]
) → retval
-
C:
IplConvKernel*
cvCreateStructuringElementEx
(int
cols, int
rows, int
anchorX, int
anchorY, int
shape, int*
values=NULL
)
-
Python:
cv.
CreateStructuringElementEx
(cols, rows, anchorX, anchorY, shape, values=None
) → kernel
-
Parameters: - shape –
Element shape that could be one of the following:
- MORPH_RECT - a rectangular structuring element:
- MORPH_ELLIPSE - an elliptic structuring element, that is, a filled ellipse inscribed into the rectangle Rect(0,0, esize.width, 0.esize.height)
- MORPH_CROSS - a cross-shaped structuring element:
- CV_SHAPE_CUSTOM - custom structuring element (OpenCV 1.x API)
- MORPH_RECT - a rectangular structuring element:
- ksize – Size of the structuring element.
- cols – Width of the structuring element
- rows – Height of the structuring element
- anchor – Anchor position within the element. The default value
means that the anchor is at the center. Note that only the shape of a cross-shaped element depends on the anchor position. In other cases the anchor just regulates how much the result of the morphological operation is shifted.
- anchorX – x-coordinate of the anchor
- anchorY – y-coordinate of the anchor
- values – integer array of cols``*``rows elements that specifies the custom shape of the structuring element, whenshape=CV_SHAPE_CUSTOM.
- shape –
The function constructs and returns the structuring element that can be further passed to createMorphologyFilter(), erode(), dilate()or morphologyEx() . But you can also construct an arbitrary binary mask yourself and use it as the structuring element.
Note
When using OpenCV 1.x C API, the created structuring element IplConvKernel* element must be released in the end usingcvReleaseStructuringElement(&element).
createTrackbar
Creates a trackbar and attaches it to the specified window.
-
C++:
int
createTrackbar
(const string&
trackbarname, const string&
winname, int*
value, int
count, TrackbarCallback
onChange=0, void*
userdata=0
)
-
C:
int
cvCreateTrackbar
(const char*
trackbarName, const char*
windowName, int*
value, int
count, CvTrackbarCallback
onChange
)
-
Python:
cv.
CreateTrackbar
(trackbarName, windowName, value, count, onChange
) → None
-
Parameters: - trackbarname – Name of the created trackbar.
- winname – Name of the window that will be used as a parent of the created trackbar.
- value – Optional pointer to an integer variable whose value reflects the position of the slider. Upon creation, the slider position is defined by this variable.
- count – Maximal position of the slider. The minimal position is always 0.
- onChange – Pointer to the function to be called every time the slider changes position. This function should be prototyped as void Foo(int,void*); , where the first parameter is the trackbar position and the second parameter is the user data (see the next parameter). If the callback is the NULL pointer, no callbacks are called, but only valueis updated.
- userdata – User data that is passed as is to the callback. It can be used to handle trackbar events without using global variables.
The function createTrackbar creates a trackbar (a slider or range control) with the specified name and range, assigns a variable value to be a position syncronized with the trackbar and specifies the callback function onChange to be called on the trackbar position change. The created trackbar is displayed in the specified window winname.
Note
[Qt Backend Only] winname can be empty (or NULL) if the trackbar should be attached to the control panel.
Clicking the label of each trackbar enables editing the trackbar values manually.
upsampling 采样
- 降低采样;
- 降低取样;
- 缩小取样
Image Pyramids
Use the OpenCV functions pyrUp and pyrDown to downsample or upsample a given image.如何使用OpenCV函数 pyrUp 和 pyrDown 对图像进行向上和向下采样。
Although there is a geometric transformation function in OpenCV that -literally- resize an image
尽管OpenCV 几何变换 部分提供了一个真正意义上的图像缩放函数
An image pyramid is a collection of images - all arising from a single original image - that are successively downsampled until some desired stopping point is reached.
一个图像金字塔是一系列图像的集合 - 所有图像来源于同一张原始图像 - 通过梯次向下采样获得,直到达到某个终止条件才停止采样。
Used to reconstruct an upsampled image from an image lower in the pyramid (with less resolution)
用来从金字塔低层图像重建上层未采样图像
Imagine the pyramid as a set of layers in which the higher the layer, the smaller the size.
想想金字塔为一层一层的图像,层级越高,图像越小。