Matlab-如何对具有多个阈值的灰度图像进行二值化?

问题描述

我正在尝试将灰度图像转换为具有两个阈值的二进制图像:

  • img =灰度图像
  • b =输出二进制图像
  • 如果(img> t1)或(img
  • 否则b = 0
t1 = 200;
t2 = 100;
src = imread('an rgb image');
img = reg2gray(src);
b1 = imbinarize(img,t1);
b2 = imbinarize(img,-t2);
b = imadd(b1,b2);

,但是此代码无效。是否可以同时设置多个阈值?

解决方法

使用逻辑矩阵。

b=(img>t1) | (img<t2);
,

可以将条件语句应用于数组。条件为真时,将初始化数组的值设置为1,并将数组的其余单元格设置为0。

Binarize Image

RGB_Image = imread("RGB_Image.png");
Grayscale_Image = rgb2gray(RGB_Image);

imshow(Grayscale_Image);
Threshold_1 = 220;
Threshold_2 = 100;

Binary_Image = ((Grayscale_Image > Threshold_1) | (Grayscale_Image < Threshold_2));

subplot(1,2,1); imshow(RGB_Image);
title("RGB Image");
subplot(1,2); imshow(Binary_Image);
title("Binary Image");