필터 지우기
필터 지우기

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 (최근 30일)
% 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
Andrew Reibold
Andrew Reibold 2014년 9월 8일
편집: Andrew Reibold 2014년 9월 8일
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! :)

댓글을 달려면 로그인하십시오.

답변 (3개)

Geoff Hayes
Geoff Hayes 2014년 9월 7일
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
Devanshee Tanna
Devanshee Tanna 2014년 9월 7일
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 2014년 9월 7일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2014년 9월 7일
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
Devanshee Tanna
Devanshee Tanna 2014년 9월 8일
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 2014년 9월 8일
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?

댓글을 달려면 로그인하십시오.


Devanshee Tanna
Devanshee Tanna 2014년 9월 7일
편집: Devanshee Tanna 2014년 9월 7일
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

카테고리

Help CenterFile Exchange에서 Matrix Computations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by