現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
how to change the color of pixels in a binary image?
5 ビュー (過去 30 日間)
古いコメントを表示

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.

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

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 で Blue についてさらに検索
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 (한국어)
