Ai
1 Star 5 Fork 1

cubone/learnopencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
otsu_method.py 1.57 KB
一键复制 编辑 原始数据 按行查看 历史
Anastasia 提交于 2020-06-02 20:14 +08:00 . Added Otsu method
import cv2
from matplotlib.ticker import FuncFormatter
from matplotlib import pyplot as plt
from otsu_implementation import otsu_implementation
def call_otsu_threshold(img_title="boat.jpg", is_reduce_noise=False):
# Read the image in a greyscale mode
image = cv2.imread(img_title, 0)
# Apply GaussianBlur to reduce image noise if it is required
if is_reduce_noise:
image = cv2.GaussianBlur(image, (5, 5), 0)
# View initial image histogram
plt.hist(image.ravel(), 256)
plt.xlabel('Colour intensity')
plt.ylabel('Number of pixels')
plt.savefig("image_hist.png")
plt.close()
# Applying Otsu's method setting the flag value into cv.THRESH_OTSU.
# Use bimodal image as an input.
# Optimal threshold value is determined automatically.
otsu_threshold, image_result = cv2.threshold(
image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU,
)
print("Obtained threshold: ", otsu_threshold)
# View the resulting image histogram
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(image_result.ravel(), 256)
ax.set_xlabel('Colour intensity')
ax.set_ylabel('Number of pixels')
# Get rid of 1e7
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: ('%1.1fM') % (x*1e-6)))
plt.savefig("image_hist_result.png")
plt.close()
# Visualize the image after the Otsu's method application
cv2.imshow("Otsu's thresholding result", image_result)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main():
call_otsu_threshold()
otsu_implementation()
if __name__ == "__main__":
main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cubone/learnopencv.git
git@gitee.com:cubone/learnopencv.git
cubone
learnopencv
learnopencv
master

搜索帮助