이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to change the Image colour of ones and zeros to white and black
조회 수: 1 (최근 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:
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);
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');
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!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)