grayscale image, adjust color intesity

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)

Never use size() on images like you did. It's dangerous. See this: Steve's blog on this
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

I want to be segmented in a color image.(grayscale image), and store all in one matrix(coordinates of pixel).
You just say that if the gray level is less than about 64, make it red, and if it's between 64 and 145 it's green, and if it's greater than 145 it's.
blue.
if i use
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 ];
elseif I2(i,j)>=65 && I2(i,j)<=145
P = [P ; i j ];
elseif I2(i,j)>=146
P = [P ; i j ];
end
end
end
size(P);
MON=P;
[IDX,ctrs] = kmeans(MON,4)
clusteredImage = zeros(size(I2));
clusteredImage(sub2ind(size(I2) , P(:,1) , P(:,2)))=IDX;
imshow((clusteredImage))
why my ouput is white image
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);
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.
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?
I do not know how to say it right, i need matrix,to cluster colors that are common.
I know that it can be used listed up, i must to use kmeans.
I need matrix, as input to kmeans, I don't know, how to get matrix from image(useful for clustering).
I need help with 2 a 3 steps. I don't know,what to do,if I have grayscale image.
Maybe just try the Otsu threshold.
i must to use kmeans.
when i used Otsu threshold, i have not input to kmeans.
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.
input image
output image
I have a problem, when i have to use my function kmeans.
My ouput is stored in cell.
I don't know show image.
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.
Of course I'm the input image, convert to grayscale image. I don't know show image, because, i have main ouput in cell, i must convert to matrix and then show,only i have error, I don't know repair error. It is pity that you do not have statistical toolbox. Thanks for your time and valuable advice.

Sign in to comment.

Asked:

on 16 Mar 2014

Commented:

on 17 Mar 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!