I have an intensity image and want to color the pixels which their intensity is 50. I want to color them red.In fact I want to replace pixels who's value is 50 with red. How can I do this?
MATLAB: How to color a certain pixel value in an intensity image
image processingImage Processing Toolboxintensity imagepixel value
Related Solutions
That's not a gray scale image you attached, it's an RGB image. So try this
rgbImage = imread('C1fig.jpg');whos rgbImagesubplot(2, 1, 1);imshow(rgbImage);title('Original RGB image C1fig.jpg');axis('on', 'image');impixelinfomask = rgbImage(:, :, 1) == 0;% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);greenChannel = rgbImage(:, :, 2);blueChannel = rgbImage(:, :, 3);redChannel(mask) = 255;greenChannel(mask) = 0;blueChannel(mask) = 0;% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);subplot(2, 1, 2);imshow(rgbImage);axis('on', 'image');impixelinfotitle('Altered RGB image');
If it really is a gray scale image, you can try this:
grayImage = imread('C1fig.jpg');if ndims(grayImage) == 3; grayImage = rgb2gray(grayImage);endwhos grayImagesubplot(2, 1, 1);imshow(grayImage);title('Original image C1fig.jpg');axis('on', 'image');impixelinfomask = grayImage == 0;% Initialize the individual red, green, and blue color channels.
redChannel = grayImage;greenChannel = grayImage;blueChannel = grayImage;redChannel(mask) = 255;greenChannel(mask) = 0;blueChannel(mask) = 0;% Recombine separate color channels into a single, true color RGB image.rgbImage = cat(3, redChannel, greenChannel, blueChannel);subplot(2, 1, 2);imshow(rgbImage);axis('on', 'image');impixelinfotitle('Altered RGB image');
It's just 3 lines. Just mask the original image:
binaryImage = grayImage > 150;maskedImage = zeros(size(grayImage)); % Initialize.
maskedImage(binaryImage) = grayImage(binaryImage);
See the full demo below:
% Demo by ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'coins.png';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);subplot(2, 2, 2); bar(pixelCount);grid on;title('Histogram of original image', 'FontSize', fontSize);xlim([0 grayLevels(end)]); % Scale x axis manually.
yl = ylim();line([150 150], [0 yl(2)], 'Color', 'r');% Threshold at 150
binaryImage = grayImage > 150;% Display the original gray scale image.subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image', 'FontSize', fontSize);% Mask the image
maskedImage = zeros(size(grayImage)); % Initialize.% Assign values from grayImage that are > 150.
maskedImage(binaryImage) = grayImage(binaryImage);% Display the original gray scale image.subplot(2, 2, 4);imshow(maskedImage, []);title('Masked Image', 'FontSize', fontSize);
Best Answer