I want to create a dataset of my own like CIFAR-10 but not with RGB values but CEDD feature vector of image. I am creating a imageDatastore and reading the images and thier labels with following code :
imageFolder = fullfile('G:\9th Semester\Project - 2\myDataset');
imds = imageDatastore(imageFolder,'LabelSource', 'foldernames', 'IncludeSubfolders',true);
[trainingSet , testingSet] = splitEachLabel(imds , 0.8 , 'randomize');
trainingSet = shuffle(trainingSet);
testingSet = shuffle(testingSet);
data = [];
labels = char.empty(0,10);
cedd = [];
for i=1:size(trainingSet.Files)
image = readimage(trainingSet,i);
cedd = CEDD(image);
zerosCount = 0 ;
for j=1:144
if cedd(j) == 0
zerosCount=zerosCount + 1;
end
end
if zerosCount ~= 144
data(i , :) = cedd;
labels(i ,: ) = trainingSet.Labels(i);
end
end
save('train.mat' , 'data' , 'labels');
But is giving me this error :
Unable to perform assignment because the size of the left side is 1-by-10 and the size of the right side is 1-by-3.
Error in dataset (line 20)
labels(i ,: ) = trainingSet.Labels(i);
Any help will be much appreciated !
Related
When I ran the code below, it gave me this error. Any thoughts?
Unable to perform assignment because dot indexing is not supported for variables of this type.
The fundamental basis of this piece of is to filter or parse through a series of tracking data (generated via ImageJ/Fiji), one named Spots and one named Tracks, to remove undesired particle tracks - those that do not share a starting time of 0 and have different durations.
Batch Process Control
%batch parse TrackMate V.7 output to individual x y and t .csv files
clear all; clc;
%specify directory
directory = '/Volumes/GoogleDrive/Shared drives/Jessica_Michael/Data/matlab/20220715_noise_floor/';
addpath(directory)
%find and store all imgs in the directory to cell array
dirspots = dir([directory '*Spots_statistics.csv']);
dirtracks = dir([directory '*Tracks_statistics.csv']);
spotsfnames = {};
tracksfnames = {};
numfiles = length(dirspots);
[spotsfnames{1:numfiles}] = dirspots(:).name;
[tracksfnames{1:numfiles}] = dirtracks(:).name;
for fnum = 1:numfiles
spots_file = spotsfnames{fnum};
tracks_file = tracksfnames{fnum};
[x_temp, y_temp, t_temp] = parse_file(tracks_file,spots_file);
x_filtered{fnum}=x_temp;
y_filtered{fnum}=y_temp;
t_filtered{fnum}=t_temp;
end
%% read track statistics
function [x_filtered,y_filtered,t_filtered]=parse_file(tracks_file,spots_file)
trackStatistics = readtable(tracks_file);
lengthTrack = length(trackStatistics.TRACK_START); %returns number of rows/tracks
trackStatistics_filtered = [];
spotsStatistics_filtered = [];
x_filtered = [];
y_filtered = [];
t_filtered = [];
spotsStatistics = readtable(spots_file);
lengthSpots = length(spotsStatistics.ID); %returns number of rows/spots
max_frame = max(spotsStatistics.FRAME)+1; %TrackMate frame number starts from 0
max_timelapse = max(trackStatistics.TRACK_DURATION);
for i = 1 : lengthTrack
if trackStatistics.TRACK_START(i) == 0 && trackStatistics.TRACK_DURATION(i) == max_timelapse
trackStatistics_filtered(end+1) = trackStatistics.TRACK_ID(i);
end
end
lengthStatistics_filtered = length(trackStatistics_filtered);
for j = 1 : lengthStatistics_filtered
for i = 1 : lengthSpots
if spotsStatistics.TRACK_ID(i) == trackStatistics_filtered(j)
% if there is a trackID equal to one in the filtered list,
% append that spotsStatistics to the *END* of the row
spotsStatistics_filtered = [spotsStatistics_filtered; spotsStatistics(i,:)];
end
end
% at the end of each x_, y_, and t_filtered, append spotsStatistics.
% x_filtered, y_filtered and t_filtered will grow in size till for loop
% ends.
x_filtered = [x_filtered spotsStatistics_filtered.POSITION_X(end-max_frame+1:end)];
y_filtered = [y_filtered spotsStatistics_filtered.POSITION_Y(end-max_frame+1:end)];
t_filtered = [t_filtered spotsStatistics_filtered.POSITION_T(end-max_frame+1:end)];
end
end
Any recommendations?
I am writing my own script/function in matlab without using built-in command, "imresize" but i am getting 3 output images instead of getting single one. I am sharing my code here also. Kindly someone spot out my mistake.
%zoomin out an imagge
originalImage = imread('imggerm1.jpg');
[origImRows, origImColumns] = size(originalImage);
newImage = zeros(origImRows/2, origImColumns/2);
newImRow = 1; newImColumn = 1;
for row = 1:2:origImRows
for column = 1:2:origImColumns
newImage(newImRow, newImColumn)=originalImage(row, column);
newImColumn = newImColumn+1;
end
newImRow = newImRow+1;
newImColumn = 1;
end
figure; imshow(originalImage);
figure; imshow(newImage/255);
This is because you originally reading a color image, where each pixel is encoded by 3 numbers. Try typing size(originalImage) and you will see that this array is 3 dimensional (the size of the last dimension is 3).
In your code the following line:
[origImRows, origImColumns] = size(originalImage);
Produces the result you don't expect: your origImColumns appears to be 3 times bigger.
Your code is easy to fix. Below I slightly changed 3 lines: #4, #6 and #11:
%zoomin out an imagge
originalImage = imread('1.jpg');
[origImRows, origImColumns,~] = size(originalImage);
newImage = zeros(origImRows/2, origImColumns/2,3);
newImRow = 1; newImColumn = 1;
for row = 1:2:origImRows
for column = 1:2:origImColumns
newImage(newImRow, newImColumn,:)=originalImage(row, column,:);
newImColumn = newImColumn+1;
end
newImRow = newImRow+1;
newImColumn = 1;
end
figure; imshow(originalImage);
figure; imshow(newImage/255);
I have an Image histogram using imhist and it contains 3 different regions like the attached image shows, I want to get the borders or the interval of the largest continuous area of the histogram, in this case, the second region is the one that I am looking for and the borders would be 43 and 225
You can find the begin and end bins for each region like this
[counts,binLocations] = imhist(I);
der = diff([false; counts>0; false]);
upedge = find(der == 1);
downedge = find(der == -1) - 1;
regions = [binLocations(upedge) binLocations(downedge)];
If the values are not exactly zero, but very close to zero then you can replace 0 with some threshold value in the above code.
Example
im = uint8(zeros(300,400));
im(1:100,:) = uint8(randi([0,40],[100,400]));
im(101:200,:) = uint8(randi([90,100],[100,400]));
im(201:300,:) = uint8(randi([140,240],[100,400]));
[counts,binLocations] = imhist(im);
der = diff([false; counts>0; false]);
upedge = find(der == 1);
downedge = find(der == -1) - 1;
regions = [binLocations(upedge) binLocations(downedge)];
results in
regions =
0 40
90 100
140 240
I will use the answer to this question to find regions of consecutive non zero elements in an array.
lets assume we have this array (histogram):
h = [0,0,0,1,2,3,44,77,5,656,0,0,0,0,0,0,2,99,7,34];
now we want to know were each region of consecutive non-zero elements starts and ends, in this example we want
startIndex = [4,17]
endIndex = [10,20]
lengths = [7,4]
to get this result we use the code from the question as follows:
dsig = diff([1,h(:)'==0,1]);
startIndex = find(dsig < 0);
endIndex = find(dsig > 0)-1;
duration = endIndex-startIndex+1;
and to get longest region use:
[~,maxLengthIndex] = max(lengths);
maxStartIndex = startIndex(maxLengthIndex);
maxEndIndex = endIndex(maxLengthIndex);
Im trying to make a loop for doing the same operation to a lot of .mov files in matlab. The code i have right now looks like this:
close all
clear all
clc
movFiles = dir('*.mov');
numFiles = length(movFiles);
mydata = cell(1,numFiles);
% mydata = zeros(numFiles);
for k = 1:numFiles
mydata{1,k} = VideoReader(movFiles(k).name);
end
for k=1:numFiles
bk_downsample = 5; %The downsample factor for frame averaging
%disp('Opening video...') %lower number =longer computation time
vob = mydata;
frame = vob.read(inf); %Reads to end = vob knows the number of frames
vidHeight = vob.Height;
vidWidth = vob.Width;
nFrames = vob.NumberOfFrames;
%% First-iteration background frame
background_frame = double(frame*0);
disp('Calculating background...')
for k = 1:bk_downsample:nFrames
background_frame = background_frame + double(read(vob, k));
disp(k/(nFrames)*100)
end
%background_frame = uint8(bk_downsample*background_frame/(nFrames));
background_frame = bk_downsample*background_frame/(nFrames);
%imshow(background_frame)
%% Second-iteration background frame
%This section re-calculates the background frame while attempting to
%minimize the effect of moving objects in the calculation
background_frame2 = double(frame*0);
pixel_sample_density = im2bw(double(frame*0));
diff_frame = double(frame*0);
stream_frame = diff_frame(:,:,1);
bk_downsample = 10;
figure
hold on
for k = 1:bk_downsample:nFrames
diff_frame = imabsdiff(double(read(vob, k)), background_frame);
diff_frame = 1-im2bw(uint8(diff_frame),.25);
pixel_sample_density = pixel_sample_density + diff_frame;
stream_frame = stream_frame + (1-diff_frame)/(nFrames/bk_downsample);
nonmoving = double(read(vob, k));
nonmoving(:,:,1) = nonmoving(:,:,1).*diff_frame;
nonmoving(:,:,2) = nonmoving(:,:,2).*diff_frame;
nonmoving(:,:,3) = nonmoving(:,:,3).*diff_frame;
background_frame2 = background_frame2 + nonmoving;
%pause
disp(k/(nFrames)*100)
end
background_frame2(:,:,1) = background_frame2(:,:,1)./pixel_sample_density;
background_frame2(:,:,2) = background_frame2(:,:,2)./pixel_sample_density;
background_frame2(:,:,3) = background_frame2(:,:,3)./pixel_sample_density;
imshow(uint8(background_frame2))
%imshow(stream_frame)
filename = ['Ring_' num2str(k) '_background_' num2str(img) '.jpg'];
imwrite((uint8(background_frame2)),filename)
end
I know that the error starts with vob=mydata; but im not sure how to correct it, hope that someone is able to help me since it would save me a lot of time in my data-analysis.
Have a great day! :)
Your code doesn't make much sense... You're creating a cell array:
mydata = cell(1,numFiles);
%// . . .
mydata{1,k} = . . .
but however you try to access it like a structure:
vob = mydata;
frame = vob.read(inf);
If I'd guess, then your error stems from you forgetting to index in the cell array, i.e.:
vob = mydata{k};
Other programming oddity I noticed in your code is the fact you're using the same looping variable k, in two nested for lops, the outer one being on k=1:numFiles and the inner ones being on k=1:bk_downsample:nFrames; don't do that unless you're trying to drive yourself crazy while figuring out why your for loop executes only once. Name them k1 for the outer loop and k2 for the inner loops and you'll be happier.
I'm no expert in video processing, but for me it looks like your line should be:
vob=mydata{1,k};
That's why that error shows up, because you are treating a cell of structs as if it was a single struct.
I am taking images from folder('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY) Images are stored as 1. gif , 2.gif , and so on
I am dividing it . I want to store these images with new name . eg image taken is 1.gif
after diving it into 4 parts, I want to name these new images as 1. gif , 2.gif , 3.gif , 4.gif
Please help me . I have tried my code but it shows the error :
Error in feb11try (line 25)
Img1(i,j) = NewImage(startr+i, startc+j);
Code is :
clc;
close all;
clear all;
srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\*.gif'); % the folder in which ur images exists
%Taking the image from the given URL, it could have been the name of the file with extension if the root folder
n =2; %defining the number of rows
m =2; %defining the number of columns
for c = 1 : length(srcFiles)
NewImage = 'newimage.gif'; %granting permission to create a file and write in it
rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file
for v = 1:n
for s = 1:m %nXm files need to be made
startr = (v-1)*rf;
startc = (s-1)*cf;
for i = 1 : rf
for j = 1 : cf
Img1(i,j) = NewImage(startr+i, startc+j);
end
end
imwrite( NewImage,'c.gif');
end
end
end
You need to use imread() to read in the source image rather than treating the filename as an image. Based on what you have, I assume your image is single channel. I also made the assumption that your image is unsigned int. Also, keep in mind there are much faster ways to perform this using indexing.
I made a few changes to your sample that should get the job done under these assumptions. This is untested.
clc;
close all;
clear all;
srcFiles = dir('E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\*.gif');
n = 2; %defining the number of rows
m = 2; %defining the number of columns
count = 0;
for c = 1 : length(srcFiles)
srcFilename = ['E:\cicila\ME PROJECT\ME 4 SEM\brodatz\IMAGES TAKEN\TRY\' srcFiles(c).name];
srcImg = imread(srcFilename); % Read source image
[~,srcName] = fileparts(srcFilename);
rf = floor(512/n); %generating the number of row pixels in the new file
cf = floor(512/m); %generating the number of row pixels in the new file
% initialize output image
destImg = uint8(zeros(rf,cf));
for v = 1:n
for s = 1:m %nXm files need to be made
startr = (v-1)*rf;
startc = (s-1)*cf;
for i = 1 : rf
for j = 1 : cf
destImg(i,j) = srcImg(startr+i, startc+j);
end
end
count = count + 1;
imwrite( destImg, sprintf('%d.gif', count) );
end
end
end