executions is fine for the first run but it is showing an error

1 view (last 30 days)
function ImagesExample(uname1) %# read images in a cell array disp(uname1); imgs = cell(6,1); for i=1:6 imgs{i} = imread( sprintf('C:/Users/maruthi1/Documents/MATLAB/images3/ma3%1d.jpg',i) ); end
%# show them in subplots
figure('Name','Image_mag','Numbertitle','off');
title('Image_mag');
for i=1:6
subplot(2,3,i);
h = imshow(imgs{i}, 'InitialMag',200, 'Border','tight');
title(num2str(i))
set(h, 'ButtonDownFcn',{@callback,i})
end
function callback(o,e,idx)
%# show selected image in a new figure
figure(2), imshow(imgs{idx})
title(num2str(idx))
promptMessage = sprintf('Drag out a box that you want to copy,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
A=imgs{idx};
n = input('Please enter the number of scenes you wish to crop: ')
ii=1;
%load user.mat
while (ii<=n)
im_cropped{ii} = imcrop(imgs{idx});
ii=ii+1;
end
for ii=1:n
figure (4) ;
h=[];
h(ii)=subplot(3,4,ii);
imshow(im_cropped{ii},'Parent',h(ii));
percent_match(uname1,im_cropped{ii});
ii=ii+1;
end
promptMessage = sprintf('Your cropped images saved successfully');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
end
end
percent_match.m
function percent_match(uname1,imgs)
disp(uname1);
myDir = 'user_images/';
myDir=strcat(myDir,uname1)
myDir=strcat(myDir,'/');
jpgFiles = dir([myDir '*.jpg']);
numFiles = length(jpgFiles);
mydata = cell(1,numFiles);
% mydata = zeros(numFiles);
for k = 1:numFiles
mydata{k} = imread([myDir jpgFiles(k).name]);
mydata{k}=rgb2gray(mydata{k});
imgs=rgb2gray(imgs);
mydataPoints=detectSURFFeatures(mydata{k});
strong1 = mydataPoints.selectStrongest(50);
imgsPoints=detectSURFFeatures(imgs);
strong2 = imgsPoints.selectStrongest(50);
[mydataFeatures,mydataPoints1]=extractFeatures(mydata{k},strong1);
[imgsFeatures,imgsPoints]=extractFeatures(imgs,strong2);
mydataPairs=matchFeatures(mydataFeatures,imgsFeatures);
matchedmydataPoints = mydataPoints(mydataPairs(:, 1), :);
matchedimgsPoints = imgsPoints(mydataPairs(:, 2), :);
[tform, inlierBoxPoints, inlierScenePoints] = ...
estimateGeometricTransform(matchedmydataPoints, matchedimgsPoints, 'affine');
boxPolygon = [1, 1;... % top-left
size(mydata{k}, 2), 1;... % top-right
size(mydata{k}, 2), size(mydata{k}, 1);... % bottom-right
1, size(mydata{k}, 1);... % bottom-left
1, 1];
newBoxPolygon = transformPointsForward(tform, boxPolygon);
%figure;
%imshow(imgs);
%hold on;
%line(newBoxPolygon(:, 1), newBoxPolygon(:, 2), 'Color', 'y');
%title('Detected Box');
numPairs = length(mydataPairs); %the number of pairs
percentage = (numPairs*2);
if percentage < 75
disp('we have this');
disp(percentage);
else
disp('we dont have this')
end
end
end
Please help me for my project.
  1 Comment
Walter Roberson
Walter Roberson on 6 Apr 2017
I recommend that you recode using fullfile() instead of building the file names the way you do now

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 6 Apr 2017
Well, clearly from the error message, you're passing a 2D matrix to rgb2gray. If you pass a 2D matrix to rgb2gray it must be a colour map which is always a matrix with 3 columns. I suspect that instead you're giving it a greyscale image. rgb2gray only works on colour images or colour maps, not greyscale images.
It's impossible for us to tell you which image is causing problem since we don't have access to them. I would suggest you learn to use the debugger. At the comment prompt, issue
dbstop if error
run your code and when it breaks into the debugger, see which rogue image (uname or imgs) is causing the issue.
You could also wrap the call to rgb2gray in a check:
switch ndims(inputimage)
case 2 %image is already greyscale
greyimage = inputimage;
case 3 %colour image
greyimage = rgb2gray(inputimage);
otherwise
error('not an image');
end

Categories

Find more on Images in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!