# alter image pixels intensities, gray scale image

5 views (last 30 days)
M on 30 Aug 2014
Commented: M on 30 Aug 2014
So I was trying to make simple pixel alteration in an image by the following code
CSF = IMG(147,53,24);
BG = IMG(1,1,24);
for i=1:256
for j=1:256
for k=1:50
if(IMG(i,j,k)==CSF)
IMG(i,j,k)=1;
elseif(IMG(i,j,k)==BG)
IMG(i,j,k)=0;
elseif (IMG(i,j,k)==1)
IMG(i,j,k)=CSF;
elseif (IMG(i,j,k)==0)
IMG(i,j,k)=BG;
end
It is suppose to alter pixels with those specific intensities into the given ones. The BG part is working properly, but the CSF part won't alter properly and I don't know why

Image Analyst on 30 Aug 2014
What is "the CSF part"? CSF is mentioned on 3 lines. What is the value of CSF and BG? They're some scalar numbers. It should do the "if" for CSF for at least one voxel, and that is the one at (1,1,24). Have you tried stepping through with the debugger?

M on 30 Aug 2014
CSF is the Cerebrospinal fluid in brain and GM is gray matter in brain, I have clustered images of brains, and I need to extract mask for the CSF by changing their color and comparing it.
I was using 4 "if" to make sure I keep same number of clusters after altering the image.
Image Analyst on 30 Aug 2014
M, I'm trying to see if this can be sped up by vectorizing but it depends on the values of CSF and BG. If they can be 0 or 1, we have a problem, and have to stay with your 3 loops. If they're not, we can vectorize and do this in a faster, more MATLAB-ish way. I don't care what GM is - you didn't even have that in your code, but you still haven't told me what the values of CSF and BG are, even after I've asked twice before (3 times now ). Can you at least tell me if they might ever take on the values of 0 or 1? Just trying to help here but you're not making it easy.
M on 30 Aug 2014
Sorry for the misunderstanding I don't know specifically what value are CSF and BG are, yes they can be 0 or 1. I am running that on many images and every image has its color distribution. I am planning to use a vectorized code as soon as I solve the problem. BUT the problem now is not with the time the 3 loops take, its with why is not that simple code working as it is supposed to