image help
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个)
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!