Hey, I have written this code for conversion of plot to rgb image followed by conversion to binary image but I am unable to get the binary image and an error that says-"index exceeds matrix dimension"is being displayed. Can someone pls help me? thks!

1 view (last 30 days)
% plot r = 1; t=0:0.002:1; a=zeros(500); s=zeros(500); for i=1:500 a(i)= deg2rad(t(i)*90); s(i) = (r*pi*t(i)) / 2; xc(i) = r * sin(a(i)); yc(i) = r * sin(a(i)); x(i) = xc(i) + ( s(i) * sin(a(i))); y(i) = yc(i) - ( s(i)* cos(a(i))); plot(x,y)
end % rasterize it to an 50 row x 100 col image; nRow = 50; nCol = 100; xNode = linspace(min(x),max(x),nCol); yNode = linspace(min(y),max(y),nRow); yInterp = interp1(x,y,xNode,'cubic','extrap'); xi = 1:nCol; yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
im = accumarray([yi(:), xi(:)],1,[nRow,nCol]); imagesc(im); % define threshold frequency threshold=125; % obtain the image size [M,N,clrs]=size(im); M=10 N=10 clrs=2 %convert the image into type double for matlab operations tempimage=double(im); % create space for the lightened image filteredimage = zeros(M,N,2); % compute the average of all color intensities temp1=(tempimage(:,:,1)+ tempimage(:,:,2))/2; % compare this with the threshold temp2=temp1 > threshold; %convert it to a range from 0-255 filteredimage(:,:,1)=255 * double(temp2);
filteredimage(:,:,2)=filteredimage(:,:,1);
%
newimage=uint8(filteredimage);
figure
image(newimage);
axis off
  1 Comment
Andrew Reibold
Andrew Reibold on 8 Sep 2014
Edited: Andrew Reibold on 8 Sep 2014
Advice to get more responses on future questions:
1) Concise topic title. "Help convert plot to binary image", or "Why do I get 'index exceeds matrix dimension' ", and then elaborate your question in better detail in the body.
If you ask the entire question in the topic, many people won't read it.
2) Format all code in question so that it is readable
It makes it easier for us to help you! :)

Sign in to comment.

Answers (3)

Geoff Hayes
Geoff Hayes on 7 Sep 2014
Devanshee - please format your above code so that it is readable. Highlight the code portions and press the {}Code button. Use the Preview window to verify that the code is readable.
As well, please include the line number that the error corresponds to. The index exceeds matrix dimension is telling you that you are trying to access data within a matrix using indices that are larger than the dimension(s) of that matrix.
Look at the initialization of im which is a 50x100 matrix, the initialization of tempimage which is img cast as a double, and then
temp1=(tempimage(:,:,1)+ tempimage(:,:,2))/2;
Knowing the dimensions of tempimage, is the above possible?
  2 Comments
Devanshee Tanna
Devanshee Tanna on 7 Sep 2014
Sir, I m not sure of that itself. kindly enlighten me on how I can fix it. I m aware of the definition of the error but am unable to fix it.
Geoff Hayes
Geoff Hayes on 7 Sep 2014
Devanshee - your code assumes that the tempimage is three-dimensional when it is only two-dimensional. See (and use) Image Analyst's solution to this problem.

Sign in to comment.


Image Analyst
Image Analyst on 7 Sep 2014
Look at the workspace and use the debugger. You'll notice that im and tempimage are grayscale images, not color. You got confused because imagesc() uses some kind of funky color map by default (that's why I hate it and never use it). Use image() or imshow() instead. Then if you want funny colors, call colormap(), like
image(im); % or imshow(im, []);
colormap(jet(256);
Then if you want a color image so that you can average the red and green channels together, you have to call ind2rgb with the desired colormap:
rgbImage = ind2rgb(im, jet(256));
Try that and come back with corrected code if you have any problems.
  7 Comments
Devanshee Tanna
Devanshee Tanna on 8 Sep 2014
Sir, I am a beginner trying to create a binary image from a plot that I have created, of an involute gear profile. The ultimate aim is to compare this image with the sample image of a gear and see how involute the latter one is.. Because I am a beginner, I am trying to achieve the output, without realizing how tedious it can get. The intention was not to make it this complex.
Image Analyst
Image Analyst on 8 Sep 2014
But why not just assign pixels to a binary image immediately rather than trying to turn a screenshot, with tick marks and tick labels and other unwanted clutter, into the binary image?

Sign in to comment.


Devanshee Tanna
Devanshee Tanna on 7 Sep 2014
Edited: Devanshee Tanna on 7 Sep 2014
Following is the code that I have written and also, i have pointed where the error is. I shall be grateful if you could help! % plot r = 1; t=0:0.002:1; a=zeros(500); s=zeros(500); for i=1:500 a(i)= deg2rad(t(i)*90); s(i) = (r*pi*t(i)) / 2; xc(i) = r * sin(a(i)); yc(i) = r * sin(a(i)); x(i) = xc(i) + ( s(i) * sin(a(i))); y(i) = yc(i) - ( s(i)* cos(a(i))); plot(x,y)
end
% rasterize it to an 50 row x 100 col image;
nRow = 50;
nCol = 100;
xNode = linspace(min(x),max(x),nCol);
yNode = linspace(min(y),max(y),nRow);
yInterp = interp1(x,y,xNode,'cubic','extrap');
xi = 1:nCol;
yi = round(interp1(yNode,1:nRow,yInterp,'linear','extrap'));
im = accumarray([yi(:), xi(:)],1,[nRow,nCol]);
imagesc(im);
% define threshold frequency
threshold=125;
% obtain the image size
[M,N,clrs]=size(im);
M=10
N=10
clrs=2
%convert the image into type double for matlab operations
tempimage=double(im);
% create space for the lightened image
filteredimage = zeros(M,N,2);
% compute the average of all color intensities
temp1=(tempimage(:,:,1)+ tempimage(:,:,2))/2;-------> this is the line where the error is showing
% compare this with the threshold
temp2=temp1 > threshold;
%convert it to a range from 0-255
filteredimage(:,:,1)=255 * double(temp2);
filteredimage(:,:,2)=filteredimage(:,:,1);
%
newimage=uint8(filteredimage);
figure
image(newimage);
axis off

Categories

Find more on Matrix Computations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!