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

3 views (last 30 days)
sui zhi lau on 5 May 2020
Commented: sui zhi lau on 7 May 2020
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)
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

sui zhi lau on 5 May 2020
Hi Kalyan and Mrutyunjaya,
Thanks so much for answering. The 'white percentage' counter does show the number of white pixels and i can just minus the difference to find the red but I was wondering that if i shift the view of the 3D plot, would the percentages change since Image = getframe() only takes the 2D picture and analyse it after? If it does, how am i supposed to find the exact number of red pixels?
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 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

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

darova on 6 May 2020
White volume looks simple
white = 40*40*40 - red 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 ?
darova on 6 May 2020
yes.

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');

sui zhi lau on 7 May 2020
I tried this but I think the totalData does not reflect the values correctly as for example, Gives me a red percentage of 1614%
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
nmesh = 5;
niter = 100;
scale = 0.5;
y = 1;
totalData = 0;
%%
d = 3;
[X,Y,Z] = meshgrid(0:nmesh-1); % create mesh
X = X * d
Y = Y * d
Z = Z * d
whitePixData = (d*(nmesh-1))^3;
%%
dx = normrnd(0,1,[nmesh^3 niter]);
dy = normrnd(0,1,[nmesh^3 niter]);
dz = normrnd(0,1,[nmesh^3 niter]);
cla
%plot3(X(:),Y(:),Z(:),'ob') % plot starting points
hold on
%plot(X,Y,'k') % plot grid
%plot(X',Y','k')
for i = 1:niter-1
s=5;
%s = normrnd(45,1)
X1 = X(:) + dx(:,i);
Y1 = Y(:) + dy(:,i);
Z1 = Z(:) + dz(:,i);
q = quiver3(X1, Y1, Z1, dx(:,i+1)-dx(:,i), dy(:,i+1)-dy(:,i),dz(:,i+1)-dz(:,i),'r')
q.LineWidth = s;
%plot([X(:) X(:)]'+[dx(:,i+1) dx(:,i)]', ...
%[Y(:) Y(:)]'+[dy(:,i+1) dy(:,i)]','.-r')
pause(0.02)
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;
delete(findall(gcf,'type','text'))
txt=text(0.0,0.95,sprintf('Red Percentage = %0.3f%%',percentageofred(y)),'Units','normalized');
y = y+1;
xlim([0 d*nmesh])
ylim([0 d*nmesh])
zlim([0 d*nmesh])
end
hold off
rotate3d on
Mrutyunjaya Hiremath on 7 May 2020
because, we are not considering the elimination of overlapping area.
sui zhi lau on 7 May 2020
How would you consider with elimination of overlapping areas?