Clear Filters
Clear Filters

Are you able to count the number of pixels in a 3D plot?

1 view (last 30 days)
I am trying to find a way to count the number of red pixels in my moving plot and I was only able to find the number of white pixels in only at an angle. Would it be possible to count the total number of red pixels in a 3D plot?
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
xlabel('My x label') % Axis Labels
ylabel('My y label')
zlabel('My z label')
view([-37.5 30]) % standard 3D View
scale = 0.5;
y = 1; % white percentage
%% range of results
d = 10; % Distance Between each point
nmesh = 3; % range
MagR = 0; % Magnetic Radius (MagR>nmesh = Strong, MagR<nmesh = weak, MagR = 0 = no Mag)
niter = 200; % no. of jumps
[X,Y,Z] = meshgrid((-nmesh+1):nmesh-1); % create mesh
X = X * d;
Y = Y * d;
Z = Z * d;
%% random direction for X,Y,Z directions
dx = normrnd(0,1,[(size(X,1))^3 niter]);
dy = normrnd(0,1,[(size(X,1))^3 niter]);
dz = normrnd(0,1,[(size(X,1))^3 niter]);
%% Magnetised particles
c0 = nmesh; % center of magnetic field
for i = 1:size(dx,1)
[ii,jj,kk] = ind2sub(size(X),i); % row and column of force
n = pdist2([ii jj kk],[c0 c0 c0]); % distance to point
if n < MagR % radius of magnetic force
dx(i,:) = -cumsum(dx(i,:)*0+(jj-c0)/n); % cosinus
dy(i,:) = -cumsum(dy(i,:)*0+(ii-c0)/n); % sinus
dz(i,:) = -cumsum(dz(i,:)*0+(kk-c0)/n);
end
end
cla
hold on
%% For Loop to plot
for i = 1:niter-1
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
Z1 = Z(:) + dz(:,i);
plot3([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]',...
[Z(:) Z(:)]'+[dz(:,i+1) dz(:,i)]','.-r','MarkerSize',5,'LineWidth',1)
%p.MarkerSize = 10; % 6 is the default
%p.LineWidth = 1; % 0.5 is the default
pause(0.01)
%% percentage readings
Image = getframe();
K = sum(sum(rgb2gray(Image.cdata)==255));
percentageofwhite(y) = K/numel(rgb2gray(Image.cdata))*100;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofwhite(y)),'Units','normalized');
y=y+1;
%%
xlim([-2*d 2*d]) % Axis limitsg
ylim([-2*d 2*d])
zlim([-2*d 2*d])
end
hold off
rotate3d on
  5 Comments
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 5 May 2020
plot3([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]',...
[Z(:) Z(:)]'+[dz(:,i+1) dz(:,i)]','.-r','MarkerSize',5,'LineWidth',1)
According to this, you are drawing 125 pair red points in each iteration
sui zhi lau
sui zhi lau on 5 May 2020
hi Mrutyunjaya,
thank you for the fast repliy! how did you get that value and how would i be able to compared the red pixels compared to the total white percentage to get a percentage of it then?
Ash

Sign in to comment.

Answers (2)

darova
darova on 5 May 2020
Can you calculate the volume manually?
dv = [dx(:,i+1)-dx(:,i) dy(:,i+1)-dy(:,i) dz(:,i+1)-dz(:,i)];
V = V + pi*R*sqrt(sum(dv.^2,2)); % volume of all lines
  9 Comments
sui zhi lau
sui zhi lau on 6 May 2020
Yes but what do i replace R with? For example i put my linewidth as 0.5, do i take my R as 0.5 ?

Sign in to comment.


Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 6 May 2020
@ sui zhi lau,
Outside Loop
totalData = 0;
whitePixData = 40*40*40;
Inside Loop
redPixData(y) = sum(sqrt(((dx(:,i+1) - dx(:,i)).^2) + ((dy(:,i+1) - dy(:,i)).^2) + ((dz(:,i+1) - dz(:,i)).^2)));
totalData = totalData + redPixData(y);
percentageofred(y) = (totalData/whitePixData)*100;
txt=text(0.0,0.95,sprintf('White Space = %0.3f%%',percentageofred(y)),'Units','normalized');
  3 Comments
Mrutyunjaya Hiremath
Mrutyunjaya Hiremath on 7 May 2020
because, we are not considering the elimination of overlapping area.
sui zhi lau
sui zhi lau on 7 May 2020
How would you consider with elimination of overlapping areas?

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!