I have written a matlab function that performs some cpu intensive operations on images. I would like to improve the speed of this function, but cannot think of anymore ways to optimize it. Does anyone know how to further optimise the function below?
function imageMedoid(imageList, resizeFolder, outputFolder, x, y)
% local variables
medoidImage = zeros([1, y*x, 3]);
alphaImage = zeros([y x]);
medoidContainer = zeros([y*x, length(imageList), 3]);
% loop through all images in the resizeFolder
for i=1:length(imageList)
% get filename and load image
fname = imageList(i).name;
container = im2double(imread([resizeFolder fname]));
% load alpha channel, convert to zeros and ones, add to alphaImage
[~,~,alpha] = imread([resizeFolder fname]);
alpha = double(alpha) / 255;
alphaImage = alphaImage + alpha;
% add (r,g,b) values to medoidContainer and reshape to single line
medoidContainer(:, i, :) = reshape(container, [y*x 3]);
end
% loop through every pixel in medoidContainer
for i=1:length(medoidContainer)
% calculate distances between all values for current pixel
distances = pdist(squeeze(medoidContainer(i,:,1:3)));
% convert found distances to matrix of distances
distanceMatrix = squareform(distances);
% find index of image with the medoid value
[~, j] = min(mean(distanceMatrix,2));
% write found medoid value to medoidImage
medoidImage(1, i, 1:3) = medoidContainer(i, j, 1:3);
end
% replace values larger than one in alpha channel
alphaImage(alphaImage > 1) = 1;
% reshape image to original proportions
medoidImage = reshape(medoidImage, y, x, 3);
% save medoid image
imwrite(medoidImage, [outputFolder 'medoid.png'], 'Alpha', alphaImage);
Any suggestions would be greatly appreciated!
I would suggest that you use the built-in profiler of MATLAB in order to have a better idea on what are the most time consuming operations and then try to optimize there.
Tools -> Open Profiler -> imageMedoid(...)
You are calling
imread([resizeFolder fname])
twice in your first for-loop. Call it once as
[A,~,alpha] = imread([resizeFolder fname]);
This should result in some improvement.
Related
I am trying to optimize the speed of a function I am writing, and trying to use vectors as much as I can. I am new to Matlab and vectorization is sometimes understandable to me, but I would like some additional help. Here is my current code:
For note, the oracle() function represents a randomly shaped object, and if you input a 1x2 matrix, it will return whether or not the matrx (or in our case, x- y-coordinates) is inside the random object.
Code In Image
function area = MitchellLitvinov_areaCalc(n)
% create random coordinate vectors, with bounds from (3, 14)
x = rand(n, 1) * 11 + 3;
y = rand(n, 1) * 11 + 3;
% find every point that is inside of oracle
inOracle = oracle([x y]);
% calculate the proportion, and multiply total area by proportion to find area
% of oracle
numPointsInOracle = nnz(inOracle);
area = numPointsInOracle/n * (11*11);
% create variable to store number of points in the area, and create a
% matrix with size [numPoints, 2] to hold x and y values
oracleCoordinates = zeros(numPointsInOracle, 2);
% HERE IS WHERE I NEED ASSISTANCE!!!!!!!!!
% find the points that are in the oracle shape
index = 0; % start index at 0 % is the index of our oracleCoordinates matrix
for i = 1:n % have to go through every point again to get their index
% if point is inside oracle, increase index and record
% coordinates
if (inOracle(i) == 1) % see if point is in oracle
index = index + 1;
oracleCoordinates(index, 1) = x(i, 1);
oracleCoordinates(index, 2) = y(i, 1);
end
end
% plot all points inside the oracle
scatter(oracleCoordinates(:,1), oracleCoordinates(:,2))
title("Oracle Shape")
xlim([3, 14]);
ylim([3, 14]);
end
Yes, even with near maximum memory usage, the code will run fairly quickly. But I want it to be fully vectorized simply for speed reasons, and if I need to repurpose this code for imaging. Currently, to calculate the area I am using vectors, but to actually reproduce an image, I need to create a separate storage matrix, and manually use indexing/appending to then transfer over the points inside the oracle function. I was wondering if there were any direct "shortcuts" to make my plotting a bit faster.
You can use an array as the index to select certain items from another array. For example, using your variable names:
oracleCoordinates(:,1) = x(inOracle == 1);
oracleCoordinates(:,2) = y(inOracle == 1);
This should give the same result as the code in your question, without using a loop.
I am trying to get pixel intensity values from regions of interest in RGB images.
I segmented the image and saved the regions of interest (ROI) using regionprops 'PixelList' in MATLAB, as shown below:
In this example I am using "onion.png" image built in MATLAB. (But in reality I have hundreds of images, and each of them have several ROIs hence why I'm saving the ROIs separately.)
%SEGMENTATION PROGRAM:
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centriod positions as two colums
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w;
k=roi2(ww).PixelList;
save('onion_PL','k');
end
How do I use this pixel list to get the intensity values in the original image? More specifically, I need to get the ratio of pixels in Green channel over Red ("grr=rdivide(gg,rr);"), but only in the region of interest labeled with PixelList. Here's my code so far:
%PL is the PixelList output we got above.
a=imread('C:\Program Files\MATLAB\MATLAB Production Server\R2015a\toolbox\images\imdata\onion.png');warning('off', 'Images:initSize:adjustingMag');
PL=dir(['*PL.mat']); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m=1:length(PL);
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
for mm=1:length(k);
%INSERT ANSWER HERE
end
This next bit is wrong because it's based on the entire image ("a"), but it contains the calculations that I would like to perform in the ROIs
figure; imshow(a,[]);
pause;
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
I am brand new to MATLAB, so my code is not the greatest... Any suggestions will be greatly appreciated. Thank you in advance!
The loop you are looking for seems simple:
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
A more efficient solution is using sub2ind for converting the x,y coordinates to linear indices:
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
In your given code sample there are 5 PixelLists.
I don't know how do you want to "arrange" the result.
In my code sample, I am saving the 5 results to 5 mat files.
Here is an executable code sample:
close all
%SEGMENTATION PROGRAM:
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
figure; imshow(a,[]);
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
bgd=(rr+bb)./(2*gg); %calculation RGB ratio of background
zr1=bgd>1.15; %matrix containing background as 1 and ROI as 0
% remake binary image for holes command which requires white object to fill % (this step is not relevant for this example, but it is for my data)
zr2=zr1<0.5;
zr3=imfill(zr2, 'holes');
figure;imshow(zr3); %pause;
roi=regionprops(zr3,'Centroid','PixelList','Area');
ar=[roi.Area];
% find sort order , largest first
[as, ia]=sort(ar(1,:),'descend');
for w=1:length(roi); xy(w,:)=roi(w).Centroid;end
% use sort index to put cenrtoid list in same order
xy1=xy(ia,:);
%and pixel id list
for w=1:length(roi)
roi2(w).PixelList=roi(ia(w)).PixelList;
end
%extract centroid positions as two columns
%SAVE PIXEL LIST FOR EACH ROI IN A SEPARATE FILE
for ww=1:w
k=roi2(ww).PixelList;
%save('onion_PL', 'k');
save(['onion', num2str(ww), '_PL'], 'k'); % Store in multiple files - onion1_PL.mat, onion2_PL.mat, ... onion5_PL.mat
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear % Use clear for testing - the variables are going to be read from the mat file.
%PL is the PixelList output we got above.
a=imread('onion.png');warning('off', 'Images:initSize:adjustingMag');
nrows = size(a,1);ncols = size(a,2);
zr=ones(nrows,ncols); %matrix of ones
r=a(:,:,1);g=a(:,:,2);b=a(:,:,3); %get RGB values
rr=double(r);gg=double(g);bb=double(b);% convert to double to avoid uint8 sums
grr=rdivide(gg,rr);
PL=dir('*PL.mat'); %load file PixelList files. "dir" is a variable with directory path containing the pixelist files. In this example, we saved "onion_PL.mat"
for m = 1:length(PL)
load(PL(m).name);
ex=[]; %empty matrix to hold the extracted values
%for mm=1:length(k)
%INSERT ANSWER HERE
grr = zeros(nrows, ncols); % Initialize grr with zeros.
for mm = 1:length(k)
x = k(mm, 1); % Get the X (column) coordinate.
y = k(mm, 2); % Get the Y (row) coordinate.
grr(y, x) = gg(y, x) / rr(y, x);
end
% Instead of using a loop, it's more efficient to use sub2ind
if false
% Convert k to linear indices.
kInd = sub2ind([nrows, ncols], k(:,2), k(:,1));
% Update only the indices in the PixelList.
grr(kInd) = rdivide(gg(kInd), rr(kInd));
end
figure;imshow(grr);title(['grr of m=', num2str(m)]) % Show grr for testing.
save(['grr', num2str(m)], 'grr'); % Save grr for testing.
imwrite(imadjust(grr, stretchlim(grr)), ['grr', num2str(m), '.png']); % Store grr as image for testing
end
First two grr matrices as images (used for testing):
grr1.png:
grr2.png:
I'm trying to extend or subtract a magnitude from vectors in an array. The code to call the function is:
v_set = {[1;0],[0;2],[1;1]};
v_extensions = [1,-.5,1];
v_set_extended = vector_set_extend(v_set, v_extensions);
And the function itself is below. What I have adds the magnitude to both x and y, but I want it to add the magnitude in the direction of that the vector is pointing, if that makes sense.
function v_set_extended = vector_set_extend(v_set,v_extensions)
% Takes a set of vectors and a set of lengths by which to extend them.
% Returns a set of extended vectors
%
% Inputs:
%
% v_set: a cell array, each element of which is a vector
%
% v_extensions: a vector of the same dimensions as v_set, containing
% lengths which should be added to the vectors
%
% Outputs:
%
% v_set_extended: cell array of the same size as v_set, each element of
% which is the vector with its new length
%%%%%%%%
% Use the 'cell' and 'size' commands to create the output
% v_set_extended as an empty cell of the same size as v_set
v_set_extended = cell(size(v_set))
%%%%%%%%
% Loop over the vectors and extensions, adding the extension to the
% length of the vector, and then storing the result in the
% corresponding entry of v_set_extended
for idx = 1:numel(v_set_extended)
v_set_extended{idx} = v_set{idx}+v_extensions(idx);
end
end
This is mostly a trigonometry problem. You have your vector, let's call it v, with components v_x, v_y. Let's say now that you want to add w to the magnitude of your vector.
The magnitude of v is sqrt(v_x^2 + v_y^2), and its angle is theta = tan(v_y/v_x).
What you want to do is then create a new vector of magnitude m = sqrt(v_x^2 + v_y^2) + w but with the same direction. Once you know the new magnitude, the angle is the same and the new components of the vector are v1_x = m*cos(theta) and v1_y = m*sin(theta).
You shouldn't have any problem implementing this, just pay attention to the difference between sin/sind in Matlab.
The solution is rather simple but it is more complex since you are using cell array. Please avoid cell array and prefer normal array as much as possible. So first we convert v_set into a matrix:
v_mat = cell2mat(v_set)
Then you want to extend the vector so that the magnitude of the new vector is extended by some value. Compute first the magnitude of the vectors:
mag_v_mat = vecnorm(v_mat);
And finally make new vectors all at once:
v_extended = v_mat.*(mag_v_mat+v_extensions)./mag_v_mat;
Dotted operators are here to vectorize the operations along the vectors.
In one-line, it may be written as:
v_extended = cell2mat(v_set).*(1+v_extensions./vecnorm(cell2mat(v_set)));
Please refer to the comments for explanation, and easy way to stick to your current structure is to just compute a unit vector and scale it, rather than directly adding the extension...
for idx = 1:numel(v_set_extended)
% Compute the unit vector (magnitude 1) in the same direction
uvec = v_set{idx} / vecnorm(v_set{idx});
% Create a vector of the extension length in the unit direction
ext = uvec * v_extensions(idx);
% Add the extension
v_set_extended{idx} = v_set{idx} + ext;
end
You can sanity check this by validating the vecnorm (magnitude) values
cellfun( #vecnorm, v_set ) % = [1, 2.0, 1.4142]
cellfun( #vecnorm, v_set_extended ) % = [2, 1.5, 2.4142]
% difference = [1,-0.5, 1]
Say I have a signal that looks a bit like this:
that I am processing in MatLab, what functions would I have to use to get rid of the flattish area in the middle? is there any functions that can do that, or do I need to program it in myself? Currently I just have a blank function as I don't know where to start:
function removala = removal(a, b)
end
Is there any quick functions that can remove it or do I just have to search for all values below a threshold and remove them myself? For reference a and b are vectors of amplitude points.
use findpeaks:
% generating signal
x = 1:0.1:10;
y = rand(size(x))*0.5;
y([5,25,84]) = [6,-5.5,7.5];
z = y;
thresh = 0.75; % height threshold
% find peaks
[pks,locs] = findpeaks(z,'MinPeakProminence',thresh);
% remove signal noise between peaks
for ii = 1:length(locs)-1
zz = z(locs(ii)+1:locs(ii+1)-1);
zz(abs(zz) < thresh) = 0;
z(locs(ii)+1:locs(ii+1)-1) = zz;
end
% plot
plot(x,y);
hold on
plot(x,z);
plot(x(locs),pks,'og');
legend('original signal','modified signal','peaks')
You probably want to remove the signal whose absolute power is less than a certain threshold.
So the two input of your function would be the raw signal, and the threshold. The function will output a variable "cleanSignal"
function cleanSignal = removal(rawSignal,threshold)
simplest implementation. remove the data below threshold. If rawSignal is a matrix the resulting variable will be a vector concatenating all the epochs above threshold.
ind = abs(rawSignal)<threshold;
rawSignal(ind) = [];
cleanSignal = rawSignal;
This might not be the behavior that you want, since you want to preserve the original shape of your rawSignal matrix. So you can just "nan" the values below threshold.
ind = abs(rawSignal)<threshold;
rawSignal(ind) = nan;
cleanSignal = rawSignal;
end
Notice that this does not really removes flat signal, but signal which is close to 0.
If you really want to remove flat signal you should use
ind = abs(diff(rawSignal))<threshold;
I am doing vlfeat in Matlab and I am following this question here.
These below are my simple testing images:
Left Image:
Right Image:
I did a simple test with 2 simple images here (the right image is just rotated version of the left), and I got the result accordingly:
It works, but I have one more requirement, which is to match the SIFT points of the two images and show them, like this:
I do understand that vl_ubcmatch returns 2 arrays of matched indices, and it is not a problem to map them for which point goes to which point on two images. However, I am currently stuck in matlab's procedure. I found this. But that only works if the subplot stays that way. When you add an image into the subplot, the size changes and the normalization failed.
Here is my code: (im and im2 are images. f, d and f2, d2 are frames and descriptors from vl_sift function from 2 images respectively)
[matches score] = vl_ubcmatch(d,d2,threshold);%threshold originally is 1.5
if (mode >= 2)%verbose 2
subplot(211);
imshow(uint8(im));
hold on;
plot(f(1,matches(1,:)),f(2,matches(1,:)),'b*');
subplot(212);
imshow(uint8(im2));
hold on;
plot(f2(1,matches(2,:)),f2(2,matches(2,:)),'g*');
end
if (mode >= 3)%verbose 3
[xa1 ya1] = ds2nfu( f(1,matches(1,:)), f(2,matches(1,:)));
[xa2 ya2] = ds2nfu( f2(1,matches(2,:)), f2(2,matches(2,:)));
for k=1:numel(matches(1,:))
xxa1 = xa1(1, k);
yya1 = ya1(1, k);
xxa2 = xa2(1, k);
yya2 = ya2(1, k);
annotation('line',[xxa1 xxa2],[yya1 yya2],'color','r');
end
end
The code above yields this:
I think subplot isn't a good way to go for something like this. Is there a better method for this in Matlab? If possible, I want something like an empty panel that I can draw my image, draw lines freely and zoom freely, just like drawing 2D games in OpenGL style.
From zplesivcak's suggestion, yes, it is possible, and not that problematic after all. Here is the code:
% After we have applied vl_sift with 2 images, we will get frames f,f2,
% and descriptor d,d2 of the images. After that, we can apply it into
% vl_ubcmatch to perform feature matching:
[matches score] = vl_ubcmatch(d,d2,threshold); %threshold originally is 1.5
% check for sizes and take longest width and longest height into
% account
if (size(im,1) > size(im2,1))
longestWidth = size(im,1);
else
longestWidth = size(im2,1);
end
if (size(im,2) > size(im2,2))
longestHeight = size(im,2);
else
longestHeight = size(im2,2);
end
% create new matrices with longest width and longest height
newim = uint8(zeros(longestWidth, longestHeight, 3)); %3 cuz image is RGB
newim2 = uint8(zeros(longestWidth, longestHeight, 3));
% transfer both images to the new matrices respectively.
newim(1:size(im,1), 1:size(im,2), 1:3) = im;
newim2(1:size(im2,1), 1:size(im2,2), 1:3) = im2;
% with the same proportion and dimension, we can now show both
% images. Parts that are not used in the matrices will be black.
imshow([newim newim2]);
hold on;
X = zeros(2,1);
Y = zeros(2,1);
% draw line from the matched point in one image to the respective matched point in another image.
for k=1:numel(matches(1,:))
X(1) = f(1, matches(1, k));
Y(1) = f(2, matches(1, k));
X(2) = f2(1, matches(2, k)) + longestHeight; % for placing matched point of 2nd image correctly.
Y(2) = f2(2, matches(2, k));
line(X,Y);
end
Here is the test case:
By modifying the canvas width and height of one of the images from the question, we see that the algorithm above will take care of that and display the image accordingly. Unused area will be black. Furthermore, we see that the algorithm can match the features of two images respectively.
EDIT:
Alternatively, suggested by Maurits, for cleaner and better implementation, check out Lowe SIFT matlab wrappers.
If you have Matlab Computer Vision Library installed on your disc already, you can simply use
M1 = [f(1, match(1, :)); f(2, match(1, :)); ones(1, length(match))];
M2 = [f2(1, match(2, :)); f2(2, match(2, :)); ones(1, length(match))];
showMatchedFeatures(im,im2,[M1(1:2, :)]',[M2(1:2, :)]','montage','PlotOptions',{'ro','g+','b-'} );