Problem with binary image based from a certain pixel's intensity

1 view (last 30 days)
Hello,
I have a problem regarding getting my pixel's intensity.
I have a binary image as a result of my segmentation algorithm (region growing) and I get a binary image (Img1). But, the problem is that sometimes I get inverted binary image (black becomes white and viceversa). In order to overcome this problem I wrote a little code where I want to overcome this since I want that my segmented object in binary image always be white(1) and background black(0):
Img1 - this is my binary image from my segmentation algorithm
iV1=1;iV0=0; % I used this later on
roundX=round(X); %%X and Y are my pixels coordinate (one pixel)
roundY=round(Y); %%with round I wanted to round up the coordinates number, so that they do not have any decimals
intensityValue1 = ~Img1(roundX,roundY); %& so I used this to get it's intensity value (1 or 0). But first problem that ocured in that I do not get the right intensity value from that pixels coordinates so I used '~' to correct that. Is that ok?
My output binary image displays either white objects (1) and black background (0) (like I wanted), or black object and white background (which I do not want). How can I bypass this problem with if, else if or then functions in order to always get binary image with white objects and black background?
I tried but I could not get the desired results. Here is my code that I wrote by I'm not getting what I want:
if intensityValue1==iV0;
Img2=imfill(Img1,'holes');
else Img2=imcomplement(Img1);
end
figure,imshow(Img2);
intensityValue2 = ~Img2(roundX,roundY);
if intensityValue2==iV0;
Img3=imcomplement(Img2);
else Img3=(Img2);
end
figure,imshow(Img3);
Is there maybe a more simple solution?
Thanks in advance!
  1 Comment
Image Analyst
Image Analyst on 11 Mar 2015
Upload the original binary image. Then upload the one that you would like to have. If you think it will help, upload the original rgb or gray scale image and your region growing code.

Sign in to comment.

Answers (1)

Mario
Mario on 11 Mar 2015
I managed to find the solution. It was in the X Y coordinates. The correct code is below:
iV1=1;iV0=0;
roundX=round(Y);
roundY=round(X); %%By switching theese coordinates if fixes the problem that I had in my code.
intensityValue1 = Img1(roundX,roundY);
if intensityValue1==iV0;
Img2=imfill(Img1,'holes');
else Img2=imcomplement(Img1);
end
% figure,imshow(Img2);
intensityValue2 = Img2(roundX,roundY);
if intensityValue2==iV0;
Img3=imcomplement(Img2);
else Img3=(Img2);
end
imshow(Img3);
  2 Comments
Image Analyst
Image Analyst on 11 Mar 2015
Images indexes are (row, column), not (x,y). So you should use img1(roundY, roundX), not img(roundX, roundY),
Mario
Mario on 12 Mar 2015
That is correct. I just switched the image indexes instead (row, column). Thanks for clarification.

Sign in to comment.

Categories

Find more on Modify Image Colors in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!