You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to change the Image colour of ones and zeros to white and black
2 views (last 30 days)
Show older comments
The mage has 50 x 100 points (vertical x horizontal dimension). Each point can be zero or one, where zero = white colour and one = black colour.
I tried this
B = randi([0 1],50, 100);
imagesc(B)
The output image is represented with yellow and blue but I want it to be black and white.
Accepted Answer
Image Analyst
on 18 Jun 2021
You don't need a colormap. Just try this:
B = randi([0 1],50, 100);
imshow(B)
impixelinfo();
16 Comments
Akakan-Abasi Okon
on 18 Jun 2021
Thank you, but I want the axes dimensions to show. The 'imagesc(B)' gives me what I want but I want the image to be black and white.
Image Analyst
on 18 Jun 2021
@Akakan-Abasi Okon, imagesc() applies some colormap by default - it's rarely the one you would have picked so that's why I avoid using it, and use imshow() instead. So, imagesc() does not give you what you want, like you said. Look at this code:
B = randi([0, 1], 50, 100); % A double image.
imshow(B, 'InitialMagnification', 1000); % Magnify image since 50x100 is so tiny.
impixelinfo();
axis('on', 'image'); % Turn on axes tick marks and labels.
It produces this image:
Let me know what, if anything you don't like about this black and white image with axis tickmarks and tick labels showing.
By the way, here are some alternative data classes for B that give the same appearance:
% B = 255 * randi([0, 1], 50, 100, 'uint8'); % B is a uint8 integer image.
% B = logical(randi([0, 1], 50, 100)); % B is a logical boolean binary image.
Akakan-Abasi Okon
on 18 Jun 2021
Edited: Akakan-Abasi Okon
on 18 Jun 2021
Please is there a way to divide the image width by n. to get something similar to the attached pic?
The dimension i'm actually working on is 100 x 200, but i want to divide the width (200) by 5.
Image Analyst
on 19 Jun 2021
You can use simple indexing. Like
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 8;
fprintf('Beginning to run %s.m ...\n', mfilename);
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
numTiles = 5;
r = round(linspace(1, rows+1, numTiles+1))
c = round(linspace(1, columns+1, numTiles+1))
for kr = 1 : numTiles
row1 = r(kr);
row2 = r(kr+1) - 1;
for kc = 1 : numTiles
col1 = c(kc);
col2 = c(kc+1) - 1;
subImage = grayImage(row1:row2, col1:col2, :);
imshow(subImage);
caption = sprintf('Rows %d to %d\nColumns %d to %d', row1, row2, col1, col2);
title(caption, 'fontSize', fontSize);
drawnow;
% Delay a fraction of a second so we can see it.
pause(0.33)
end
end
fprintf('Done running %s.m.\n', mfilename);
If you want to paste the extracted subimages onto a canvass with some kind of custom placement like you've shown, then you can paste them on to a larger canvass. See my copy and paste demo.
Akakan-Abasi Okon
on 19 Jun 2021
Thanks for your continous support, but it's a bit unclear or may be I don't understand how to use it. I substituted the 'moon.tif' with the image I wanted to reduce the width dimension shown below;
grayImage = imread('TstDim.jpg');
The output was like rounding it up to several dimensions until it ended. Please see the attached image.
Image Analyst
on 19 Jun 2021
You didn't post TstDim.jpg, but for the uncla.PNG image you attached this works fine. It goes through the 25 tiles in a 5x5 layout and shows you each tile. Each tile has a reduced dimension (size) because it's 1/5 the size of the original, though it's a pixel-for-pixel sub image extracted from it.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
fprintf('Beginning to run %s.m ...\n', mfilename);
rgbImage = imread('uncla.png');
subplot(1, 2, 1);
imshow(rgbImage)
axis('on', 'image');
title('Original Image', 'FontSize', fontSize);
g = gcf;
[rows, columns, numberOfColorChannels] = size(rgbImage)
numTiles = 5;
r = round(linspace(1, rows+1, numTiles+1))
c = round(linspace(1, columns+1, numTiles+1))
% Draw lines over image.
for k = 1 : numTiles
xline(c(k), 'Color', 'r', 'LineWidth', 2);
yline(r(k), 'Color', 'r', 'LineWidth', 2);
end
subplot(1, 2, 2);
loopCounter = 1;
for kr = 1 : numTiles
row1 = r(kr);
row2 = r(kr+1) - 1;
for kc = 1 : numTiles
col1 = c(kc);
col2 = c(kc+1) - 1;
subImage = rgbImage(row1:row2, col1:col2, :);
imshow(subImage);
axis('on', 'image');
caption = sprintf('Tile #%d\nRows %d to %d\nColumns %d to %d', ...
loopCounter, row1, row2, col1, col2);
title(caption, 'fontSize', fontSize);
g.WindowState = 'maximized';
drawnow;
loopCounter = loopCounter + 1;
% Delay a fraction of a second so we can see it.
pause(0.33)
end
end
msgbox('Done!');
fprintf('Done running %s.m.\n', mfilename);
But you did not actually ask a question or say if anything is wrong or not what you want. If this didn't work, then be specific because I don't know what else to offer you.
Akakan-Abasi Okon
on 19 Jun 2021
Edited: Akakan-Abasi Okon
on 19 Jun 2021
Let's say we are working with this.
B = randi([0, 1], 100, 200);
imshow(B, 'InitialMagnification', 1000);
impixelinfo();
axis('on', 'image');
The output image of the above program is going to be 100 x 200 points i.e (height x width) as shown in attached IMSHW 1 image. Now what I wan't is to divide the width by 5 to get something similar to 5 x 4 layout with 20 tiles as shown in attached IMSHW image with vertical gaps separating the black rectangles as shown in IMSHW 2.
Image Analyst
on 19 Jun 2021
There are 15 black patches (not 25 and not 20) on a white background canvass. Additionally, the black patches seem to have random heights and to be placed at random row locations. And they black patches to not have the salt and pepper appearance of the images like you said you wanted first. So do you just want to create black patches of random heights and paste them in at random rows for each of the 5 columns?
This code will give you something similar to IMSHW.png:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
canvass = 255 * ones(100, 200, 'uint8');
patchWidth = 35;
for col = 1 : 5
% Make patch a random height between 16 and 34
patchHeight = round(16 + 18 * rand(1))
% Get a random location to paste it
row1 = 1 + round(15*rand)
row1bottom = (row1 + patchHeight - 1)
row2 = round(35 + 15*rand)
row2bottom = (row2 + patchHeight - 1)
row3 = round(70 + 15*rand)
row3bottom = min([100, (row3 + patchHeight - 1)])
% Get columns
col1 = (col-1)*40 + 1;
col2 = col1 + patchWidth - 1;
% Paste black rectangle onto canvass
canvass(row1 : row1bottom, col1 : col2) = 0;
imshow(canvass, []);
axis('on', 'image');
canvass(row2 : row2bottom, col1 : col2) = 0;
imshow(canvass, []);
axis('on', 'image');
canvass(row3 : row3bottom, col1 : col2) = 0;
imshow(canvass, []);
axis('on', 'image');
end
axis('on', 'image');
fprintf('Done! Thanks Image Analyst!\n');
Akakan-Abasi Okon
on 19 Jun 2021
Great job Image Analyst, this is just what I wanted.
Thanks so much for your support.
Akakan-Abasi Okon
on 24 Jul 2021
Hi, I want to find all the points in the original image that are “zeros”.
Thereafter, generate as many uniformly distributed random numbers as "zeros" and compare with the former using these steps.
For example,
M = exprnd(2, 5,6) % Original image
N = M < 1; % points in the image that are zeros
C = (1-0).*rand(5,6)+0 % generating random uniform numbers of zero
How do I achieve this
If C < 0.02, then change the points for which this inequality is true from zero to one.
If C = 0, then change the points for which this inequality is true from one to zero.
Then compare the outputs with M & N.
Image Analyst
on 24 Jul 2021
I don't know what "find" means to you. If you want a list of all the rows/columns coordinates you can use find():
[rows, columns] = find(~binaryImage); % Find zeros
% Plot them
plot(columns, rows, 'r.', 'MarkerSize', 10);
Akakan-Abasi Okon
on 20 Aug 2021
Edited: Akakan-Abasi Okon
on 20 Aug 2021
Hi, please how can I regroup the scattered 'ones' in the matrix back to it's original matrix form which was made up of rectangular blocks of ones as shown in the attached images.
I need a code to regroup the adjacent 'ones' into a rectangular tiles or blocks.
Image Analyst
on 20 Aug 2021
I don't know off the top of my head. You'll have to figure that out on your own.
Akakan-Abasi Okon
on 21 Aug 2021
Thank you Image Analyst, please you can help with this one.
What I actually want is how to remove noise from this noisy image to get back my original image.
Though the recovered image might not look exactly the way it was but should have the rectangular blocks(probably with some distortions) in similar position as it was in the original image.
More Answers (0)
See Also
Categories
Find more on Basic Display in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)