代码拉取完成,页面将自动刷新
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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。