Question cell structure, plotting and how to avoid a loop.

조회 수: 1 (최근 30일)
Filippos
Filippos 2014년 9월 21일
댓글: Filippos 2014년 9월 21일
Hello everybody,
I have a 1x13 cell structure called "Im" containing 13 matrices of 512x512 dimension with integer number ( which represent images). I want to plot all 13 matrices along with a fit I made, which basically should be like this code:
plot(fit,Pixels,Im{:}(:,256))
As I am sure you know, I get an error called Bad cell reference operation. The code works if I choose just one cell, for example plot(fit,Pixels,Im{5}(:,256)) but doesn't work as I tried above. I would wish not to use a loop as I think it could be avoided.
Thank you very much in advance

채택된 답변

Guillaume
Guillaume 2014년 9월 21일
편집: Guillaume 2014년 9월 21일
Even if Im{:}(:, 256) was valid matlab syntax it still wouldn't work as plot expects a pair of x and y coordinate for each line, not just one x vector and a number of y vectors.
The way to extract column 256 from all the images is with:
cellfun(@(img), img(:, 256), Im, 'UniformOutput', false) %return a cell array of columns
plot also accepts a vector of x and matrix of y where each column or row is a line to plot, so I would just transform your image columns in a matrix and concatenate that with Pixels. Assuming that Pixels is a column vector:
plot(fit, [Pixels cell2mat(cellfun(@(img) img(:, 256), Im, 'uni', false))]);
edited for spelling and typos
  댓글 수: 4
Guillaume
Guillaume 2014년 9월 21일
Sorry, there was an extra comma between @(img) and img(..) that shouldn't have been there (now corrected).
@(img) img(:, 256) is an anonymous function. It is equivalent to:
function out = myfun(img)
out = img):, 256);
end
The whole cellfun is equivalent to:
c = cell(size(Im));
for idx = 1:numel(Im)
img = Im{idx};
c{idx} = img(:, 256);
end
Filippos
Filippos 2014년 9월 21일
Many thanks, it has worked exactly as I wanted. Thank you very much and have a nice day sir.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 9월 21일
I don't know what fit and Pixels are but presumably they're lists of x and y coordinates. Then, for the next argument of plot() you don't put a whole image. You display images with a separate call to imshow(), image(), or imagesc().
% First, loop to display all 13 images.
for k = 1 : length(Im)
thisImage = Im{k}; % Extract image
subplot(3, 5, k);
imshow(thisImage);
end
% Now plot fit vs. Pixels
subplot(3, 5, 15);
plot(fit, Pixels, 'b-', 'LineWidth', 3);
grid on;
xlabel('fit', 'FontSize', 20);
ylabel('Pixels', 'FontSize', 20);
title('Pixels vs. fit', 'FontSize', 20);
  댓글 수: 2
Filippos
Filippos 2014년 9월 21일
I am sorry that I was not very clear. I do now want to produce the images, but the intensity profile in the middle of the image. The "fit" variable is the one you get from the cftool and the Pixels is just a vector 1:512. In the end I am planning to do something like this : http://tinypic.com/view.php?pic=2j0dnqr&s=8#.VB7KhFfitNg
Guillaume
Guillaume 2014년 9월 21일
Doesn't my answer below produce what you want?

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

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by