I want to capture 100 images from my webcam and then store them in a structure. I'm trying to do it like this but i'm getting the error, 'subscripted assignment dimensions mismatch'.
The code is this:
sep_images=struct('images',[]);
vid=videoinput('winvideo',1,'YUY2_320x240');
set(vid,'FramesPerTrigger',Inf);
set(vid,'ReturnedColorspace','rgb');
vid.FrameGrabInterval=1;
start(vid)
for num_frames= 1:100
im=getsnapshot(vid);
sep_images.images(num_frames)=im;
end
stop(vid);
and it is giving me the error in this statement, sep_images.images(num_frames)=im;.
If someone has the idea of how to do it? please let me know.
I think you intended the images field to be a cell.
Initialize like:
sep_images=struct('images',{[]})
Assign like:
sep_images.images{num_frames}=im;
Just remember to access it with curly braces too (i.e. I = sep_images.images{iframe}).
Related
My issue involves using the RS-232 Simulink RT blocks.
A model is uploaded to the target PC (xPC) and it transmits and receives data from a variable frequency drive (VFD) that controls a motor. The issue arises on the receiving end when I take data and try to send that data to a display block in my model as a string. Code would be helpful here:
disp = uint8(zeros(1,24));
display = uint8(zeros(1,length(disp)));
cmd = 0;
status = stat_lb;
%% Start-Up
% Initialization Period
if (status == 0 || status == 1)
cmd = 0;
msg = uint8('Start up');
display = [msg uint8(zeros( 1, length(disp)- length(msg) ))];
end
...
%Multiple status cases with unique displays.
...
disp = display
So, here the cmd portion functions as expected. As noted above, I want to display the display string on a display block in my Simulink model. As you can see, though, it is of type uint8, so I need to convert it to type string; however, when I pass it through either the ascii2str Simulink block or just place it in the function call (e.g. display = ascii2str(display)) I get the following error message:
Executing the 'CheckData' command produced the following error: Invalid parameter/value pair arguments
My thought is that this has something to do with the fact that I am using MEX and this function (ascii2str) is not supported. Anyways, I am wondering if anyone knows why I receive this error and if there is anything I can do to resolve it.
Oh, and one last thing: I can get the display to work if I just remove the ascii2str; however, the only problem with this is that the display is in uint8 form and not really helpful. So, if there is any other way that I can decode the uint8 to a string I am all ears.
Thanks!
I have found that there is no support for this feature in Simulink RT. One option is to use external functions, but I found it better for my application to simply output a number and have a table in the simulation that explained what each number meant.
I am working on the face scrub dataset, which has a long list of urls of pictures.
I used a for loop to fetch these photos. However, some of the urls had expired so my matlab code return an error saying 'Unable to determine the file format.' However I think the actual reason is that the url link does not have the image anymore. For example, one of the bad urls is:
http://www.gossip.is/administrator/components/com_n-myndir/uploads/16de47418d69b1c2991731dffaca8a78.jpg
How do I identify and ignore this error so my code can keep working on the rest of the list? I could use R instead if that make solving this problem easier.
You can implement a try/catch block to catch (original isnt'it) the error message and skip the image if the link is indeed broken.
When we use the following syntax:
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
%// Just so we know what the identifier is.
ME
end
Matlab first tries to read the image given by the url. If it can't, we ask it to catch the error message (MException actually) and perform some other appropriate action.
The thing is, we need to know what is the exact error message in order to recognize it in the try/catch block.
When I entered the above code, I got the following structure for ME:
ME =
MException with properties:
identifier: 'MATLAB:imagesci:imread:fileFormat'
message: 'Unable to determine the file format.'
cause: {0x1 cell}
stack: [2x1 struct]
Therefore, once we know the exact identifier generating the error we can use strcmp to look for it in the try/catch block. For instance with the following code:
clear
clc
try
A = imread('http://www.gossip.is/cgi-sys/suspendedpage.cgi');
catch ME
if strcmp(ME.identifier,'MATLAB:imagesci:imread:fileFormat')
disp('Image link broken')
end
A = imread('peppers.png');
end
imshow(A);
Matlab displays 'Image link broken' and reads peppers.png, as expected.
Hope that helps!
I am getting an error when running this bit of code about the index. I have ran through the logic several times and have yet to catch my error and I am thinking it is in the way I coded this section. Any help would be greatly appreciated. Please let me know if I am missing any information vital for this bit of code.
index_pairs = [1,12661;12662,46147;46148,52362]
group_class_count = [10137,2524;127448,20738;1570,4645]
group_count = 3
cross_sections = 10
for j=1:group_count
rand_index=randsample(index_pairs(j,1):index_pairs(j,2),(group_class_count(j,1)+group_class_count(j,2)),true); % Creates an index of random rows for the current group.
cross_size(j)=floor(size(rand_index,2)/cross_sections);
for k=1:cross_sections
cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
end
end
error: Index exceeds matrix dimensions. Error in cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
If you change
cross_rand_indices(j,k)={rand_index(cross_size*(k-1)+1:cross_size*(k))};
to
cross_rand_indices(j,k)={rand_index(cross_size(j)*(k-1)+1:cross_size(j)*(k))};
the error will disappear.
I assume this is in line with your intent when saving something to cross_size(j) in the outer loop.
I'm sorry that currently I'm not able to boil my code down to a minimal example.
It's a huge bunch of image processing code.
I have a loop that iterates over images (descriptors in variable stphogs) and for each image runs a detection.
function hogpatches = extractDetectionsFromImages(stphogs, poselet)
hogpatches = cell(1,length(stphogs));
parfor i = 1:length(stphogs)
tmp = extractDetectionsFromImage(stphogs(i), poselet); %e.g. 1x6 struct
if ~isempty(tmp)
hogpatches{i} = tmp;
end
end
hogpatches = cell2mat(hogpatches);
end
So this is the main loop. But the function calls in extractDetectionsFromImage go very deep.
My problem: Running this with a normal for-loop gives the correct result. When using PARFOR as above, hogpatches only contains 5 instead of 18 structs.
Where can I start to look for the error? I had a global variable the program did change. I removed that already. There is still a global variable 'config' which is however only read.. Any other hints? What could be the problem?
EDIT:
Even if I just run one iteration (size of stphogs is 1), the parfor fails. It doesn't have anything to do with the isempty part. Problem persists if I remove that.
EDIT2:
Ok here I boiled it to a minimal working example.
It is indeed caused by a global variable:
function parGlobalTest()
global testVar;
testVar = 123;
parfor i = 1:1
fprintf('A Value: %d\n', testVar);
testFunction();
end
end
function testFunction()
global testVar;
fprintf('B Value: %d\n', testVar);
end
In this example. The output for A will be 123, for B it will be nothing (undefined).
Why is that?
Ok, here is my solution:
function syncTestVar()
global testVar;
save('syncvar.mat', 'testVar');
pctRunOnAll global testVar;
pctRunOnAll load('syncvar.mat');
end
If someone has a better approach please tell me... This one works though
Btw: The save/load is needed because in my real program, testVar is a complex struct
I'm doing a project in liver tumor classification. I used this code and it gave some output. I don't know whether I'm correct.
Actually I initially used Region Growing method for liver segmentation and from that I segmented tumor using FCM. So, to this GLCM program, I gave the tumor segmented image as input. Was I correct? If so, I think, then, my output will also be correct.
I gave the parameters exactly as in the example. Actually what do they mean? Do I need to change them for different images? If so, how to give the parameters? I'm completely new to this. So, kindly guide me.
I got this output. Am I correct?
stats =
autoc: [1.857855266614132e+000 1.857955341199538e+000]
contr: [5.103143332457753e-002 5.030548650257343e-002]
corrm: [9.512661919561399e-001 9.519459060378332e-001]
corrp: [9.512661919561385e-001 9.519459060378338e-001]
cprom: [7.885631654779597e+001 7.905268525471267e+001]
cshad: [1.219440700252286e+001 1.220659371449108e+001]
dissi: [2.037387269065756e-002 1.935418927908687e-002]
energ: [8.987753042491253e-001 8.988459843719526e-001]
entro: [2.759187341212805e-001 2.743152140681436e-001]
homom: [9.930016927881388e-001 9.935307908219834e-001]
homop: [9.925660617240367e-001 9.930960070222014e-001]
maxpr: [9.474275457490587e-001 9.474466930429607e-001]
sosvh: [1.847174384255155e+000 1.846913030238459e+000]
savgh: [2.332207337361002e+000 2.332108469591401e+000]
svarh: [6.311174784234007e+000 6.314794324825067e+000]
senth: [2.663144677055123e-001 2.653725436772341e-001]
dvarh: [5.103143332457753e-002 5.030548650257344e-002]
denth: [7.573115918713391e-002 7.073380266499811e-002]
inf1h: [-8.199645492654247e-001 -8.265514568489666e-001]
inf2h: [5.643539051044213e-001 5.661543271625117e-001]
indnc: [9.980238521073823e-001 9.981394883569174e-001]
idmnc: [9.993275086521848e-001 9.993404634013308e-001]
Kindly guide me. Thank you
its ok but i don't think we usually need all this extra information i usually prefer to use the following code
GLCM2 = graycomatrix(img,'Offset',[1 1]);
stats = graycoprops(GLCM2);
i hope it will help you