現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to change the Image colour of ones and zeros to white and black
2 ビュー (過去 30 日間)
古いコメントを表示
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.
採用された回答
Image Analyst
2021 年 6 月 18 日
You don't need a colormap. Just try this:
B = randi([0 1],50, 100);
imshow(B)
impixelinfo();
16 件のコメント
Akakan-Abasi Okon
2021 年 6 月 18 日
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
2021 年 6 月 18 日
@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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/658160/image.png)
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
2021 年 6 月 18 日
編集済み: Akakan-Abasi Okon
2021 年 6 月 18 日
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
2021 年 6 月 19 日
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
2021 年 6 月 19 日
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
2021 年 6 月 19 日
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/658525/image.png)
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
2021 年 6 月 19 日
編集済み: Akakan-Abasi Okon
2021 年 6 月 19 日
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
2021 年 6 月 19 日
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/658755/image.png)
Akakan-Abasi Okon
2021 年 6 月 19 日
Great job Image Analyst, this is just what I wanted.
Thanks so much for your support.
Akakan-Abasi Okon
2021 年 7 月 24 日
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
2021 年 7 月 24 日
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
2021 年 8 月 20 日
編集済み: Akakan-Abasi Okon
2021 年 8 月 20 日
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
2021 年 8 月 20 日
I don't know off the top of my head. You'll have to figure that out on your own.
Akakan-Abasi Okon
2021 年 8 月 21 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Basic Display についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)