threshold multiple values at the same time

Hi am I trying to find the value of a global threshold (mean, median, 50, 100, 150, 200)
Can someone assist me asap please?
TIA!

댓글 수: 3

Please explain your question more.
Matpar
Matpar 2019년 8월 6일
편집: Matpar 2019년 8월 6일
I am trying to do a global threshold for the given values 50, 100, 150, 200
all at once and have matlab project all images respectfully!!
I do not know how to start the syntax hence the reason for me asking!!!
Help if you can please!!!
What does it mean to do a global threshold for the given values 50, 100, 150, 200 ?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 6일

0 개 추천

nd = ndims(YourImageArray);
Thresholded_arrays = bsxfun(@le, YourImageArray, reshape([50, 100, 150, 200], [ones(1,nd), 4]) );
Now Thresholded_arrays is one higher dimension than your original array. For example if your original array was 512 x 768 x 3 x 128 (that is, a stack of 128 RGB images each of which is 512 x 768 pixels), then Thresholded_arrays would be 512 x 768 x 3 x 128 x 4, and Thresholded_arrays(:,:,:,:,1) would be YourImageArray thresholded at value 50.

댓글 수: 15

Matpar
Matpar 2019년 8월 6일
I am a beginner and this is frustrating What about a simple code like I=imread(image) B=[50,100,150,200] Threshold =image>B
I am not sure what to do to get the images Print as titled 50,100,150,200
Please assist! I don't get your answer
B = [50,100,150,200];
nB = length(B);
I = imread(image);
thresholded_I_binary = bsxfun(@le, I, reshape(B, [1 1 1 nB]));
thresholded_I_image = im2uint8(thresholded_I_binary);
for K = 1 : nB
subplot(1, nB, K);
imshow(thresholded_I_image(:,:,:,K));
title(sprintf('threshold = %f', B(K)));
end
Note that you will get colored outputs if the original image is RGB.
Your problem would be easier if you were to state what kind of image (grayscale, pseudocolor, RGB, RGBA, CMYK, grayscale+A) that you are reading in, and if you were to state more clearly what result you expect to display, especially if the input is not grayscale. You would probably also be less frustrated with trying to understand the code if you were to relax the requirement that all of the threshold have to be calculated "at the same time": this is something that would be clearer written as a loop instead of calculating everything "at the same time".
Matpar
Matpar 2019년 8월 6일
편집: Matpar 2019년 8월 6일
Hi walter, my apologies,
I am working with a grayscale image! Would it be to much to ask you to comment on each line so that i can learn what it does
Matpar
Matpar 2019년 8월 6일
Hi Walter the code did not work I tried it! can it be a little easy?
B = [50,100,150,200];
nB = length(B);
for K = 1 : nB
subplot(1, nB, K);
imshow(I <= B(K));
title(sprintf('threshold = %f', B(K)));
end
Matpar
Matpar 2019년 8월 6일
ok let me try now thanks in advance
Hi Walter,
This my code I am trying to get that threshold running inside of here
i=imread('car.jpg');
gr=rgb2gray(i);
figure,imshow(gr), title("Origina image");
[count,bins]=imhist(gr);
count(100);
imhist(gr);
C=[150,100,20,90,75];
T = [50,100,150,200];
for cadd=1:length(C)
newim=adcon(gr,C(cadd));
% subplot(2,3,c),imshow(RImage), title(sprintf("%c constant",c));
subplot(2,3,cadd),imshow(newim),title(sprintf("%C Constant",C));
drawnow;
end
function newim = adcon(gr, Constant)
[r, c]=size(gr);
newim=gr;
for i=1:r
for j=1:c
newim(i,j)=gr(i,j)+Constant;
if newim(i,j)>255
newim(i,j)=255;
else if newim(i,j)<0
newim(i,J)=0;
end
end
end
end
end
Your input, gr, is uint8. When you add a constant to uint8, it is not possible for the resulting value to be > 255 and it is not possible for the resulting value to be < 0
>> uint8(7)+(-10)
ans =
uint8
0
>> uint8(250)+10
ans =
uint8
255
Matpar
Matpar 2019년 8월 7일
Hi Walter, a huge thank you for responding ! But how do i understand what your're saying clearer! I don't get it! Can you break it down further for a beginner so that i never forget please?
Replace your existing adcon function with:
function newim = adcon(gr, Constant)
newim = gr + Constant;
Replace
subplot(2,3,cadd),imshow(newim),title(sprintf("%C Constant",C));
with
subplot(2,3,cadd),imshow(newim),title(sprintf("%C Constant",C(cadd)));
Matpar
Matpar 2019년 8월 7일
편집: Matpar 2019년 8월 7일
Ok i will try it! But there are no comments to understand what you mentioned about the 8bit grayscale image I would like to get that understand as well please! Thanks in advance Walter I appreciate it loads!!
Matpar
Matpar 2019년 8월 7일
Hi Walter this made no difference to me!! can you tell what i should be looking for in reference to the changes that should take place?
The images presented themselves as they were before!!!
Is this in reference to the 8bit features which you highlighted some comments ago?
Thanks for guiding me you have done a great job thus far!!
Matpar
Matpar 2019년 8월 7일
Hi Walter,
I tried and tried it did work Walter i am so greatful!!!! yippy!!!
all the lables are now on the image I have been trying to get that to work for the longest while and now realise the alterations.
Ah the next thing is I am trying to work out adpative, global/local thresholding with the same constant!! can you help me please?
and thanks for all your assistance if you didn't help me I would not have gotten this far!!!
I am so happy walter just to figure it out!!
Adaptive thresholding and local thresholding do not use constants. The code I showed is already for global thresholding.
"If you convert a number that is larger than the maximum value of an integer data type to that type, MATLAB sets it to the maximum value. Similarly, if you convert a number that is smaller than the minimum value of the integer data type, MATLAB sets it to the minimum value."
The calculation A+B in uint8 is like
uint8( max(min(double(A)+double(B),255), 0) )
There is no need to go through and test whether A+B is in the range 0 to 255, because that kind of limit is already built-in to the way that uint8() is defined to operate.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2019년 8월 6일

댓글:

2019년 8월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by