I have a quick question. I'm trying to compute an images 2D gradient using the gradient() function in MATLAB, but its not working. Specifically, here's my code (The image I'm using is grayscale):
im = imread('C:\yosemite1.bmp');
g = FindImageGradients(im);
I get the following error:
??? Error using ==> rdivide Integers
can only be combined with integers of
the same class, or scalar doubles.
Error in ==> gradient at 75
g(2:n-1,:) = (f(3:n,:)-f(1:n-2,:))./h(:,ones(p,1));
Any clues on how to solve this ?
Your image data is probably being read as integers in the range [0,255] (for 8 bit per color channel), so the type of im is uint8 or other int type. Try converting it to single or double:
g = FindImageGradients(single(im));
Related
I am reading an image with the following command:
lefty = imread('sintra2.JPG');
and imshow(); gives me a good resault. but If I try to use:
lefty = double(imread('sintra2.JPG'));
imshow() gives me a white image. I am working with a relatively big image shared here. Is there a connection?
How do I convert to double if at all it is necessary? I was told it is better to work with double when working on image processing and computer vision in MATLAB.
When you read the image, its type was uint8 and thus lefty contained the values from 0 to 255 (28 = 256). When you used double, it converted the class from uint8 to double but the values remained same i.e 0-255.
You need im2double here which not only converts the values to double precision but also rescales the values in the range 0-1 by dividing all the entries of the input by the maximum possible value of the input data type. So in your case, as the input data type is uint8 whose maximum possible value is 255, therefore all the values will be divided by 255. Note that it is possible that the maximum value in your image data may not be 255 but since the maximum possible value of uint8 is 255, so all the values will be divided by 255.
So the following is what you're looking for:
lefty = imread('sintra2.JPG');
imshow(lefty)
figure
imshow(im2double(lefty))
The problem is with the data type that imshow requires. If the image is of type int, its range should be between 0 and 255. If it is double – between 0.0 and 1.0. Try this:
lefty = imread('sintra2.JPG');
imshow(lefty)
or:
lefty = imread('sintra2.JPG');
imshow(double(lefty)/double(max(lefty(:))))
I am trying to use imhist to display the histogram of a uint8 .jpg, however I am getting this error:
Error using imhist Expected input number 1, I or X, to be
two-dimensional.
Error in imhist>parse_inputs (line 278) validateattributes(a,
{'double','uint8','int8','logical','uint16','int16','single','uint32',
'int32'}, ...
Error in imhist (line 60) [a, n, isScaled, top, map] =
parse_inputs(varargin{:});
Here is my image information:
whos f Name Size Bytes Class
Attributes
f 2988x5312x3 47616768 uint8
Do I need to convert my image to another data class? I would appreciate any help on this.
Thanks!
The cause of error is because your image is RGB and imhist does not deal with that. To work around this you can either use a single channel:
imhist(YourImage(:,:,Channel));
or convert from RGB to grayscale:
imhist(rgb2gray(YourImage));
That should work fine now.
I have a problem in Matrix type conversion.
So, I want to extract the SIFT features from an image by using VLFEAT function " vl_covdet"
Here is the detail:
Input images = <141x142x3 uint8>
And because vl_covdet only can read 1 channel and an image with type of single , I give R channel of my input image to vl_covdet:
R_input_Images = Input images(:,:,1) <141x142 uint8>
R_Single_Images= im2single(R_input_Images);
[frames, descrs,info] = vl_covdet(R_Single_Images,'Method','multiscalehessian','EstimateAffineShape', false,'EstimateOrientation', true, 'DoubleImage', false, 'Verbose');
And now, I got features
descrs = <128x240 single> which values are ranging from 0 - 0.368
But to compute BoW, I have to use K-Means clustering from VLFEAT ("vl_hikmeans") which require uint8 input type.
descrs must be of class UINT8.
So then I tried to convert it again into uint8
descrs=uint8(descrs);
Now
descrs = <128x240 uint8> **AND ALL THE VALUES BECOME 0**.
What I have to do now??
values are ranging from 0 - 0.368
Well, if you round those to integer, it's no surprise they become zeros.
Since an image in floating-point format has range 0-1, and in uint8 format has range 0-255, try
descrs = uint8(descrs * 255);
I am trying to create bit scales images. I am getting an error with this code. What is wrong?
clc
clear all
a=imread('image.tif');%read file
[row col]=size(a);%row of image and column of image
b=zeros(row,col,8);%3D 0 matrix
for k=1:8%position of bit
for i=1:row%for every row
for j=1:col%for every column
bits = de2bi(a(i,j));
b(i,j,k)=bits(k);
end%endFor
end%endFor
end%endFor
for k=1:8
subplot(3,3,k);
imshow(b(:,:,k));
title(strcat(num2str(k),'. bit'));
end%endFor
ERROR:
??? Attempted to access bits(2); index out of bounds because numel(bits)=1.
Error in ==> soru1 at 13
b(i,j,k)=bits(k);
At this line of code:
bits = de2bi(a(i,j));
You are calling de2bi on the value of the pixel at "i,j". Presuming that the image you are opening is of type uint8, the value of a(i,j) can be anywhere between 0 and 255. If these values are 0 or 1, the output of de2bi as you call it is just "0" or "1" - that is, it only has one element in it, and you cannot access the second, non-existent element.
To correct this, you need to force the size of the output of de2bi to be the size you require, which can be done using a second input, like this:
bits = de2bi(a(i,j),8)
In fact the loop isn't required since de2bi, like most MATLAB functions, can handle vectors or matrices as input, not just single numbers:
a=imread('image.tif');
b=de2bi(a);
b = reshape(de2bi,[size(a),8]);
I've got two datasets, which I load from a CSV file, and split them into X and T:
X (3x5000) double
T (1x5000) double
I'm trying to configure this function, but I can't
http://www.mathworks.co.uk/help/toolbox/nnet/ref/layrecnet.html
X has three features and 5000 examples. T has one feature and 5000 examples. For an example the target is feature 1 20 steps ahead. So basically X(1,21) == T(1).
[X,T] = simpleseries_dataset;
This works perfectly, in this case, I have 1x100, 1x100.
If I use my own data set, however, I get this:
X = data(:,1:3)';
T = data(:,4)';
net = layrecnet(1:2,10);
[Xs,Xi,Ai,Ts] = preparets(net,X,T);
??? Index exceeds matrix dimensions.
Error in ==> preparets at 273
ti = tt(:,FBS+((1-net.numLayerDelays):0));
I don't understand, what am I doing wrong?
UPDATE
I've noticed that my data set is T (1x5000) double while the example dataset is T (1x100) cell. What's the difference between double and cell?
I solved it by:
X = num2cell(X);
T = num2cell(T);
I have no idea why; it must be MATLAB syntax...
You can solve it by:
P = con2seq(p);
T = con2seq(t);
.....% for example
p=(1 2;3 4;5 6);
t=(3;7;11);
.....%now
P = con2seq(p);
T = con2seq(t);
net = elmannet(1:2,12);
[Xs,Xi,Ai,Ts] = preparets(net,P,T);
net = train(net,Xs,Ts,Xi,Ai);
view(net)
Y = net(Xs,Xi,Ai);
perf = perform(net,Ts,Y);
To clarify "(...)it must be MATLAB syntax...":
The problem here is the conversion from double to cell arrays. Matlab does not do this automatically since a cell can contain any type of value as mentioned here: http://www.mathworks.com/help/matlab/matlab_prog/what-is-a-cell-array.html
So, as mentioned in your answer, you can either convert your double arrays to cell arrays using num2cell() or you can allocate X and T as cell arrays from the very beginning using cell() and then copying your double values into them. This explicit type cast is necessary because preparets expects cell arrays as input, much like many of the plot functions in the ANN package.