使用 Matlab 进行硬阈值处理

Hard Thresholding with Matlab

我有一个错误

Error using  .* 
Integers can only be combined with integers of the same class, or scalar doubles.

Error in wthresh (line 27)
    y   = x.*(abs(x)>t);

错误信息很清楚。您正在乘以 x 这可能是一个 uint8 和 (abs(x)>t) 这是一个逻辑。乘以整数或转换为双精度时,您需要相同的类型。也看看 this question。将您的代码更改为以下内容:

I = im2double(imread('cameraman.tif')); %Converting pixel values from [0,255] to [0,1]
thr = 0.4;
hard= wthresh(I,'h',thr);
imshow(hard,[]);

或者

I = double(imread('cameraman.tif')); % Just change the data-type but keep values 
                                     % in the range [0,255].
thr = round(0.4*255); %Instead of converting pixel values, change the threshold.
hard= wthresh(I,'h',thr);