Image processing using MATLABR2010a - matlab

I tried to read an image and display it but i faced an error and i didn't understand it.can any one help me please, note that i use MATLAB R2010a, and the display below is the type of error.
>> imread('tas.jpg');
>> imshow('tas.jpg');
??? Attempt to call constructor image with incorrect letter case.
**Error in ==> basicImageDisplay at 9
hh = image(xdata,ydata,cdata, ...
Error in ==> imshow at 246
hh = basicImageDisplay(fig_handle,ax_handle,...**

I = imread('tas.jpg');
imshow(I);
The imread function reads the file and converts it to a RGB matrix of pixels. This is stored on the variable I. Then, you can call imshow passing this RGB matrix as a parameter ;)
edit you can call imshow with the filename as well, but it's not that useful because it does not return the matrix you will later use for processing. And as the error is thrown only on imshow, I'm guessing the imread function, for some reason, is working.
If not, just double check if the image is on the actual directory or in a directory on the path, or if it is not corrupted.

This might be the reason (from the below thread):
Reason: "current directory folder name is match with built-in function in matlab library and gives the error - Attempt to call constructor image with incorrect letter case".
Solution: change the folder name with unique name.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256922

Related

Code given in MATLAB documentation for montage function returning errors

I'm trying to display a montage of images in MATLAB, where each image is an N x M array and K images are stored as an N x M x K array.
MATLAB returns the errors:
Error using images.internal.imageDisplayValidateParams>validateCData (line 115)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 27)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 78)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 240)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Error in montage (line 152)
hh = imshow(bigImage, displayRange,parentArgs{:});
These exact errors persist when I copy-paste and run the code supplied the MATLAB documentation for the montage function, included here:
img1 = imread('AT3_1m4_01.tif');
img2 = imread('AT3_1m4_02.tif');
img3 = imread('AT3_1m4_03.tif');
img4 = imread('AT3_1m4_04.tif');
multi = cat(3,img1,img2,img3,img4);
montage(multi);
This code is supposed to create a montage from an N x M x K array in the same manner as the original code I am troubleshooting, and fails to in the same manner.
Does anyone else receive these errors from this code? Can someone tell me how to adjust this code to produce a montage of images as demonstrated in the documentation example?
My guess is that this is a version error. Are you running Matlab 2018a? That is the version which the documentation is for. I would fancy a guess that an older version of Matlab used a different interface.
I would try typing
help montage
in your Matlab. Perhaps there are some addition tips there.
The documentation says
montage(I) displays all the frames of a multiframe image array I. A
multiframe image array can be a sequence of binary, grayscale, or
truecolor images. A binary or grayscale image sequence must be an
M-by-N-by-K or an M-by-N-by-1-by-K array. A truecolor image sequence
must be an M-by-N-by-3-by-K array.
however the M-by-N-by-K does not seem to work. But the M-by-N-by-1-by-K does.
Therefore
multi = cat(4,img1,img2,img3,img4);

regionprops() is giving an error in matlab

I'm using regionprops() but I'm getting an error with the following code:
J = imread('E:\Canopy New Exp\Data Set\input.jpg');
I = rgb2gray(J);
BW = regionprops(J,'basic');
stats = regionprops('table',BW,'Centroid',...
'MajorAxisLength','MinorAxisLength');
centers = stats.Centroid;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
hold on;
viscircles(centers,radii);
hold off;
But I'm getting the following error:
Error using regionprops
Expected input number 1, L, to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64
Instead its type was char.
Error in regionprops (line 142)
validateattributes(L, {'numeric'}, ...
Error in Untitled (line 8)
stats = regionprops('table',BW,'Centroid',...
Any suggestions?
Thanks in advance!
You are doing regionprops twice, and the second time with 'table' as the first parameter. regionprops expects an image (black and white, connected components, or labelled) as the first parameter, so that is why you are getting the error type char.
Instead feed in a black and white (binary) image to one call of regionprops, and that should be done:
thresh = graythresh(I); % get a threshold (you could just pick one)
I_BW = im2bw(I,thresh); % make the image binary with the given threshold
stats = regionprops(I_BW,'basic'); % do regionprops on the thresholded image
You can also do regionprops with 2 image parameters, one to show the regions in the other, so instead of the regionprops call above, you could possibly try:
stats = regionprops(I_BW, J, 'basic');
regionprops outputs an object so in the third line of the above code sample you call it on J, an image, which is fine and returns an appropriate object BW. But then in the following line you call it again on the BW object and that's where the error comes from. It isn't meaningful to call it twice on successive objects, but it's more likely that that wasn't your intention and you meant to binarise the image first with im2bw.
When you read the error messages output by matlab be aware that the bottom line is the line in your code where the error occurred. If you are supplying the wrong kind of input to one of matlab's builtin functions (this is by far the most common kind of error in my own experience) then it won't be until you've gone deeper into matlab's internal functions that the error manifests.
So reading the error report from the bottom upwards you go deeper into the call stack until the top line, which is the 'actual' error. That top line gives you the cause of the conflict, which is half the story. You can then take that half back to the line of your code to see why it happened and how to fix it.
You're probably feeding it a RGB array NxMx3. regionprops takes a NxM binary array according to the documentation.

Error Numeric or logical matrix required for image CData

While reading a normal image file it is showing this error.
I can use the imread command in command window directly but not in the M-file.
I had used the imread function like this:
BW=imread('C:\Users\parikh5555\Desktop\books\matlab image\1.jpg')
The error I'm getting is:
??? Error using ==> image
Error using ==> image
Numeric or logical matrix required for image CData
That error is most likely due to the fact that you are trying to use image as a variable when it is an actual function: http://www.mathworks.com/help/matlab/ref/image.html.
Specifically, you probably ran some code that used image as a variable, that variable is now cleared from the MATLAB workspace, and when you are trying to reuse that same M-file script after, it thus spits out that error because image is no longer being shadowed as a variable and now it is actually calling the function.
That error has nothing to do with imread. As such, you should go through your M file and make sure you have no variables called image and rename them to something else to prevent shadowing over the function unintentionally.
BTW, I'm going to close your question as it's due to a simple typographical error. Please take no offence.

The name 'Units' is not an accessible property for an instance of class 'lineseries' - in getframe

How to capture plot image into memory? I am trying to use getframe, but failing
>> plot(h)
>> myhandle=plot(h)
myhandle =
174.0044
>> myframe=getframe(myhandle)
Error using graph2d.lineseries/get
The name 'Units' is not an accessible property for an instance of class 'lineseries'.
Error in getframe>Local_getRectanglesOfInterest (line 138)
if ~strcmpi(get(h, 'Units'), 'Pixels')
Error in getframe (line 56)
[offsetRect, absoluteRect, figPos, figOuterPos] = ...
From the MATLAB doc found here: http://www.mathworks.com/help/matlab/ref/getframe.html
F = getframe(h) gets a frame from the figure or axes identified by handle h.
When I do handle=plot(......), then type get(handle), and look at the type, you will see it is indeed a line series object, not an axis or figure.
To do what you want, use getframe(gca) or getframe(gcf). gca/gcf = getCurrentAxes/Figure respectively.
To be honest though, unless you are doing a semi complicated GUI or something like that, getframe really doesn't usually need any input arguments.

BMP2AVI program in matlab

HI
I wrote a program that use to work (swear to god) and has stopped from working. this code takes a series of BMPs and convert them into avi file. this is the code:
path4avi='C:/FadeOutMask/'; %dont forget the '/' in the end of the path
pathOfFrames='C:/FadeOutMask/';
NumberOfFiles=1;
NumberOfFrames=10;
%1:1:(NumberOfFiles)
for i=0:1:(NumberOfFiles-1)
FileName=strcat(path4avi,'FadeMaskAsael',int2str(i),'.avi') %the generated file
aviobj = avifile(FileName,'compression','None');
aviobj.fps=10;
for j=0:1:(NumberOfFrames-1)
Frame=strcat(pathOfFrames,'MaskFade',int2str(i*10+j),'.bmp') %not a good name for thedirectory
[Fa,map]=imread(Frame);
imshow(Fa,map);
F=getframe();
aviobj=addframe(aviobj,F)
end
aviobj=close(aviobj);
end
And this is the error I get:
??? Error using ==> checkDisplayRange at 22
HIGH must be greater than LOW.
Error in ==> imageDisplayValidateParams at 57
common_args.DisplayRange = checkDisplayRange(common_args.DisplayRange,mfilename);
Error in ==> imageDisplayParseInputs at 79
common_args = imageDisplayValidateParams(common_args);
Error in ==> imshow at 199
[common_args,specific_args] = ...
Error in ==> ConverterDosenWorkd at 19
imshow(Fa,map);
for some reason I cant put it as code segments. sorry
thank you
Ariel
Are the BMP images indexed? I think the map parameter only applies to images with indexed color maps.
The only way I am able to reproduce the error that you get is when map is a two-element vector where the first element is greater than the second. Note first that the function IMSHOW can be called with the following syntax:
imshow(I,[low high]);
In which I is a grayscale image and low and high specify the display range for the pixel intensities. The extra argument is ignored when I is an RGB image, but even then the value of high must be greater than the value of low or an error is thrown (the one you see above).
What's confusing is why map would be a two-element vector. When loading an image with IMREAD, the map output will either be empty (if the image is not an indexed image) or it will be an N-by-3 color map. I can't think of a situation where the built-in IMREAD would return a map argument with just 2 elements.
Based on the fact that you said it was working, and now suddenly isn't, I would suggest first checking to see if you have inadvertently created an m-file somewhere with the name imread. Doing so could cause that new imread function to be called instead of the built-in one, giving you different outputs than you expect.