MATLAB Answers

how to threshold image in 3*3 window?

17 views (last 30 days)
majid
majid on 26 Sep 2021 at 13:29
Commented: Image Analyst on 26 Sep 2021 at 15:47
Hello,
I have a 256x256 noisy image and I want to find threshold value of elements on the middle row and column in 3x3 window and then sliding this window all over the image pixel by pixel and do the same.
I do this work by 2 way : graythresh and one file exchange, but I get in both an error! can any one help me to solve this?
here is my code :
clear all;
close all;
clc;
I=im2double(imread ('cameraman.tif'));
size_I=size(I);
%% adding noises%speckle noise
SI=imnoise(I,'speckle',0.2);
% adding salt and pepper noise or shot noise
Shot_noise = imnoise(I, "salt & pepper", 0.10);
% total noise
noisy_image=I+SI+Shot_noise;
subplot(121)
imshow(I);
title('Original Image')
subplot(122)
imshow(noisy_image)
title("noisy image");
%----------------------------------------------------------------
k=1;
hwin=2*k+1;
im= padarray(noisy_image, [k k],'replicate'); % 'zero-padding'
[row,col] = size(im);
% + Mask
plus=zeros(hwin,hwin);
plus(:,2)=1; plus(2,:)=1;
plus_find=find(plus); % plus pixel's positions
for p=1:row-2
for q=1:col-2
w=im(p:p+(hwin-1),q:q+(hwin-1));%sliding window / filter
sw=w(:)'; %sliding window elements
plus_val=w(plus_find)';
center_val=w(5);
% T=graythresh(sw);
% I have error here!
% [~,T]=imthresh(w);
% and also here
% my code is not complete.
end
end
when I use graythresh it returns this error
and for imthresh function it returns this error :

Answers (1)

Image Analyst
Image Analyst on 26 Sep 2021 at 15:24
Edited: Image Analyst on 26 Sep 2021 at 15:26
Try beginning at row and columns 2,
for p= (k+1) : row-k
for q = (k+1) : col-k
  2 Comments
Image Analyst
Image Analyst on 26 Sep 2021 at 15:47
OK, I just ran your original code and it ran without any errors.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!