필터 지우기
필터 지우기

image help

조회 수: 1 (최근 30일)
pdp rpa
pdp rpa 2012년 3월 20일
What is the meaning of the following code and why it is running in infinite loop??
image=imread('pp.jpg'); [rows,cols]=size(image); pixels=[];
for i=1:rows
for j=1:cols
pixels=[pixels;image(i,j)];
end
end

채택된 답변

Image Analyst
Image Analyst 2012년 3월 20일
Just use
pixels = reshape(yourImageArray, [1 numel(yourImageArray)]);
or
pixels = yourImageArray(:);
to get a 1D list of all the pixels. I think your code takes nearly forever because it needs to reallocate a new array every time you do pixels=[pixels;image(i,j)]; and that array gets bigger and bigger each time. And like the others said, don't use "image" for the name of any variable because you're blowing away the built-in function of that name if you do.
Tip: In the future, make sure there is a blank line before your code, then highlight all your code and click the "*{}Code*" icon above your edit box to format your text like code so that we can read it better.
  댓글 수: 1
pdp rpa
pdp rpa 2012년 3월 22일
Thank you so much sir for your kind response. Sory again i m seekig for your kind help. i have the following code of dbscan. but the problem is that i want to use this code for image segmentation. so plz help me to do so.
function [class,type]=dbscan(X,K,d)
% [class,type]=dbscan(X,K,d)
% X: matrix
% K: minimal points
% d: maximal distance
% class: assignments
% type: core: 1, border: 0, outlier: -1
if nargin<3
d=epsilon(X,K);
end
m=size(X, 1);
class=zeros(m,1);
type=zeros(m,1);
touched=zeros(m,1);
c=1;
for i=1:m
if touched(i), continue; end
indices=find(distances(X(i,:),X)<=d);
k=length(indices);
if k==1
type(i)=-1;
class(i)=-1;
touched(i)=1;
elseif k<=K
type(i)=0;
class(i)=0;
else
type(i)=1;
class(indices)=c;
while ~isempty(indices)
index=indices(1);
touched(index)=1;
I=find(distances(X(index,:),X)<=d);
if length(I)>1
class(I)=c;
if length(I)<=K;
type(index)=0;
else
type(index)=1;
end
for i=1:length(I)
if touched(I(i)), continue; end
touched(I(i))=1;
indices(end+1)=I(i);
class(I(i))=c;
end
end
indices=indices(2:end);
end
c=c+1;
end
end
I=find(class==0);
class(I)=-1;
type(I)=-1;
function d=epsilon(x,k)
[m,n]=size(x);
d=((prod(max(x)-min(x))*k*gamma(.5*n+1))/(m*sqrt(pi.^n))).^(1/n);
function D=distances(x,X)
D=sqrt(sum((ones(size(X, 1),1)*x-X).^2, 2));

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

추가 답변 (1개)

Jakob Sørensen
Jakob Sørensen 2012년 3월 20일
It shouldn't be infinite, but very possibly it will take a long time to run it. It seems to plot the image in a very weird and time consuming way, over and over again. I can't think of any reason to use that kind of code, but maybe someone else can. What is it you want to do?
  댓글 수: 3
Jakob Sørensen
Jakob Sørensen 2012년 3월 20일
Also just noted another thing, which explain why it seemed so confusing. "image" is the name of a function Matlab used to plot images (see "help image"), so using it as a variable name, why not disastrous, isn't very smart.
pdp rpa
pdp rpa 2012년 3월 22일
thank you sir...actually i have found this piece of code for image segmentation using dbscan. plz can you suggest some good code of dbscab for image segmentation. I will be very thankful to you.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by