grayscale image, adjust color intesity
Show older comments
Hello,
I have image.
I1= imread('strom','bmp');
imshow(I1)
I2=rgb2gray(I1);
imshow(I2);
[m n]=size(I2)
P = [];
for i=1:m
for j=1:n
if I2(i,j)>=0 && I2(i,j)<=64
P = [P ; i j ];
end
end
end
size(P);
MON=P;
how i set all shades of gray in matrix.
Thanks.
Answers (1)
Image Analyst
on 16 Mar 2014
Do this instead:
[rows, columns, numberOfColorChannels] = size(yourImage);
And I don't know what "set" means to you. Do you just want (for some reason) an N by 2 list of the (row, column) locations of all gray levels in a certain range? If so do this:
binaryImage = I2 >= 0 & I2 <= 64;
[rows, columns] = find(binaryImage);
13 Comments
Image Analyst
on 16 Mar 2014
Edited: Image Analyst
on 16 Mar 2014
But Tomas, I don't know that you like my answers. For example after I gave you the code to vectorize and simplify the process, you chose not to use any of the code I provided and went back to your inefficient, non-MATLAB-ish nested for loops approach. I'll show give you a full demo to turn those parts red using two different methods (pick your favorite to use in your code). Since I spent time on it I hope you'll find it useful and informative and take my suggestions this time.
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;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% 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.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, '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;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% 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(grayLevels, pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
%======== METHOD 1 ===================================
% Find out where the image is >= 0 and <= 64
binaryImage = grayImage >= 0 & grayImage <= 64;
[rows, columns] = find(binaryImage);
% Initialize a color image
redChannel = grayImage;
greenChannel = grayImage;
blueChannel = grayImage;
% Use binary image to set colors in the individual color channels
redChannel(binaryImage) = 255;
greenChannel(binaryImage) = 0;
blueChannel(binaryImage) = 0;
% Create RGB image from separate color channels.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
subplot(2,2,3);
imshow(rgbImage);
title('Image transformed with binary image', 'FontSize', fontSize);
%======== METHOD 2 ===================================
% Create a colormap
colorMap = jet(256);
% Set 0=64 = red (1,0,0)
colorMap(1:65, :) = repmat([1, 0, 0], [65,1]);
% Apply colormap to create an RGB image
rgbImage2 = ind2rgb(grayImage, colorMap);
subplot(2,2,4);
imshow(rgbImage);
title('Image transformed with color map', 'FontSize', fontSize);

Image Analyst
on 16 Mar 2014
Tomas's "Answer" moved here:
Ok, my teacher, We said that it do so. The thing that I don't know what i have to clustering in the image. I just want to know how to create a matrix from image, I could use clustering methods.
I'm very glad you took the time to help me.
Thaks for you help.
Image Analyst
on 16 Mar 2014
I added the "homework" tag, which you forgot to do. An image and a matrix are the same thing, or can be. What kind of matrix do you want?
Tomas
on 16 Mar 2014
Image Analyst
on 16 Mar 2014
Why don't you just follow this example then:
Tomas
on 16 Mar 2014
Image Analyst
on 16 Mar 2014
Maybe just try the Otsu threshold.
Tomas
on 16 Mar 2014
Image Analyst
on 16 Mar 2014
Again, I don't have the Statistical Toolbox so I can't use kmeans() to try anything for you. Since you don't have 2D clusters with a gray scale image, let's imagine that you just had a bunch of pixels with gray level around 50 and another bunch with a gray level around 200. So one might say that the "clusters" would be the gray levels themselves. So just throw grayImage(:), which is a 1D vector of all the pixel gray level values in the image, into kmeans() and see what pops out.
Tomas
on 16 Mar 2014
Image Analyst
on 17 Mar 2014
I gave you an example for a color image, but then you said "I don't know,what to do,if I have grayscale image." so I gave you suggestions for grayscale images. But now you show a color image again. And you ask "I don't know show image." whatever that means. Why not just use imshow() to show an image? Why are you confusing me by keep switching color mode? I've given suggestions for both but whenever I do you ask about the other. Sorry, but I think maybe it's best if we let someone who has kmeans() to take over and help you now. They will be able to understand you better than me.
Tomas
on 17 Mar 2014
Categories
Find more on k-Means and k-Medoids Clustering 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!
