11、OpenCV图像二值化处理
原创
©著作权归作者所有:来自51CTO博客作者已注销的原创作品,请联系作者获取转载授权,否则将追究法律责任
Python:cv.
Threshold
(src, dst, threshold, maxValue, thresholdType) → None
Parameters:
| - src– input array (single-channel, 8-bit or 32-bit floating point).
- dst– output array of the same size and type as
src . - thresh– threshold value.
- maxval– maximum value to use with the
THRESH_BINARY andTHRESH_BINARY_INV thresholding types. - type– thresholding type (see the details below).
|
#!/usr/bin/python
#-*- coding:utf-8 -*-
import numpy as np
import cv2
# original image (gray scale image)
org_img = cv2.imread('1.jpg', 0)
# preference
THRESHOLD = 127
MAXVALUE = 255
# binarization using opencv
_, bin_cv2 = cv2.threshold(org_img, THRESHOLD, MAXVALUE, cv2.THRESH_BINARY)
'''Parameters:
src – input array (single-channel, 8-bit or 32-bit floating point).
dst – output array of the same size and type as src.
thresh – threshold value.
maxval – maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types.
type – thresholding type (see the details below).
'''
# binarization not using opencv
bin_npy = np.zeros(org_img.shape, org_img.dtype)
bin_npy[np.where(org_img > THRESHOLD)] = MAXVALUE
# check
cv2.imshow('original.png', org_img)
cv2.imshow('binary_cv2.png', bin_cv2)
cv2.imshow('binary_npy.png', bin_npy)
cv2.waitKey(0)