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 color of pixels in a binary image?
15 views (last 30 days)
Show older comments
I want to generating random pixels on edge boundary and give red color to that pixels. Thanks in advance.
1 Comment
Rik
on 1 Dec 2017
A binary image has only 2 colors. Depending on what method you choose to display it, you can choose the two colors yourself.
Accepted Answer
Image Analyst
on 2 Dec 2017
See attached m-file.
20 Comments
bamini thavarajah
on 2 Dec 2017
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
on 3 Dec 2017
Edited: Image Analyst
on 3 Dec 2017
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
on 3 Dec 2017
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
on 3 Dec 2017
I need for loop. because I want to choose a 31×31 small cell around each candidate point. could you help me ?
Image Analyst
on 3 Dec 2017
In my code, take the binaryImage and call imdilate() to expand each location.
bamini thavarajah
on 3 Dec 2017
redPixels = I(:,:,1)==255 & I(:,:,2)==0 & I(:,:,3)==0; numRedPixels = sum(redPixels(:));
this gives 196
Image Analyst
on 3 Dec 2017
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
on 3 Dec 2017
Edited: Image Analyst
on 3 Dec 2017
sorry sir, I can't understand how to use imdilate().
my original image
coding file
Image Analyst
on 3 Dec 2017
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
on 3 Dec 2017
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
on 3 Dec 2017
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
on 5 Dec 2017
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
on 5 Dec 2017
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
on 5 Dec 2017
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
on 5 Dec 2017
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
on 5 Dec 2017
thanks a lot sir for spent your time for me. I understood & I got the answer.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox 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 (한국어)