总言
MATLAB最近总是出现小BUG,决定自己采用不同的方法来满足数字图像处理的一些要求。综合考虑选择了MWORKS软件进行相关数字图像处理代码编写。纯经验分享,纯学习分享。
1、图像二值化
using TyImages
using TyPlot
function Binarize()
I = imread("C:/Users/16120/Desktop/lena.png")//路径替换为自己的
I_gray = rgb2gray(I)
level = graythresh(I_gray) * 255
BW = I_gray .> level
subplot(1, 2, 1)
imshow(I)
title("Original Image")
subplot(1, 2, 2)
imshow(BW)
title("Binary Image")
end
Binarize()
2、基于灰度直方图峰谷阈值的图像二值化
using TyImages
using TyImageProcessing
using TyPlot
using StatsBase
function binarize_image(image_path::String)
img = TyImages.imread(image_path)
gray_img = TyImages.rgb2gray(img)
subplot(1, 3, 1)
TyImages.imshow(gray_img)
title("Original Image")
hist_values, bins = TyImages.imhist(gray_img)
subplot(1, 3, 2)
stem(bins, hist_values)
title("Gray Histogram")
level = graythresh(gray_img)
println("Calculated threshold level: ", level)
threshold = level * 255 # 将阈值从[0, 1]转换到[0, 255]
binary_img = imbinarize(gray_img, level)
subplot(1, 3, 3)
TyImages.imshow(binary_img)
title("Binary Image")
end
binarize_image("C:/Users/16120/Desktop/lena.png")//路径替换为自己的
---------------------------------------------------分界线---------------------------------------------------
目前只做了上述两个实验,有需要的小伙伴可以后台私信我,后续考虑做更多的MWORKS软件学习分享。