Error:
Subscript indices must either be real positive integers or logicals.
Hi I am doing image conversion and I get an error when checking the size of the matrix. I am confused as to why I am getting it for this particular instance with the code:
size(maleGrey)
Here is the code I am running:
male = getAllFiles('male');
% Variable Initialization
size = 250*250;
numM = length(male);
maleGrey = zeros(size,numM);
% Convert to gray scale
for i = 1:numM
rgb = imread(char(male(i)));
img = single(rgb2gray(rgb));
vec = img(:); % make it a vector of (62500,1) in size
maleGrey(:,i) = vec;
end
You made the mistake of using size as a variable when calling size=250*250. Once you do that in a workspace, the function is masked (overloaded) by the variable, and Matlab will always treat further calls to size as manipulations of the variable.
Call clear size, and the function will work as intended. Also, do not use size as name for a variable (or other function names like length or double, or zeros etc), but e.g. siz, or numberOfRows (because that's what your variable stands for).
You're overwriting (locally) Matlabs native function reference size by a variable name
% Variable Initialization
size = 250*250; % <--
Hence when you call size(maleGrey) it treats maleGrey as an index in the variable size.
Related
Idk why the code won't work. All it does is give me a blank white image. And if you don't declare a matrix before by zeros(x, y) then it works fine. Wth is wrong here?
I tried not declaring the zeros matrix before and only that works. I even tried doing img2(i,j) = img2(i,j)+img1(i,j)
function [imgOut] = scaleLoopBased(img,s)
%UNTITLED4 Summary of this function goes here
% creating a zero matrix of the given scale
[rows,columns]=size(img);
imgTemp=zeros(rows, columns);
for i=1:rows
for j=1:columns
imgTemp(i, j) = img(i, j);
end
end
imshow(imgTemp);
imgOut = imgTemp;
end
Blank white image
This is a result of your new image (of type double, what zeros creates by default) not having the same type as your original image (typically type uint8). You can fix this by initializing your new image to have the same data type as the original using the class function and passing an additional argument to zeros:
imgTemp = zeros(rows, columns, class(img));
The reason it works correctly when you don't initialize imgTemp is because MATLAB initializes the variable for you using the data type of img by default when you perform the first indexed assignment.
The imshow utility expects one of the standard image types in MATLAB. An image of type double is expected to have values spanning the range [0 1], while images of type uint8 will have values in the range [0 255]. In your example you likely have a matrix imgTemp that is of type double but has values spanning [0 255]. Another way to fix your issue is to explicitly tell imshow what value range to use for the display (since the default [0 1] doesn't work):
imshow(imgTemp, [0 255]);
Always be aware of the data type when manipulating or processing images. You may need to scale or convert back and forth, using a double type for calculations (since integers saturate) and a uint8 type for display and reading/writing to files.
I need to iterate through a large number of LinearModel fit Objects and have them stored in a logical way, preferably through an indexing method, e.g. model{x,y}. Less preferable is eval(), which I have tried and in any case is not working. I get the error Index exceeds matrix dimensions. - although the string expression works just fine outside of eval.
counter = 48;
str=strcat('model',+num2str(counter)); % Dynamic variable name
str1 = strcat(str,'=fitlm(tbl,modelspec)'); % Full string to be evaluated
eval(str1)
Ideally I wanted to do, while iterating through x
model{x,y} = fitlm(tbl,modelspec) % This is the equivalent expression
But the error I get is
"Assignment using {} is not allowed for a FitObject."
I think this is similar to this question - with no answer:
Dynamic Objects in Matlab
Managed to solve this by assigning to a cell structure.
e.g.
model = cell(3,3) % Pre-assign a cell structure
for x=1:3
for y=1:3
model{x,y} = fitlm(tbl,modelspec);
end
end
As a follow up to my question here:
What does createMask actually do? I went to the description from MathWorks here, but wan't much clear.
If you see in the answer of my question referenced above: img2(roi.createMask) = 1;, the part roi.createMask reminds me of function call, is that what we are really doing here? Calling the createMask function?
Thanks.
In the code
img = im2double(imread('cameraman.tif'));
imshow(img);
roi = imfreehand(gca);
img2 = img;
img2(roi.createMask) = 1;
imshow(img2);
roi is the handle to the object generated by imfreehand. One of the methods (~functions) available through the object (using the handle) is createMask, which can be accessed with the . operator. The method generates a type logical array of the same size as the pixel dimensions of the image. Values in the logical array are either 1 or 0 with values of 1 assigned to entries in the region corresponding to the area selected with the imfreehand operation. The operation img2(roi.createMask) =1; indexes into the image img2 (it picks elements in img2) using the positions in the logical array with value 1 and assigns those elements value 1.
I am reading in the matlab documentation that rgb2hsv will return an m-by-n-by-3 image array, yet when I call it, I get a 1-by-3 vector. Am I misunderstanding something?
Here is an sample code:
image_hsv = rgb2hsv('filepath')
and as output
image_hsv =
0.7108 0.3696 92.0000
You cannot call rgb2hsv on a filepath - it must be called on a MATLAB image matrix. Try:
image_rgb = imread('filepath'); % load the image array to MATLAB workspace
image_hsv = rgb2hsv(image_rgb); % convert this array to hsv
You can see these matrices with:
>> whos image* % display all variables whose name begins with 'image'
Name Size Bytes Class Attributes
image_hsv 480x640x3 7372800 double
image_rgb 480x640x3 921600 uint8
What your original code was doing was converting your filepath string to ascii numbers, taking the first three values of this array as RGB values and converting these to HSV.
NOTE: This example highlights dangers with MATLAB's weak typing system, where data types are silently converted from one type to another. Also maybe a lack of correct input checking to the rgb2hsv function.
Matlab is giving me the error, "Subscripted assignment dimension mismatch" however I don't think there should be an issue. The code is below but basically I have a temp matrix that mimics the dimensions of another matrix, testData (actually a subset of it). I can assign the output of imread to the temp matrix but not to a subset of testData that has the same dimensions. I can even use the size function to prove they are the same dimensions yet one works and one doesn't. So I set temp = imread and then testData = temp and it works. But why should I have to do that?
fileNames = dir('Testing\*.pgm');
numFiles = size(fileNames, 1);
testData = zeros(32257, numFiles);
temp = zeros(32256, 1);
for i = 1 : numFiles,
fileName = fileNames(i).name;
% Extracts some info from the file's name and stores it in the first row
testData(1, i) = str2double(fileName(6:7));
% Here temp has the same dimensions as testData(2:end, i)
% yet testData(2:end, i) = imread(fileName) doesn't work
% however it works if I use temp as a "middleman" variable
temp(:) = imread(fileName);
testData(2:end, i) = temp(:);
end
If the file that you're reading is a color image, imread returns an MxNx3 array. You can't assign a 3D array to a 1D vector without reshaping it, even if it contains the same number of elements. That's probably why you get the error when you try to assign the output of imread directly to testData. However, when you use an intermediate variable and collapse it into a column vector, the assignment works because now you're assigning a 1D vector to another 1D vector of equal size.
If you don't want to use an additional step, try this
testData(2:end,i)=reshape(imread(fileName),32256,1);