An image thresholding
Prepare a grayscale image
The value of all the pixels of the processing threshold is usually set a threshold, so the picture of its comparative make a series of operations.
In conventional thresholding opencv has five functions, namely THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV. Next, look at the effect after each of these types of processing pictures
1. THRESH_BINAR
# 127 becomes the pixel value exceeds 255, otherwise 0, brighter bright
ret, threshold1 = cv2.threshold (img, 127,255, cv2.THRESH_BINARY)
2. THRESH_BINARY_INV
# Pixel value exceeds 127 becomes 0 or 255, the light darker
ret, threshold2 = cv2.threshold (img, 127,255, cv2.THRESH_BINARY_INV)
3. THRESH_TRUNC
# Pixel value exceeds 127 becomes 127, otherwise unchanged, to understand the whole picture darken ret, threshold3 = cv2.threshold (img, 127,255, cv2.THRESH_TRUNC)
4. THRESH_TOZERO
# Pixel value exceeds 127 unchanged 0 otherwise, is understood to increase the contrast of the picture ret, threshold4 = cv2.threshold (img, 127,255, cv2.THRESH_TOZERO)
5. THRESH_TOZERO_INV
# Pixel value of more than 127 0, otherwise unchanged ret, threshold5 = cv2.threshold (img, 127,255, cv2.THRESH_TOZERO_INV)
Usage scenarios threshold processing ~ ~ ~ I will write in subsequent blog,
II. Filter
Principle filtering is done by specifying the size of the image correlation matrix operations, linear algebra specific things do not unfold here, if the follow-up work will be thorough, perfect to come back ...
Frequently used three filtering operation, mean filtering, Gaussian filtering, median filtering.
1. Mean Filter (inner product and by taking the average of the image processing unit matrix do)
blur = cv2.blur(img,(3,3))
2. Gaussian (normal distribution processing an image, the closer to the center point, the closer the value)
blur2 = cv2.GaussianBlur(img,(3,3),1)
3. The median filter (all designated size of the matrix element values taken median value processing)
blur3 = cv2.medianBlur(img,5)
This level of splicing observed a few pictures, difficult to find in the scene processing of noise using median filtering effect is most obvious
III. Corrosion and expansion
1. corrosion
img = cv2.imread("test.png") img2 = cv2.erode(img,kernel=numpy.ones((9,9),numpy.uint8),iterations=9) 和单位矩阵做处理,迭代9次,意味这腐蚀的程度 cv2.imshow("IMage",numpy.hstack((img,img2))) cv2.waitKey(0) cv2.destroyAllWindows()
2. 膨胀 (嗯,和腐蚀操作刚好相反)
img2 = cv2.dilate(img,kernel=numpy.ones((9,9),numpy.uint8),iterations=9) cv2.imshow("IMage",numpy.hstack((img,img2))) cv2.waitKey(0) cv2.destroyAllWindows()
3. 梯度运算(膨胀-腐蚀)
4. 礼帽与黑帽
img2 = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel=numpy.ones((5,5),numpy.uint8)) # 就是腐蚀掉的部分
img3 = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel=numpy.ones((5,5),numpy.uint8)) # 原始部分的外壳
结语:这段时间工作实在太忙了,不是评论提醒都快忘记继续写博客了,这部分的内容学过比较久了,今天整理一下方便他人也方便自己。
——2019-10-17 22:42:32