how to change the color of pixels in a binary image?
6 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170510/image.png)
I want to generating random pixels on edge boundary and give red color to that pixels. Thanks in advance.
1 个评论
Rik
2017-12-1
A binary image has only 2 colors. Depending on what method you choose to display it, you can choose the two colors yourself.
采纳的回答
Image Analyst
2017-12-2
See attached m-file.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/179755/image.png)
20 个评论
bamini thavarajah
2017-12-2
what is the pixel value of this red color? I want to choose a 31×31 small cell around each candidate points. can you help me?
bamini thavarajah
2017-12-3
编辑:Image Analyst
2017-12-3
In the modified image we have 100 red pixels (Is it correct?). When I count red pixels my code gives 700. can you correct me? This is my image. </matlabcentral/answers/uploaded_files/97091/point.png>
my code
I=imread('point.png');
rpcount=0;
[rows, columns, numberOfColorChannels] = size(I);
for i=1:rows
for j=1:columns
if (I(i,j,1)==255)
rpcount=rpcount+1;
end
end
end
disp(rpcount);
Image Analyst
2017-12-3
The white pixels also have a red component of 255 so you counted all red AND white pixels. You'd need
redPixels = I(:,:,1)==255 & I(:,:,2)==0 & I(:,:,3)==0;
numRedPixels = sum(redPixels(:));
instead of that for loop.
bamini thavarajah
2017-12-3
I need for loop. because I want to choose a 31×31 small cell around each candidate point. could you help me ?
Image Analyst
2017-12-3
In my code, take the binaryImage and call imdilate() to expand each location.
bamini thavarajah
2017-12-3
redPixels = I(:,:,1)==255 & I(:,:,2)==0 & I(:,:,3)==0; numRedPixels = sum(redPixels(:));
this gives 196
Image Analyst
2017-12-3
You forgot to attach your code and image. Your link above doesn't work. Also I'd need your dilate code if you want me to reproduce it from the original image.
bamini thavarajah
2017-12-3
编辑:Image Analyst
2017-12-3
sorry sir, I can't understand how to use imdilate().
my original image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187272/image.gif)
coding file
Image Analyst
2017-12-3
What about this:
% Initialization / clean-up code.
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 = 25;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'apple-1.gif';
% Get the full filename, with path prepended.
folder = []; % Determine where demo folder is (works with all versions).
fullFileName = fullfile(folder, baseFileName);
%===============================================================================
% Read in a demo image.
binaryImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(binaryImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
binaryImage = rgb2gray(binaryImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 3, 1);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Original Gray Scale Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Get the edges.
binaryImage = edge(binaryImage,'Sobel');
% Display the image.
subplot(2, 3, 2);
imshow(binaryImage, []);
axis on;
axis image;
caption = sprintf('Edge Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
% Let's find the location of all the white pixels:
[rows, columns] = find(binaryImage)
% Get 30 random locations
randomIndexes = randperm(length(rows), 30);
% Get a new binary image
redDots = false(size(binaryImage));
for k = 1 : length(randomIndexes)
redDots(rows(randomIndexes(k)), columns(randomIndexes(k))) = true;
end
% Display the image.
subplot(2, 3, 3);
imshow(redDots, []);
% Dilate the red channel
redChannel = binaryImage | imdilate(redDots, true(5,6));
% Display the image.
subplot(2, 3, 4);
imshow(redChannel, []);
% Wherever red channel is set, make blue and green channels black there.
greenChannel = binaryImage; % Initialize
blueChannel = binaryImage; % Initialize
greenChannel(~redChannel) = 0;
blueChannel(~redChannel) = 0;
% Create an RGB image so we can have red pixels.
redChannel = 255 * uint8(redChannel);
greenChannel = 255 * uint8(greenChannel);
blueChannel = 255 * uint8(blueChannel);
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
% Display the image.
subplot(2, 3, 5);
imshow(rgbImage, []);
axis on;
axis image;
caption = sprintf('Modified Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo();
redPixels = rgbImage(:,:,1)==255 & rgbImage(:,:,2)==0 & rgbImage(:,:,3)==0;
numRedPixels = sum(redPixels(:))
Image Analyst
2017-12-3
Sorry, I've spent enough time already and need to do some other stuff. I can't just do the whole project for you. You need to do something. Try by looking at my Image Segmentation Tutorial https://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial to see how you can use regionprops() to make various measurements.
bamini thavarajah
2017-12-3
Thank you for your help. I need 100 random red pixel on edge. But Your above code didn't give 100. I Think binaryImage = edge(binaryImage,'Sobel'); this may not 1 pixel thinning.
bamini thavarajah
2017-12-5
In your above code, To create rgb image, why you use redChannel = 255 * uint8(binaryImage); greenChannel = 255 * uint8(binaryImage); blueChannel = 255 * uint8(binaryImage); and why we multiply by 255?
Image Analyst
2017-12-5
Because binary images are only in the range 0-1 while RGB images are in the range 0-255 if they are integer, or 0-1 is they are floating point. But I'm using integer so I scaled to 0-255.
bamini thavarajah
2017-12-5
when we set the green and blue channel to zero in those locations, can we obtain red on whole image or in those locations? I want in those locations.
Image Analyst
2017-12-5
The image will be red anywhere the red value is non-zero and the green and blue values are both 0. If that describes the whole image, then it's the whole image. If that describes only some pixels in the image, then only those pixels will be red.
bamini thavarajah
2017-12-5
thanks a lot sir for spent your time for me. I understood & I got the answer.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
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 (한국어)