问题
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('logo.png')
kernel = np.ones((5, 5), np.float32) / 25
dst = cv2.filter2D(img, -1, kernel)
plt.subplot(121), plt.imshow(img), plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(dst), plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
I was trying smoothing a picture and i didnt understand the ddepth parameter of cv2.filter2d() where the value is -1. So what does -1 do and also what does ddpeth mean ?
回答1:
ddepth
ddepth
means desired depth of the destination image
It has information about what kinds of data stored in an image, and that can be unsigned char (CV_8U
), signed char (CV_8S
), unsigned short (CV_16U
), and so on...
type
As for type, the type has information combined from 2 values:
image depth + the number of channels.
It can be for example CV_8UC1
(which is equal to CV_8U
), CV_8UC2
, CV_8UC3
, CV_8SC1
(which is equal to CV_8S
) etc.
Further Readings
For more discussion, it can be found in the following two articles
- image type vs image depth
- Matrix depth equals 0
- Detailed - Fixed Pixel Types. Limited Use of Templates
回答2:
You can see in the doc that ddepth
stands for "Destination depth", which is the depth of result (destination) image.
If you use -1
, the result (destination) image will have the same depth as the input (source) image.
回答3:
Basically there are five methods I know to blur images:
1) use gamma Method
2) create your own kernal (kernal: it is nothing but a numpy array of ones of desired shape) and apply it on images
3) use built_in function of OpenCv
blur_img = cv2.blur(Image_src,Kernal_size)
4) gaussian Blur
Guassian_blur_img=cv2.GuassianBlur(img_src,kernel_size,sigma_value)
5) median Blur
Median_blur_img=cv2.medianBlu(img_src,kernel_size_value)
I personally prefer to use Median blur as it smartly remove the noise from your image such only backgroung of image will only get blurred and other features of images is at is in image like corner will be unchanged.
来源:https://stackoverflow.com/questions/43392956/explanation-for-ddepth-parameter-in-cv2-filter2d-opencv