thresholding for multiple values

Hi all i have my thresholding code here and i am trying to do a thresholding processing for multiple types of thresholding in the same go!!!
I am new and i am not sure if this is possible but can a professional assist me where I am going wrong and add comments so that I get the understanding of it!!
I am getting loads of errors and I am not sure where to beging as a beginner!!
Thanks in advance for you consideration and assistance!!!
*********** My Code *******************************
b=imread("bird.jpg");
Gbird=rgb2gray(sc);
t=mean(Gbird(:));
t1=meadian(Gbird(:));
t2=[50,100,150,200];
[r, c]=size(Gbird);
Msc=zeros(r,c);
for i=1:r
for j=1:c
if Gbird(i,j)>t,t1;t2;
Msc(i,j)=1;
end
end
end
imshow(Msc), figure;
imshow(Gbird), figure;
imshow(sc), figure;
subplot(1,3,1), imshow(sc), title('Binary Image Of The Car');
subplot(1,3,2), imshow(Gbird), title('Grayscalee Image Of The Car');
subplot(1,3,3), imshow(Msc), title('Original Image Of The Car');

Answers (2)

t1=meadian(Gbird(:));
median is misspelled...
What do you want/expect as a result? Logicals don't work that way; you'll have to test for each level.
As written, even if the logical test worked, you would only have a '1' in the Msc array for all locations which had a value greater than the least of those in your list of various t* -- not a way to locate which would be greater than each threshold. To do that you would need to be able to store a logical plane for each of the levels--6 total so would need a r x c x 6 3D array.
The way to do something like this in ML would be to loop over the number of levels and do the logical test as vector operation. Something otoo--
...
thresh=[mean(Gbird(:));median(Gbird(:));[50:50:200].']; % the threshold levels desired
nTh=numel(thresh);
Msc=false([size(Gbird) nTh]);
for i=1:nTh
Msc(:,:,i)=(Gbird>thresh(i));
end

5 Comments

%% Please Comment On This I Got The Top Bits but i am not sure where to implement this!!
Msc=false([size(Gbird) nTh]);
for i=1:nTh
Msc(:,:,i)=(Gbird>thresh(i));
end
%% Am I doing The Subplot right?
subplot(3,3,median), imshow(Ms(:,:,i);
subplot(3,3,mean), imshow(Ms(:,:,i);
subplot(3,3,median), imshow(Ms(:,:,i);
Matpar
Matpar on 7 Aug 2019
Edited: Matpar on 7 Aug 2019
I don't think this is it let me post the question I am trying to figure out!!
********question***************
Write a MATLAB code that replaces each pixel value in a grey-scale
image by adding to it a given constant value c={10,20,50,100}. If the resulting value is >255 then replace it with 255
  • Find out how many pixels of value 100 are there in the image % I figured This One Out
I am now trying to find out this one it's a bit tricky for me!!! Please comment so that i can understand it!
  • Finding the value of a global threshold (mean, median, 50, 100, 150, 200)
Thank You In Advance for acknowledging my digital presence!!!
The threshold is there but I am not sure how to conjure up the code, that's what's causing my confusion!! T = [10,20,50,100];
The Code Thus Far**************************
img = imread("car.jpg");
gimg = rgb2gray(img); % convert to grayscale image
figure,imshow(gimg); % To display the original image
title("Original Image") % To display a title on top of the image
C = [50, 100, 150, 200];
T = [10,20,50,100];
% Counting the amount of pixels of value 100
% Bins store the numbers of pixel values
% Count displays the amount of values in the image of that number
[count,bins] = imhist(gimg);
disp("The Number of 100 pixel values are:")
count(100)
for c = 1:length(C)
RImage = ImAcon(gimg, C(c)); % The addition function which returns the added image
%figure,imshow(Returned_Image),title('Returned Image'); % To display the returned image
subplot(2,3,c),imshow(RImage), title(sprintf("%i constant",C(c)));
drawnow;
end
% Creating The Function
function NewImage = ImAcon(Image, Constant)
[rows, cols] = size(Image); % Get the size/dimensions of the image
NewImage = Image; % Create a New Image - to store the resultant operation on
% Going through the image pixels and add the constant to them
for i = 1:rows% Start from All Pixel from Row 1
for j=1:cols% Start from All Pixel from Column 1
NewImage(i,j) = Image(i,j) + Constant; % The Constant Addition Operation
if (NewImage(i,j) > 255)% Ensureing Values Greater Than 255 are set at threshold 255
NewImage(i,j) = 255;
else
if (NewImage(i,j) < 0)% Ensureing Values lesser Than 255 are set at threshold 0
NewImage(i,j) = 0;
end
end
end
end
end% End the Functions Process
Indeed, that's something different than what your initial code appeared to be trying to do...but, I'm not sure what
  • Finding the value of a global threshold (mean, median, 50, 100, 150, 200)
means, exactly. "Finding" indicates they're going to illustrate something; that isn't a question and the value of the mean and/or median is simple evnough; I'm not sure what is intended by to find the "value of" a threshold when a value is given.
Surely there's some more context to the question...
what i gathered from it is that they would like to to see all three!!!
please assist me with the code for the threshold equal to the values
t= 50, 100, 150, 200
the syntax is a bit challenging for me
I am trying to get an image to show for all of the values in the threshold
and use one subplot syntax for all the values rather than type them all out!!
hope that helps really need this to be solve
I don't understand what it means "for the threshold equal to the values", sorry.
Where did this come from? Can you show a link to the original?

Sign in to comment.

Matpar
Matpar on 7 Aug 2019
Screenshot 2019-08-07 at 17.38.04.png

1 Comment

This is how far i have gotten but it seems like i am more confused than you are dpb!
b=imread("bird.jpg");
Gbird=rgb2gray(b);
whos
t=100;150;200
[r, c]=size(Gbird);
Msc=zeros(r,c);
for i=1:r
for j=1:c
if Gbird(i,j)>t
Msc(i,j)=1;
end
end
end
imshow(Msc), figure;
imshow(Gbird), figure;
imshow(b), figure;
subplot(1,3,1), imshow(b), title('Binary Image Of The Car');
subplot(1,3,2), imshow(Gbird), title('Grayscalee Image Of The Car');
subplot(1,3,3), imshow(Msc), title('Original Image Of The Car');

Sign in to comment.

Asked:

on 7 Aug 2019

Edited:

on 7 Aug 2019

Community Treasure Hunt

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

Start Hunting!