Error when reading data
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Coulton
il 27 Set 2012
Commentato: Walter Roberson
il 28 Lug 2016
I am having an issue when obtaining some image data using the image processing toolbox. When I run the program using one set of images the program works fine. When I use a different set it spits out an error.
Too many outputs requested. Most likely cause is missing [] around
left hand side that has a comma separated list expansion.
Error in ImFunc (line 51)
bb = measurements.BoundingBox;
Error in ImageProcessing (line 18)
[Theta,centroid] = ImFunc(Length,ImNum,FoldLoc,First);
For the record, when I add the [] the program then gets stuck. Here is the code I am using:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
% Used to calibrate pixel length.
Length = input('Enter length of strip (in inches): ');
% Used to determine what images to look at
ImNum = input('Enter total images taken: ');
First = input('Enter the first image used: ');
%This is where the program looks for the images.
% Example: K:\Matlab_Research\3B_01\3B_01
FoldLoc = input('Enter the location of the file folder: ','s');
% Defined function
[Theta,centroid] = ImFunc(Length,ImNum,FoldLoc,First);
% Use these variables to plot the x and y locations
x = centroid(:,1);
y = centroid(:,2);
% Use this to plot against Theta
z = First:ImNum;
subplot(2,1,1), plot(x,y)
subplot(2,1,2), plot(z,Theta)
And the function can be seen below:
function [Theta,centroid] = ImFunc(Length,ImNum,FoldLoc,First)
% Pixel per inch sum
PPIS = 0;
for i = First:ImNum
% Detemine which image to use.
if i < 10
Imagenumber = sprintf('B0000%d.jpg',i);
elseif i < 100
Imagenumber = sprintf('B000%d.jpg',i);
elseif i < 1000
Imagenumber = sprintf('B00%d.jpg',i);
elseif i < 10000
Imagenumber = sprintf('B00%d.jpg',i);
else
Imagenumber = sprintf('B0%d.jpg',i);
end
% Read in a standard MATLAB gray scale demo image.
folder = FoldLoc;
baseFileName = Imagenumber;
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Crop away tick marks, colorbar, etc..
grayImage = rgb2gray(imcrop(grayImage, [1 1 725 400]));
% Get the binary image
% Use an algorithm to find the threshold.
binaryImage = grayImage > 110;
% Smooth it out some and clean it up.
binaryImageCent = bwareaopen(binaryImage, 80);
labeledImage = bwlabel(binaryImageCent);
measurements = regionprops(labeledImage, 'all');
% Crop it to the bounding box.
bb = measurements.BoundingBox;
% Crop the image.
binaryImage = imcrop(binaryImageCent, bb);
% Skeletonize the image.
skeleton = bwmorph(binaryImage, 'skel', 'inf');
% Find the endpoints of the skeleton.
skeleton = bwmorph(skeleton, 'endpoints');
% Get the coordinates of all endpoints
[rows cols] = find(skeleton);
% Find the two that are farthest apart
maxDistance = -1;
for k1 = 1 : length(rows)
row1 = rows(k1);
col1 = cols(k1);
for k2 = 1 : length(rows)
row2 = rows(k2);
col2 = cols(k2);
distance = sqrt((row1-row2)^2 + (col1-col2)^2);
if distance > maxDistance
maxDistance = distance;
% Finding x and y component distances
y = abs(row2-row1);
x = abs(col2-col1);
end
end
end
% Angle from horizontal
Thet = atan(y/x)*180/pi;
% Ensure Theta values correspond to image number
Theta(i + 1 - First) = Thet;
% Determine pixels per inch
PPI = maxDistance/Length;
%Find the center location
measurements = regionprops(binaryImageCent, 'Centroid');
cent = measurements(1).Centroid;
% Convert pixel location to actual inch location
% Ensure centroid values correspond to the image number
centroid(i + 1 - First,:) = cent/PPI;
% Find every PPI value
PPIS = PPI + PPIS;
end
% Average the PPI values
PPIA = PPIS/(ImNum - First);
% Find actual size of image
ImageSize = size(grayImage);
height = ImageSize(1,1)/PPIA;
width = ImageSize(1,2)/PPIA;
% Convert the center locations to reflect the actual image
centroid(:,2) = height - centroid(:,2);
centroid(:,1) = width - centroid(:,1);
Ideas? If you need more info let me know. In the working data I am processing about 400 images. In the nonworking data I am processing 250.
0 Commenti
Risposta accettata
Walter Roberson
il 27 Set 2012
If more than one region is found in the bwlabel, then the regionprops is going to return a structure array, and then measurements.BoundingBox would be attempting to expand a structure array, leading to the error.
0 Commenti
Più risposte (2)
Salitha Kaveendra Nanayakkara
il 28 Lug 2016
Facing same problem.Any solutions?
1 Commento
Walter Roberson
il 28 Lug 2016
Somewhere in your code you have a line similar to
A = B.C;
but B is a non-scalar array. You need to either track down why B is non-scalar when you expect it to be scalar, or else you need to use one of
A = {B.C};
or
A = {};
[A{1:length(B)}] = B.C;
In some cases you might be able to use
A = vertcat(B.C);
This reply is general because you asked a general question with no code for us to examine.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!