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

조회 수: 7(최근 30일)
sui zhi lau 2020년 5월 5일
댓글: sui zhi lau 2020년 5월 7일
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
##### 댓글 수: 5표시숨기기 이전 댓글 수: 4
sui zhi lau 2020년 5월 5일
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

댓글을 달려면 로그인하십시오.

### 답변(2개)

darova 2020년 5월 5일
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표시숨기기 이전 댓글 수: 8
darova 2020년 5월 6일
yes.

댓글을 달려면 로그인하십시오.

Mrutyunjaya Hiremath 2020년 5월 6일
@ 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표시숨기기 이전 댓글 수: 2
sui zhi lau 2020년 5월 7일
How would you consider with elimination of overlapping areas?

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!