plotting optical flow in matlab using vision tool box - matlab

I am trying to plot optical flow using quiver. I can plot the optical flow lines but not the arrows. This code is similar examples on Matlab tutorials.
Following is the code
function display_optical_flow (video_file_name)
vid_reader_obj = vision.VideoFileReader(video_file_name, 'VideoOutputDataType','single');
optical_flow_calculator = vision.OpticalFlow('Method','Lucas- Kanade','TemporalGradientFilter','Derivative of Gaussian', 'MotionVectorImageOutputPort', ...
true, 'OutputValue', 'Horizontal and vertical components in complex form', 'NoiseReductionThreshold',0.02);
screen_size = get(0, 'ScreenSize');
player_window_pos = [20 screen_size(4)-300 400 400];
video_display_originalVideo = vision.VideoPlayer('Name', 'Original Input Video', 'Position', player_window_pos);
player_window_pos(1) = player_window_pos(1) + 500;
video_display_opticalFlow = vision.VideoPlayer('Name','Optical Flow Vector', 'Position',player_window_pos);
player_window_pos(1) = player_window_pos(1) + 500;
video_display_of_image = vision.VideoPlayer('Name','Optical Flow image', 'Position',player_window_pos);
shape_inserter_line = vision.ShapeInserter('Shape','Lines', 'BorderColor','Custom','CustomBorderColor', [255 255 0]);
motion_vector_gain=20;
decimFactorRow = 5;
decimFactorCol = 5;
borderOffset = 5;
flag_first_time = true;
while ~isDone(vid_reader_obj)
cur_rgb_frame = step(vid_reader_obj);
cur_gray_frame = rgb2gray(cur_rgb_frame);
[optical_flow_vector IMV]= step(optical_flow_calculator, cur_gray_frame);
%magnitude of optical flow vectors by dot product
%of_magnitude= optical_flow_vector .* conj(optical_flow_vector);
if true == flag_first_time
flag_first_time = false;
[row col] = size(optical_flow_vector);
rowV = borderOffset:decimFactorRow:(row-borderOffset);
colV = borderOffset:decimFactorCol:(col-borderOffset);
[Y X] = meshgrid(rowV, colV);
end
lines =videooptflowlines(optical_flow_vector, 20);
if ~isempty(lines)
motion_vectors = step(shape_inserter_line, cur_rgb_frame, lines);
step(video_display_opticalFlow, motion_vectors);
end
%Display
step(video_display_originalVideo, cur_rgb_frame);
step(video_display_of_image, IMV);
%
[Xsize Ysize] = meshgrid(1:size(cur_gray_frame, 2), 1:size(cur_gray_frame, 1));
imshow(cur_gray_frame); hold on;
Real_part = real(optical_flow_vector(:));
Imag_part = imag(optical_flow_vector(:));
quiver(Xsize(:), Ysize(:), Real_part(:), Imag_part(:), 0);
pause;
end
release (video_display_opticalFlow);
release (video_display_originalVideo);
release (vid_reader_obj);
This do not show the arrow as I expect to see on each frame image by quiver.
What wrong I should do to correct the code?
nitin

Related

How do I use code to generate a graph where the line crosses the origin in Matlab?

I am trying to use Matlab to generate a line graph, but the line terminates at the last point, and doesn't go all the way to the origin. Is there any way to make it so that the line goes beyond the points in code?
I've attached the code that I'm currently using, along with pictures of what the graph looks like right now and how I want it to look.
%Enter Data
fnet = [0.465, 0.560, 0.670, 0.763, 0.870, 0.971, 1.063];
faccel = [0.434, 0.514, 0.612, 0.684, 0.776, 0.850, 0.915];
asys = [0.4963, 0.6034, 0.7074, 0.8088, 0.9210, 1.030, 1.138]
mh = [0.050, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11]
x = fnet;
y = asys;
%Model Equation
model = 'm*x'
%the model can be changed, ex. m*x^2
IV = 'x'
DV = 'y'
%Create and perform curve fit
newfit = fittype(model, 'Independent', IV, 'Dependent', DV);
%result and goodness of fit, prime symbol converys rows to columns
[result, gof] = fit(x', y', newfit, 'StartPoint', 1)
%plot fits and data points, create plot object for formatting
p = plot(result, x, y);
%style the data points
p(1).MarkerSize = 10;
p(1).Marker = '.';
p(1).MarkerFaceColor = 'blue';
%p(1).MarkerEdgeColor = 'green';
%style the line of best fit
p(2).LineWidth = 1;
p(2).Color = 'black';
%Create graph object, set formatting to latex
graph = gca;
set(graph, 'defaultTextInterpreter', 'latex');
set(legend, 'visible', 'off');
%format title and subtitle
graph.Title.String = {'System Acceleration vs. Net Force on System', 'in Modified Atwood Machine'};
graph.Title.FontSize = 16;
%subtitle, where we will place our equation and statistics
%specifically, the equation w/ units, r squared, slope with plusminus %
graph.Subtitle.Interpreter = 'latex';
graph.Subtitle.String = '$a_{sys} = 1.064 m^{-1}F_{net}, \, r^2=0.9994, m=1.064 \pm 0.007$';
graph.Subtitle.FontSize = 13;
%format x and y axes
graph.XLabel.Interpreter = 'latex';
graph.XLabel.String = '$F_{net} \: (N)$';
graph.XLabel.FontSize = 15;
graph.XLim = [0,1.5];
graph.XGrid = 'on';
graph.XMinorGrid = 'on';
graph.XMinorTick = 'on';
graph.YLabel.Interpreter = 'latex';
graph.YLabel.String = '$a_{sys} \: (\frac{m}{s^2})$';
graph.YLabel.FontSize = 15;
graph.YLim = [0,1.5];
graph.YGrid = 'on';
graph.YMinorGrid = 'on';
graph.YMinorTick = 'on';
Instead of using plot directly on the fit result object, you can call plot with a bit more control and evaluate the result directly for the line
Replace this:
p = plot(result, x, y);
with this:
hold on
p(1) = plot( x, y );
p(2) = plot( [0,2.2], feval(result, [0,2.2]) );
Note you could add the marker and line options during the plot calls now if you wanted, instead of updating p(1) and p(2) in retrospect.

How to display a group of subplots as a movie?

I have 30 heatmap subplots. How could I present these subplots as an animation or a movie (i.e. one heatmap each 0.5 seconds)?
The subplots are obtained with the following code:
var = {'GeneX','GeneY','GeneZ'};
syms x y z S
alpha_x = 3.9e-2;
beta_x = 6.1;
z_x = 1.3e-5;
n_zx = 2.32;
alpha_y= 4.3e-2;
beta_y = 5.7;
x_y = 7.9e-4;
n_xy = n_zx;
delta_y = 1.05;
x_z = 12e-2;
n_xz = n_zx;
y_z = 11e-3;
n_yz = n_zx;
delta_z = 0.2;
ACDC_X = (alpha_x+beta_x*S)/(1+S+(z/z_x)^n_zx)-x;
ACDC_Y = (alpha_y+beta_y*S)/(1+S+(x/x_y)^n_xy)-delta_y*y;
ACDC_Z = 1/(1+(x/x_z)^n_xz+(y/y_z)^n_yz)-delta_z*z;
ACDCsys_div = [ ACDC_X, ACDC_Y, ACDC_Z ];
J = jacobian(ACDCsys_div,[x;y;z]);
Jsolnew(x,y,z,S) = [J];
%%Construction of the coordinates as a matrix
A = load('matlab.mat','unnamed');% import data from directory
a2 = struct2array(A);% coordinates of the equilibrium point.
numofGraphs = 80;
bx = length(a2(1,1:numofGraphs));
%% Construction of the heatmaps
figure;
hmapax = ceil(sqrt(bx));
for kk = 1:bx %bnx %All bin windows = iteration
JacACDCnew(:,:,kk) = Jsolnew(a2(1,kk),a2(2,kk),a2(3,kk),a2(4,kk));
ACDC_HmapJnew = double(JacACDCnew(:,:,kk));
subplot(hmapax,hmapax,kk);%
heatmap(var,var,ACDC_HmapJnew,'ColorScaling','log');
S = a2(4,kk);
title(['Jac','s=',num2str(S)]);
end
Consider the following example:
function q56130816
% Load some data:
frames = imread('https://i.stack.imgur.com/03kN8.gif');
frames(frames > 1) = 2;
% Create subplots:
figure('WindowState','maximized'); subplot(2,4,1);
for ind1 = 1:8
subplot(2,4,ind1);
imagesc(frames(:,:,1,ind1)); axis image; axis off;
end
colormap([255 255 255; 188 188 188; 244 128 36]./255);
% Capture subplots as frames:
for ind1 = 8:-1:1
frameHolder(ind1) = getframe( subplot(2, 4 ,ind1) );
end
% Play as movie:
hF = figure(); movie(hF, frameHolder, 20, 2);
Which will turn:
Into:
As you can see, in the example above I used getframe, but frames can also be captured using print, as mentioned in the getframe docs. Frames can also be exported to a video file, as explained here.
Animation credit: frames were screen-captured from Johan Lindell's Codepen example.

how to find the corners of rotated object in matlab?

I want to find the corners of objects.
I tried the following code:
Vstats = regionprops(BW2,'Centroid','MajorAxisLength','MinorAxisLength',...
'Orientation');
u = [Vstats.Centroid];
VcX = u(1:2:end);
VcY = u(2:2:end);
[VcY id] = sort(VcY); % sorting regions by vertical position
VcX = VcX(id);
Vstats = Vstats(id); % permute according sort
Bv = Bv(id);
Vori = [Vstats.Orientation];
VRmaj = [Vstats.MajorAxisLength]/2;
VRmin = [Vstats.MinorAxisLength]/2;
% find corners of vertebrae
figure,imshow(BW2)
hold on
% C = corner(VER);
% plot(C(:,1), C(:,2), 'or');
C = cell(size(Bv));
Anterior = zeros(2*length(C),2);
Posterior = zeros(2*length(C),2);
for i = 1:length(C) % for each region
cx = VcX(i); % centroid coordinates
cy = VcY(i);
bx = Bv{i}(:,2); % edge points coordinates
by = Bv{i}(:,1);
ux = bx-cx; % move to the origin
uy = by-cy;
[t, r] = cart2pol(ux,uy); % translate in polar coodinates
t = t - deg2rad(Vori(i)); % unrotate
for k = 1:4 % find corners (look each quadrant)
fi = t( (t>=(k-3)*pi/2) & (t<=(k-2)*pi/2) );
ri = r( (t>=(k-3)*pi/2) & (t<=(k-2)*pi/2) );
[rp, ip] = max(ri); % find farthest point
tc(k) = fi(ip); % save coordinates
rc(k) = rp;
end
[xc,yc] = pol2cart(tc+1*deg2rad(Vori(i)) ,rc); % de-rotate, translate in cartesian
C{i}(:,1) = xc + cx; % return to previous place
C{i}(:,2) = yc + cy;
plot(C{i}([1,4],1),C{i}([1,4],2),'or',C{i}([2,3],1),C{i}([2,3],2),'og')
% save coordinates :
Anterior([2*i-1,2*i],:) = [C{i}([1,4],1), C{i}([1,4],2)];
Posterior([2*i-1,2*i],:) = [C{i}([2,3],1), C{i}([2,3],2)];
end
My input image is :
I got the following output image
The bottommost object in the image is not detected properly. How can I correct the code? It fails to work for a rotated image.
You can get all the points from the image, and use kmeans clustering and partition the points into 8 groups. Once partition is done, you have the points in and and you can pick what ever the points you want.
rgbImage = imread('your image') ;
%% crop out the unwanted white background from the image
grayImage = min(rgbImage, [], 3);
binaryImage = grayImage < 200;
binaryImage = bwareafilt(binaryImage, 1);
[rows, columns] = find(binaryImage);
row1 = min(rows);
row2 = max(rows);
col1 = min(columns);
col2 = max(columns);
% Crop
croppedImage = rgbImage(row1:row2, col1:col2, :);
I = rgb2gray(croppedImage) ;
%% Get the white regions
[y,x,val] = find(I) ;
%5 use kmeans clustering
[idx,C] = kmeans([x,y],8) ;
%%
figure
imshow(I) ;
hold on
for i = 1:8
xi = x(idx==i) ; yi = y(idx==i) ;
id1=convhull(xi,yi) ;
coor = [xi(id1) yi(id1)] ;
[id,c] = kmeans(coor,4) ;
plot(coor(:,1),coor(:,2),'r','linewidth',3) ;
plot(c(:,1),c(:,2),'*b')
end
Now we are able to capture the regions..the boundary/convex hull points are in hand. You can do what ever math you want with the points.
Did you solve the problem? I Looked into it and it seems that the rotation given by 'regionprops' seems to be off. To fix that I've prepared a quick solution: I've dilated the image to close the gaps, found 4 most distant peaks of each spine, and then validated if a peak is on the left, or on the right of the centerline (that I have obtained by extrapolating form sorted centroids). This method seems to work for this particular problem.
BW2 = rgb2gray(Image);
BW2 = imbinarize(BW2);
%dilate and erode will help to remove extra features of the vertebra
se = strel('disk',4,4);
BW2_dilate = imdilate(BW2,se);
BW2_erode = imerode(BW2_dilate,se);
sb = bwboundaries(BW2_erode);
figure
imshow(BW2)
hold on
centerLine = [];
corners = [];
for bone = 1:length(sb)
x0 = sb{bone}(:,2) - mean(sb{bone}(:,2));
y0 = sb{bone}(:,1) - mean(sb{bone}(:,1));
%save the position of the centroid
centerLine = [centerLine; [mean(sb{bone}(:,1)) mean(sb{bone}(:,2))]];
[th0,rho0] = cart2pol(x0,y0);
%make sure that the indexing starts at the dip, not at the corner
lowest_val = find(rho0==min(rho0));
rho1 = [rho0(lowest_val:end); rho0(1:lowest_val-1)];
th00 = [th0(lowest_val:end); th0(1:lowest_val-1)];
y1 = [y0(lowest_val:end); y0(1:lowest_val-1)];
x1 = [x0(lowest_val:end); x0(1:lowest_val-1)];
%detect corners, using smooth data to remove noise
[pks,locs] = findpeaks(smooth(rho1));
[pksS,idS] = sort(pks,'descend');
%4 most pronounced peaks are where the corners are
edgesFndCx = x1(locs(idS(1:4)));
edgesFndCy = y1(locs(idS(1:4)));
edgesFndCx = edgesFndCx + mean(sb{bone}(:,2));
edgesFndCy = edgesFndCy + mean(sb{bone}(:,1));
corners{bone} = [edgesFndCy edgesFndCx];
end
[~,idCL] = sort(centerLine(:,1),'descend');
centerLine = centerLine(idCL,:);
%extrapolate the spine centerline
yDatExt= 1:size(BW2_erode,1);
extrpLine = interp1(centerLine(:,1),centerLine(:,2),yDatExt,'spline','extrap');
plot(centerLine(:,2),centerLine(:,1),'r')
plot(extrpLine,yDatExt,'r')
%find edges to the left, and to the right of the centerline
for bone = 1:length(corners)
x0 = corners{bone}(:,2);
y0 = corners{bone}(:,1);
for crn = 1:4
xCompare = extrpLine(y0(crn));
if x0(crn) < xCompare
plot(x0(crn),y0(crn),'go','LineWidth',2)
else
plot(x0(crn),y0(crn),'ro','LineWidth',2)
end
end
end
Solution

Extraction of Plankton using Segmentation with Matlab

I am trying to extract plankton from a scanned image.
I segmented the plankton using the technique I found here, http://www.mathworks.com/help/images/examples/detecting-a-cell-using-image-segmentation.html
The outline is not bad, however, now I am not sure how to extract the images so each individual plankton can be saved individually. I tried to use labels but there is a lot of noise and it labels every single spec. I am wondering if there is a better way to do this.
Here is my code:
I = imread('plankton_2.jpg');
figure, imshow(I), title('original image');
[~, threshold] = edge(I, 'sobel');
fudgeFactor = .5;
BWs = edge(I,'sobel', threshold * fudgeFactor);
figure, imshow(BWs), title('binary gradient mask');
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');
BWnobord = imclearborder(BWdfill,1);
figure, imshow(BWnobord), title('cleared border image');
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');
BWoutline = bwperim(BWfinal);
Segout = I;
Segout(BWoutline) = 0;
figure, imshow(Segout), title('outlined original image');
label = bwlabel(BWfinal);
max(max(label))
for j = 1:max(max(label))
[row, col] = find(label == j);
len = max(row) - min(row)+2;
breadth = max(col)-min(col) +2;
target = uint8(zeros([len breadth]));
sy = min(col)-1;
sx = min(row)-1;
for i = 1:size(row,1)
x = row(i,1)-sx;
y = col(i,1) - sy;
target(x,y)=I(row(i,1),col(i,1));
end
mytitle =strcat('Object Number:',num2str(j));
figure, imshow(target);mytitle;
end
for j = 1:max(max(label))
[row, col] = find(label == j);
len = max(row) - min(row)+2;
breadth = max(col)-min(col) +2;
target = uint8(zeros([len breadth]));
sy = min(col)-1;
sx = min(row)-1;
for i = 1:size(row,1)
x = row(i,1)-sx;
y = col(i,1) - sy;
target(x,y)=I(row(i,1),col(i,1));
end
mytitle =strcat('Object Number:',num2str(j));
figure, imshow(target);mytitle;
end
You should use the regionprops function to filter the detected objects by size and/or shape characteristics.

How to use the new functions of PDE toolbox in image processing (Matlab R2015a)

I want to use the line new command of PDE toolbox as Matlab R2015 to restore a noisy image with gaussian noise.
The PDE is:
∇.(( ∇u)/(√(1+|∇u|2))) +(f2)/(u2) = 1 in Ω (∂u)/(∂n)=0 in ∂Ω
Where f is the noisy image and u the restored image.
I tried the following code:
clear
close all
clc
img = 'AA.jpg';
mInputImage = double(imread(img));
mInputImage = rgb2gray(mInputImage);
[numRows, numCols] = size(mInputImage);
Var = 0.04;
Mean = 0;
mInputImageNoisy = imnoise((mInputImage(:,:,1)),'gaussian',Mean, Var);
% reshape the input and noisy images to vectors
mInputImageVector = reshape(mInputImage,numRows*numCols,1);
mInputImageNoisyVector = reshape(mInputImageNoisy,numRows*numCols,1);
Residu1 = norm(mInputImageVector-mInputImageNoisyVector)/norm(mInputImageVector)
RegularisationCoefficient = 0.7*ones((numRows-1)*(numCols-1),1);
mOutputImageVector = mInputImageNoisyVector;
%a = (mInputImageNoisyVector.^2) ./ mOutputImageVector.^3;
f = 1;
rtol = 1e-1;
c = '1./sqrt(1+ux.^2+uy.^2)';
% Create a PDE Model with a single dependent variable
numberOfPDE = 1;
pdem = createpde(numberOfPDE);
g = #squareg;
geometryFromEdges(pdem,g);
% Plot the geometry and display the edge labels for use in the boundary
% condition definition.
figure;
pdegplot(pdem, 'edgeLabels', 'on');
%axis([0 numRows 0 numCols]);
axis([-2 2 -2 2]);
title 'Geometry With Edge Labels Displayed'
b2 = applyBoundaryCondition(pdem,'Edge',[1 2 3 4], 'u', 0);
[p,e,t] = poimesh(g,numRows, numCols);
numCols
pdemesh(p,e,t);
axis equal
for iter = 1: numRows*numRows,
mOutputImageVector(iter) = pdenonlin(pdem,c,...
(mInputImageNoisyVector(iter).^2) ./ mOutputImageVector(iter).^3,...
f,'tol',rtol);
SaveImageVector(iter) = mOutputImageVector;
end
mOutputImage = reshape(SaveImageVector,numRows,numRows);
mOutputImage = uint8(mOutputImage);
figure()
imshow(mOutputImage)