I want to read a training image set for SVM training. This is the code
%Location of the image.
Class1 = 'Training/11';
% load the dataset
dirList = dir(fullfile(Class1,'*.ppm'));
%dirList
files={dirList.name}';
The type of files that I got is of type cell. How I can access those images to perform something, like show it and do feature extraction??
When I tried to show it:
figure, imshow(files)
I got this error
Error using imageDisplayValidateParams
Expected input number 1, I, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64,
logical
Instead its type was cell.
Error in imageDisplayValidateParams (line 12)
validateattributes(common_args.CData, {'numeric','logical'},...
Error in imageDisplayParseInputs (line 79)
common_args = imageDisplayValidateParams(common_args);
Error in imshow (line 220)
[common_args,specific_args] = ...
Do you know how to access and do some processing of these images in files?
MY FOLDER DIRECTORY!!
MY DIRECTORY
Inside my training Folder
First off, imshow requires an actual image as its input. You are specifying a cell array of strings. On top of that, you can only show one image at a time. Try accessing individual cell elements instead and using those to read in an image and display them on the screen.
im1 = imread(files{1}); % Read in first image
imshow(im1); % Show the first image
figure;
im2 = imread(files{2}); % Read in second image
imshow(im2); % Show the second image
If you want to display all of them, you could try using a combination of imshow and subplot.
Let's say you had 9 images, and wanted to organize them in a 3 x 3 grid. You could do something like:
figure;
for i = 1 : 9
subplot(3, 3, i);
im = imread(files{i});
imshow(im);
end
Now for performing feature extraction, my suggestion is that you take a look at the Computer Vision toolbox that is accompanied with MATLAB. There is a whole suite of tools that performs feature extraction for you. Things like MSER, SURF, HOG and methods to match keypoints between pairs of images.
Check this link out: http://www.mathworks.com/products/computer-vision/code-examples.html
Related
I already made the image registration, and now I want to apply the registered image in rgb because I need to extract the countourr. As the imregister just work with grayscale images I converted my image to grayscale,but now I canĀ“t find the intensity value of the contour to find the contour indexes. Wat kind of algorithm does imregister applies to the image, tochange the intensity value of the pixels? Or there is another way to go back to the rgb image to extract the inicial countour in the registered image? Does anyone have any sugestion?
There is my matlab code :
% Algorithm for image validation
% Open the two images which will be compared
name2=input('Image name ( automated segmentation) ','s');
img_automated=imread(name2,'png');
figure (1), imshow(img_automated), title('Image automated')
name=input('Image name ( manual segmentation) ','s');
img_manual=imread(name,'png');
img_manual_gray=rgb2gray(img_manual);
%img_manual_gray=img_manual(:,:,3);
figure (2), imshow (img_manual),title('Image manual')
img_automated_gray=rgb2gray(img_automated);
%img_automated_gray=img_automated(:,:,3);
%img_double=im2double(img_automated_gray);
figure (3), imshow (img_automated_gray), title (' Image converted to double ');
%img_automated_gray2=rgb2gray(img_automated);
% View images side by side
figure (6), imshowpair(img_manual,img_automated_eq,'diff')
title('Images overlap')
%% Configure parameters in imreconfig
[optimizer,metric]=imregconfig('Multimodal');
%% Default registration
registered=imregister(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
%tform = imregtform(img_automated,img_manual,imref2d,'affine',optimizer,metric)
figure(7), imshowpair(registered, img_manual_gray,'falsecolor'); title('Default registration');
figure(8), imshowpair(registered, img_manual_gray,'montage','Scaling','independent'); title('Default registration');
figure(9), imshow(registered);
C = imfuse(registered,img_automated);
figure(21);imshow(C);
%%%%%%%%%%%%%%% Here%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%I tried this process to recover the transformation applied in the registered image,and them aplly this in the initial automated rgb image,but B isn't the same as the registered image. Any suggestions??
tform = imregtform(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
B = imwarp(img_automated,tform);
figure(22);imshow(B);
Links to the images:
https://www.dropbox.com/s/xbanupnpjaaurj5/manual.png?dl=0 (manual- rgb)
https://www.dropbox.com/s/6fkwi3xbicwzonz/registered%20image.png?dl=0
You are on the right track with your use of imregtform. The missing piece is that imwarp, by default, chooses an output grid with the same resolution as the input grid with limits big enough to capture the entire output image.
In most registration cases, this is not what you want. What you want, is to specify the 'OutputView' Name/Value to specify the resolution and limits that you want in the output grid. In many registration use cases, this is the resolution/limits of the fixed image:
tform = imregtform(img_automated_gray,img_manual_gray,'rigid',optimizer,metric);
B = imwarp(img_automated,tform,'OutputView',imref2d(size(img_manual_gray)));
figure(22);imshow(B);
I am creating a GUI containing an image using the following code:
try
Imagenamehere = imread('Imagenamehere.jpg');
axes(handles.Logo)
image(Imagenamehere)
set(gca,'xtick',[],'ytick',[])
catch
msgbox('Please download all contents from the zipped file into working directory.')
end
The image shows up but for some reason is completely coloured blue as if put through a blue filter. I don't think it would be wise to upload the image but it is a simple logo coloured black and white.
Anyone know what could be causing this?
Check the size, type (probably uint8) and range of your image. It sounds like for some reason your images are being displayed with colormap as jet (the default), and possibly also that your range is not what MATLAB expects (e.g. 0 to 1 not 0 to 255), resulting in all your values being relatively low (blue on the jet colormap).
"black and white" is just one way of interpreting an image file which contains only two colors. MATLAB makes several assumptions when you pass data into a display function like image. If you don't specify colormap and image data range, it will make a guess based off things like data type.
One possibility is that your logo file is an indexed image. In these cases you need to do:
[Imagenamehere map] = imread('Imagenamehere.jpg');
colormap(map);
I'm using the Images package and I want to load in an image and get the mean and standard deviation of the pixels in r image.
I tried:
using Images, Color, FixedPointNumbers, ImageView, Testimages
img = testimage("mandril")
mean(data(img))
The mean worked fine, in fact in IJulia it displays the mean color of the image. However, when I tried to get the standard deviation of the image, I get:
std(data(img))
`varm` has no method matching varm(::Image{RGB{UfixedBase{Uint8,8}},2,Array{RGB{UfixedBase{Uint8,8}},2}}, ::RGB{Float32})
while loading In[66], in expression starting on line 1
in var at statistics.jl:162
How would one go about getting the standard deviation of the image?
You can use red, green and blue to extract the components of image,
and compute the standard deviation with std on the resulting matrices.
using Images
using TestImages
using Color
img = testimage("mandril")
data(img)
RGB(
std(red(img)),
std(green(img)),
std(blue(img))
)
# RGB{Float32}(0.22030124f0,0.18964756f0,0.24422659f0)
You could also build a 3-dimensional array with all the data,
with separate,
and apply std on the three 2-dimensional slices corresponding
to the three components, with mapslices.
vec( mapslices( std, separate(img), [1,2] ) )
If you want to figure out why mean(data(img)) works
and std(data(img)) does not (at least, currently),
you can check which method is called
with #which, and read the corresponding code, with #less.
#which mean(data(img))
# mean(A::AbstractArray{T,N}) at statistics.jl:17
#which std(data(img))
# std(A::AbstractArray{T,N}) at statistics.jl:204
#less mean(data(img))
#less std(data(img))
There are (at least) two problems: first, sqrt is not defined for colours,
second, some of the code in std assumes that the mean is a Number.
I'm currently creating my figures in matlab to embed themvia latex into a pdf for later printing. I save the figures and save them via the script export_fig! Now I wonder which is the best way to go:
Which size of the matlab figure window to chose
Which -m option to take for the script? It will change the resolution and the size of the image...
I'm wondering about those points in regards to the following two points:
When chosing the figure-size bigger, there are more tickmarks shown and the single point markers are better visible
When using a small figure and using a big -m option, I still have only some tickmarks
When I generate a image which is quite huge (e.g. resolution 300 and still 2000*2000px) and than embed it into the document: Does this than look ugly? Will this be embedded in a nice scaling mode or is it the same ugliness as if you upload a 1000*1000px image onto a homepage and embed it via the widht and height tags in html -> the browser displays it quite ugly because the browser doesn't do a real resize. So it looks unsharp and ugly.
Thanks in advance!
The MATLAB plots are internally described as vector graphics, and PDF files are also described using vector graphics. Rendering the plot to a raster format is a bad idea, because you end up having to choose resolution and end up with bigger files.
Just save the plot to EPS format, which can be directly embedded into a PDF file using latex. I usually save my MATLAB plots for publication using:
saveas(gcf, 'plot.eps', 'epsc');
and embed them directly into my latex file using:
\includegraphics[width=0.7\linewidth]{plot.eps}
Then, you only need to choose the proportion of the line the image is to take (in this case, 70%).
Edit: IrfanView and others (XnView) don't display EPS very well. You can open them in Adobe Illustrator to get a better preview of what it looks like. I always insert my plots this way and they always look exactly the same in the PDF as in MATLAB.
One bonus you also get with EPS is that you can actually specify a font size so that the text is readable even when you resize the image in the document.
As for the number of ticks, you can look at the axes properties in the MATLAB documentation. In particular, the XTick and YTick properties are very useful manually controlling how many ticks appear no matter what the window resolution is.
Edit (again): If you render the image to a raster format (such as PNG), it is preferable to choose the exact same resolution as the one used in the document. Rendering a large image (by using a big window size) and making it small in the PDF will yield bad results mainly because the size of the text will scale directly with the size of the image. Rendering a small image will obviously make for a very bad effect because of stretching.
That is why you should use a vector image format. However, the default MATLAB settings for figures produce some of the same problems as raster images: text size is not specified as a font size and the number of ticks varies with the window size.
To produce optimal plots in the final render, follow the given steps:
Set the figure's font size to a decent setting (e.g. 11pt)
Render the plot
Decide on number of ticks to get a good effect and set the ticks manually
Render the image to color EPS
In MATLAB code, this should look somewhat like the following:
function [] = nice_figure ( render )
%
% invisible figure, good for batch renders.
f = figure('Visible', 'Off');
% make plots look nice in output PDF.
set(f, ...
'DefaultAxesFontSize', 11, ...
'DefaultAxesLineWidth', 0.7, ...
'DefaultLineLineWidth', 0.8, ...
'DefaultPatchLineWidth', 0.7);
% actual plot to render.
a = axes('Parent', f);
% show whatever it is we need to show.
render(a);
% save file.
saveas(f, 'plot.eps', 'epsc');
% collect garbarge.
close(f);
end
Then, you can draw some fancy plot using:
function [] = some_line_plot ( a )
%
% render data.
x = -3 : 0.001 : +3;
y = expm1(x) - x - x.^2;
plot(a, x, y, 'g:');
title('f(x)=e^x-1-x-x^2');
xlabel('x');
ylabel('f(x)');
% force use of 'n' ticks.
n = 5;
xlimit = get(a, 'XLim');
ylimit = get(a, 'YLim');
xticks = linspace(xlimit(1), xlimit(2), n);
yticks = linspace(ylimit(1), ylimit(2), n);
set(a, 'XTick', xticks);
set(a, 'YTick', yticks);
end
And render the final output using:
nice_figure(#some_line_plot);
With such code, you don't need to worry about the window size at all. Notice that I haven't even showed the window for you to play with its size. Using this code, I always get beautiful output and small EPS and PDF file sizes (much smaller than when using PNG).
The only thing this solution does not address is adding more ticks when the plot is made larger in the latex code, but that can't be done anyways.
I want to switch back and forth between two images, like blinking: 1 second for the first image and one second for second image.
I'm not totally sure of what you want to do (specifically what type of images you are trying to display), but here's some sample code that may do what you want:
image1 = imread('cameraman.tif'); % Load a test image
image2 = imread('circles.png'); % Load another test image
hAxes = gca; % Get a handle to the current axes
for iLoop = 1:5, % Loop five times
imshow(image1,'Parent',hAxes);
pause(1);
imshow(image2,'Parent',hAxes);
pause(1);
end
I used the general function IMSHOW, but this sometimes changes other properties of the figure/axes and that may not be to your liking (since you mention adding this to an existing GUI). You may want to use the IMAGE function instead. Also, instead of the for loop you could use a while loop that stops switching images when a condition is met (such as a button press).
How are your images stored in Matlab? As a matlab movie or a 3 or 4 dimensional matrix depending on if the images are color or grayscale. Also, if you have the image processing toolbox, implay and immovie. Another option assuming that your images are in a mxnx3xk (rgb color) or a mxnxk (gray scale) matrix. Then the following should work. Assuming the following
Img - matrix storing image data either with dimensions mxnx3xk
or mxnxk
handles.imageAxes -
handle for the axis you want to
display the image (set the tag of the
axis to imageAxes in GUIDE)
Now you can loop through Img
for i=1:k
% display the i^th image use `Img(:,:,i)` for a gray scale stack
image(Img(:,:,:,i),'parent',handles.imageAxes);
pause(1) % pause one second
end
that's it.