How do I assign a number to an image in a cell array?
Mostrar comentarios más antiguos
Hi guys!
I have a 1x40 cell array with 40 images ({'1.jpg'},{'2.jpg'}...and so forth). And I want to assign certain numbers to certain images because these numbers stand for certain chracteristics of the images (gender, identity, facial expression....). I need these numbers for a randomization.
So, does somebody know how to assign numbers to the images of a cell array?
Thanks in advance!
Best,
R
1 comentario
KSSV
el 8 de Oct. de 2020
Simple..you define the numbers in a 1*40 array. Where each index corresponds to respetive image.
Respuestas (1)
If you have a number of characteristics and don't want to have some elaborate encoding for how these are all specified I would put the data into a table with a column for the images (your current cell array) and then additional columns for each characteristic of interest. You could then easily access the data according to the rows that have the charactertics that match the ones of interest. Type doc table on the command line for details about how to create a table. Here is an overview of using tables https://www.mathworks.com/help/matlab/matlab_prog/advantages-of-using-tables.html
Another way to do this, if you just had a single characteristic encoded as a number to go with each image would be just to make a vector with the same number of elements as your cell array and store the characteristics in the elements of this vector. Lets call the vector of characteristics, imageCharacteristics, and the cell array of image, images. Then if you wanted for example to set the imageCharacteristics corresponding to the 4th image in your cell array to a value of 232 (just making up something for the example) you could assign
imageCharacteristics(4) = 232
later if you wanted the image and its characteristics you could open up
image = images{4}
imageCharacteristic = imageCharacteristic(4)
Still I would recommend putting the data into a table as a good general approach
8 comentarios
Rupert Steinmeier
el 13 de Oct. de 2020
Jon
el 13 de Oct. de 2020
I'm sorry, but I'm not understanding your follow up question. Can you please try to explain what you are doing further. Also, I'm wondering about the values you have in your columns. It looks like they are all binary (true/false ie 0 1). I can somewhat imagine this makes sense for gender, assuming for example that true means male and false means not male. I have a harder time understanding how age would be binary exce[t something like over 50 years old, not over 50 years old. Is this really what you are intending? Also what is the meaning of a 1 or 0 in the column called identity, or can that take on other values than 1 and 0 (you just show 1 for both rows).
Rupert Steinmeier
el 13 de Oct. de 2020
Jon
el 14 de Oct. de 2020
Sorry, I haven't had a chance to look deeper into this. Busy at moment but maybe later this week. When you say you need an equal number of con-con, incon-con etc do they actually have to be equal, or just uniformly distributed, like the age, gender etc?
Rupert Steinmeier
el 15 de Oct. de 2020
It's not obvious to me how you would generate "random" sequences of con,incon pairings, such that you had exactly the same number of each. The difficulty that I see is that you can't just have a bucket of say 25 con-incon, 25 con-con, 25 incon-con and 25 incon-incon possibilities and then just randomly draw from the bucket until they are exhausted. The problem in doing this is that certain pairing can only occur following other pairings. For example if on the nth presentation we had a con-con pairing then on the n+1 presentation we could only have a con-con or a incon-con (Here I assume your terminology is to list the current presentation type followed by the previous presentation type).
So I don't think I can help you much on developing your actual methodology. Once you get to the point that you have defined exactly what you want to do (you more or less have an algorithm) but are having problems determining how to implement that in MATLAB I (and I'm sure many other contributors) could help you further.
Rupert Steinmeier
el 17 de Oct. de 2020
I've been away from the computer for awhile. Maybe you've already solved your problem. If not here are some further ideas.
If you have the images and their features in a table, let's call it T, you can draw a random row from the table using
sample = T(randi(numImages),:) % numSample is total number of images
note that sample is itself a table with just one row but you can access the various features using dot notation.
Suppose also that you had already saved your previous sample as lastSample (also a one row table)
So if your column names (variable names ) were imFile, id, con, gender, expression, age and you wanted to make sure that the sample was a different subject, and gender than the last one (just for example) you could then check
isGood = sample.id ~= lastSample.id && sample.gender~=lastSample.gender
if its good you can get the name of the image file and present it and then set your lastSample equal to your current sample
So I guess you would have a loop something like
% you will have to do some initialization here before entering loop
% ...
numPresented = 0
lastSample = T(randi(numImages),:) % random last sample just to initialize
% enter loop
while numPresented < numRequired
% draw a sample
sample = T(randi(numImages),:) % numSample is total number of images
% check criteria
% the ones below are just for illustration, you can set what you need in a similar way
isGood = sample.id ~= lastSample.id && sample.gender~=lastSample.gender
if isGood
image = sample.imFile; % and then show it to the user
% save the last sample for comparison
lastSample = sample
numPresented = numPresented + 1
end
end
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!