How to load an image to a figure using uigetfile/imread?

Hello,
Novice coder here with a question that I am certain there will be an easy answer to - it just escapes me at the moment...
I am simply trying to display an image that I select from my PC's location, in a figure for further analysis. I actually have the analysis code working, but the problem I am having is that when I use uigetfile to provide the 'file look-up', I can navigate to the file, get the figure to launch, but the image then doesn't write to the figure. My code thus far is:
[filename, pathname] = uigetfile(...
{'*.jpg; *.JPG; *.jpeg; *.JPEG; *.img; *.IMG; *.tif; *.TIF; *.tiff, *.TIFF','Supported Files (*.jpg,*.img,*.tiff,)'; ...
'*.jpg','jpg Files (*.jpg)';...
'*.JPG','JPG Files (*.JPG)';...
'*.jpeg','jpeg Files (*.jpeg)';...
'*.JPEG','JPEG Files (*.JPEG)';...
'*.img','img Files (*.img)';...
'*.IMG','IMG Files (*.IMG)';...
'*.tif','tif Files (*.tif)';...
'*.TIF','TIF Files (*.TIF)';...
'*.tiff','tiff Files (*.tiff)';...
'*.TIFF','TIFF Files (*.TIFF)'},...
'MultiSelect', 'on');
% Error check - if no filename there is an error
if isequal(filename,0)
error(' Load Error: No files selected! Load cancelled.')
else
end
% launch the figure box
figure
imagefilename = imgfile(filename,pathname);
c = imread(imagefilename);
image(c)
axis image
grid on
I'm certain the problem is the bit after I launch the figure, but I have tried several different combinations of syntax, but just cant get it right.
Could anyone offer some wizardry to put me right please?
Best regards,
10B.

 Réponse acceptée

I think you want fullfile() instead of imgfile().
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)
myImage = imread(fullFileName);
imshow(myImage);

6 commentaires

Hello Image Analyser,
With a name like that - you must be the right person for this query!
Your code works correctly, thanks, but there is a small problem. In my code, I used this to get the image plotted with pixel numbers and a grid over the figure:
axis image
grid on
but when I add that to your code, even corrected to myImage thus:
axis myImage
grid on
it doesn't do what it did previously i.e. grid/numbers. Is this because the command imshow has been used? Is there another fix for this?
Regards,
10B.
myImage is not one of the arguments to axis - it probably ignored it. I don't know why you changed it. What do you want to do? Do you want to show axes and tick marks and labels? If so do
axis on;
Do you want to mouse around and show the location and gray level in a status panel? If so, use impixelinfo().
10B
10B le 10 Août 2015
Ha - so simple! Thanks again. What I want to do is have the gridlines over the top of the image, so I can visually create the extents for a bounding box (tried to code a bounding box but couldn't get that to work either!). For example, I can look at the grid take the X,Y extents and then use that to clip the image down to my area of interest. So is there a way to put the gridlines over the top?
Ive sorted for myself now! just using grid on works just fine, and I wrote the following to improve the grid colours over the image:
axis on;
grid on;
set(gca, 'xcolor', 'w', 'ycolor', 'w');
Anyway, thanks for your help and earlier code.
"grid on" sort of makes grid lines over the image but they're dotted and hard to see. If you want nice, high-contrast, solid color lines over the image, you should use line():
% Make horizontal lines every 50 rows.
hold on;
for row = 1 : 50 : rows
line([1, columns], [row, row], 'Color', 'r');
end
% Make vertical lines every 75 columns.
for col = 1 : 75 : columns
line([col, col], [1, rows], 'Color', 'r');
end
Yes, thats a good suggestion - the dotted lines aren't great TBH, which is why I put them in white. I have tried to use your last bit of code, but I'm running into errors.
I leave axis on and remove grid on like this:
fullFileName = fullfile(folder, baseFileName);
myImage = imread(fullFileName);
imshow(myImage);
axis on;
hold on;
for row = 1 : 50 : rows
line([1, columns], [row, row], 'Color', 'r');
end
% Make vertical lines every 75 columns.
for col = 1 : 75 : columns
line([col, col], [1, rows], 'Color', 'r');
end
but unfortunately, it doesn't add the solid lines over the image. I have also tried different combinations of with the grid, without the axis etc. and all no joy as it throws the following error:
"Undefined function or variable 'rows'.
Error in Untitled7 (line 27) for row = 1 : 50 : rows"
I also tried to put the rows in single quote and enclose that section in parenthesis like this:
hold on;
for row = (1 : 50 : 'rows')
line([1, columns], [row, row], 'Color', 'r');
but that simply threw another error:
"For colon operator with char operands, first and last operands must be char.
Undefined function or variable 'rows'."
The Doc Help on rows/columns doesn't really address what this problem may be - other then to say rows/columns are unassigned variables. Sorry to ask again, but do you have another insight on this problem?

Connectez-vous pour commenter.

Plus de réponses (1)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by