How to display only one image in the following code?

In the following code, the output is displayed as 3 images horizontally. What is the reason and how to display only one image? I=imread('Input.bmp'); figure, imshow(I); count=0; [row,column]=size(I) for i=1: row for j=1:column if I(i,j) > 50 & I(i,j) < 70 count=count+1; I1(i,j) = 255; else I1(i,j) = 0; end end end figure, imshow(I1(:,:,1))

 Accepted Answer

The output is displayed as two images (not three) wherever the operating system decides to put them. This is because you call imshow() twice. Anyway you're doing it wrong (i.e. slow and not vectorized). Plus you probably don't need it to be 255 - you probably want it to be binary (logical) which will make it so much easier to process those pixels. If you make it 255 you'll just have to convert to 0 and 1 sooner or later anyway. Try my code:
binaryImage = (I > 50) & (I < 70);
count = sum(binaryImage(:));
figure;
subplot(1,2,1);
imshow(I);
title('Original Image', 'FontSize', 24);
subplot(1,2,2);
imshow(binaryImage);
title('Binary Image', 'FontSize', 24);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

4 Comments

Thank you for your reply. In my previous code, The binary image was displayed 3 times (positions in this order: x x x on a single row) (and additional original image on a separate window). Please try my code also and give me solution. I am having a brain MRI image of size 200*150 in bmp format. Please send your mail ID so that I send the image for testing. I tried your code and I am getting following error. Please cooment. ??? Error using ==> imageDisplayValidateParams>validateCData at 119 If input is logical (binary), it must be two-dimensional.
Error in ==> imageDisplayValidateParams at 31 common_args.CData = validateCData(common_args.CData,image_type);
Error in ==> imageDisplayParseInputs at 79 common_args = imageDisplayValidateParams(common_args);
Error in ==> imshow at 199 [common_args,specific_args] = ...
Error in ==> MathworkAnswer at 13 imshow(binaryImage);
I did try your code with a standard test image, and it displayed two images, not three. You can attach images here, with the paper clip icon above the edit box.
The image size is 200*150*3 when I tried with matlab but it shows 200*150 using windows standard properties. I used imshow(I(:,:,1) to display red channel and it worked. The image looks like grayscale image. I sent the image as attachment. Please comment on the properties of image.
It's a grayscale image just stored in BMP format which likes to make everything color. Just convert it to gray scale immediately after reading it in.

Sign in to comment.

More Answers (0)

Categories

Find more on Display Image 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!