Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need quickly help to define matrix using variable

Subject: need quickly help to define matrix using variable

From: norton

Date: 3 Apr, 2010 14:08:05

Message: 1 of 5


asslamo alycom


i have a code to find the histogram of an image, and when i want to define the index of a matrix using a variable , it gives me an error:
here is the code below:


clear all;
close all;
clc
x = imread('s1.jpg');

h = rgb2gray(x);
imshow(h)
 m = size(x,1)
n = size(x,2)

r = 1;
r = repmat(r,1,255);



for k = 0 :1:255
    k = [k];
    n = 0;
    
    for i = 1: 305
        
        for j = 1:470
            
            if h(i,j)==k
                n = n+1;
               
                n = [n];
            r(r) = k %%%%% here is the problem , i dont know why???
      
            end
        end
    end
end





plzzz help me quickly

Subject: need quickly help to define matrix using variable

From: us

Date: 3 Apr, 2010 14:18:05

Message: 2 of 5

"norton "
> r(r) = k %%%%% here is the problem , i dont know why???

what do you CSSMers expect to do with this incomplete bit of information(?)...

us

Subject: need quickly help to define matrix using variable

From: ImageAnalyst

Date: 3 Apr, 2010 14:29:53

Message: 3 of 5

Not sure I follow this strange code but I think you might be trying
what I have below. Try my code and see if it does what you want:
Be sure to join any lines split into two by the newsreader.

% Clean up and initialize.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear all; % Erase all existing variables.
imtool close all; % Close all imtool windows.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Read in one of the standard MATLAB demo images
% as our original gray scale image and display it.
promptMessage = sprintf('Which image do you want to use.\nThe coins or
the cameraman.');
button = questdlg(promptMessage, 'Select Image', 'Coins', 'Cameraman',
'Coins');
if strcmp(button, 'Coins')
    grayImage = imread('coins.png');
else
    grayImage = imread('cameraman.tif');
end
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

[rows columns numberOfColorBands] = size(grayImage) % Not used, just
for info.

% Compute and plot the histogram.
subplot(2,2,2);
[counts, grayLevels] = imhist(grayImage);
minGLValue = find(counts > 0, 1, 'first')
maxGLValue = find(counts > 0, 1, 'last')
maxCount = max(counts);
bar(counts, 'r');
grid on;
xlabel('Gray Levels');
ylabel('Pixel Count');
title('Histogram of Gray Image', 'FontSize', fontSize);

subplot(2, 3, 5);
for grayLevel = minGLValue:maxGLValue
    [r c] = find(grayImage == grayLevel); % Can be used later, if
needed.
    promptMessage = sprintf('The current gray level = %d.\n\nDo you
want to Continue processing, or Cancel?', grayLevel);
    imshow(grayImage == grayLevel, []);
    caption = sprintf('Pixels with gray level of %d', grayLevel);
    title(caption, 'FontSize', fontSize);

    button = questdlg(promptMessage, 'Check out locations',
'Continue', 'Cancel', 'Continue');
    if strcmpi(button, 'Cancel')
        break;
    end
end

Subject: need quickly help to define matrix using variable

From: John D'Errico

Date: 3 Apr, 2010 14:30:15

Message: 4 of 5

"norton " <amz_ahmad@yahoo.com> wrote in message <hp7i45$on2$1@fred.mathworks.com>...
>
> asslamo alycom
>
>
> i have a code to find the histogram of an image, and when i want to define the index of a matrix using a variable , it gives me an error:
> here is the code below:
>
>
> clear all;
> close all;
> clc
> x = imread('s1.jpg');
>
> h = rgb2gray(x);
> imshow(h)
> m = size(x,1)
> n = size(x,2)
>
> r = 1;
> r = repmat(r,1,255);
>
>
>
> for k = 0 :1:255
> k = [k];

You seem to think for some reason that this

> k = [k];

does something. It does nothing. k already exists.
all you do with that line (and the other lines like
it where you do the same thing is to waste a few
spare cycles of cpu time.

Next, try to find a way to do the operation you are
trying to do, WITHOUT any loop at all.

John

Subject: need quickly help to define matrix using variable

From: Sadik

Date: 3 Apr, 2010 14:42:08

Message: 5 of 5

In addition, here are my 2 cents for your case:

1. You can define a vector of ones using

r = ones(1,255);

2. You're saying the problem is here:

r(r) = k;

Since all your values in r are 1s, you are simply setting

r(1) = k;

If you want your whole vector to be equal to [k,k,..., then you should instead do:

r = k*ones(1,255);

This will give you a vector of k's.

3. Let's do this. Explain what you want to do step by step, if none of these suggestions work and let's go from there.

Best.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us