Clear Filters
Clear Filters

Parallel operation processing using parfor for fast operation

1 view (last 30 days)
When processing images by accepting two ip cams, using parfor seems to make the calculation faster, but an error occurs.
In the code below, while inf I wonder how to change it to parfor and process it all at once.
An error occurs if the syntax is incorrect.
vid1 = ipcam('');
vid2 = ipcam('');
win = 50;
while inf
% 카메라 1 영상처리
img_1 = snapshot(vid1);
img1_mask = im2uint8(roipoly(img_1,[380 380 1100 1100],[0,720,720,0]))/255;
img_1 = img1_mask.*img_1;
% Get the snapshot of the current frame
[R C X] = size(img_1) ; % 이미지 행렬의 행(R) 과 열(C)의 값을 구한다.
for i = 1:R
for j = 1:C
if img_1(i,j,1) - img_1(i,j,2) < 25|| img_1(i,j,1) - img_1(i,j,3) < 35
img_R1(i,j,1) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
img_R1(i,j,2) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
img_R1(i,j,3) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
else img_R1(i,j,:) = img_1(i,j,:);
img_R1 = img_R1/255;
img_gray_1= rgb2gray(img_R1);
img_gray_1 = double(img_gray_1)/255; % data 형식을 uint8을 double 형식으로 변경
img_bw_1 = imbinarize(img_gray_1,'adaptive','ForegroundPolarity','dark','Sensitivity',0.9); %img_bw는 호환문제로 잘 쓰지 않음
img_bw_1 = bwareaopen(img_bw_1,400); % 크기가 작은 객체 제거
img_bw_1 = imfill(img_bw_1,'holes'); % 빈 공간 채우기
% image skeleton detection
se = strel('disk',1);
img_skel_1 = bwmorph(imdilate(bwskel(img_bw_1,'MinBranchLength',20),se),'skel',Inf);
x1 = movmean(i1,win);
y1 = movmean(j1,win);
% 카메라 2 영상처리
img_2 = snapshot(vid2);
img2_mask = im2uint8(roipoly(img_2,[252 252 1230 1230],[0,720,720,0]))/255;
img_2 = img2_mask.*img_2;
% Get the snapshot of the current frame
[R, C , ~] = size(img_2) ; % 이미지 행렬의 행(R) 과 열(C)의 값을 구한다.
for i = 1:R
for j = 1:C ;
if img_2(i,j,1) - img_2(i,j,2) < 25|| img_2(i,j,1) - img_2(i,j,3) < 35
img_R2(i,j,1) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
img_R2(i,j,2) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
img_R2(i,j,3) = 0; %(img(i,j,1)+img(i,j,2)+img(i,j,3))/3;
else img_R2(i,j,:) = img_2(i,j,:);
end ;
end ;
end ;
img_R2 = img_R2/255;
img_gray_2= rgb2gray(img_R2);
img_gray_2 = double(img_gray_2)/255; % data 형식을 uint8을 double 형식으로 변경
img_bw_2 = imbinarize(img_gray_2,'adaptive','ForegroundPolarity','dark','Sensitivity',0.9); %img_bw는 호환문제로 잘 쓰지 않음
img_bw_2 = bwareaopen(img_bw_2,400); % 크기가 작은 객체 제거
img_bw_2 = imfill(img_bw_2,'holes'); % 빈 공간 채우기
% image skeleton detection
se = strel('disk',1);
img_skel_2 = bwmorph(imdilate(bwskel(img_bw_2,'MinBranchLength',20),se),'skel',Inf);
x2 = movmean(i2,win);
y2 = movmean(j2,win);
x = rmmissing([x1; x2;]);
y = rmmissing([y1; y2;]);
% x = rmmissing([x1; x3; ]);
% y = rmmissing([y1; y3; ]);
% x = movmean(x,win);
% y = movmean(y,win);
[th1,r1] = cart2pol(x,y);
b = [th1, r1];
b = rmoutliers(b,'percentiles',[0 97]);
th = b(:,1);
r = b(:,2);
th = [th-2*pi; th; th+2*pi];
r = [r; r; r];
Raymond Norris
Raymond Norris on 14 Dec 2022
Are you asking to change the while loop to a parfor? Or are you asking how to rewrite some of your for-loops to a parfor-loop? Your while loop runs indefinitely. A parfor-loop would run for a limited number of iterations, so I don't see how to morph one to the other.
What error message is being thrown?
장훈 정
장훈 정 on 14 Dec 2022
First, I converted the while loop itself to parfor, but it was not executed. error massange following as;
ERROR: cannot assign a value to parfor-loop variable 'n' in a parfor-loop For more information, see "Loop Variables" in Parallel for Loops in MATLAB.
And I changed for in the while loop to parfor.
At upper picture, exclamation mark says that "Cannot execute PARFOR loop because of the way variable 'img_R1' is used."
and if you run
Error: parfor -loop cannot be used inside a parfor -loop. For more information, see "Nested parfor-Loops" in Parallel for Loops in MATLAB.
message appears.
I simply want to increase the computational speed. Please help!

Sign in to comment.

Accepted Answer

Alvaro on 23 Jan 2023
There are some considerations about variables that you need to take into account when using parfor loops.
You need to ensure that you are not violating any of those rules. Consider, however, that parallelization is not always going to make your computations go significantly faster than in serial.

More Answers (0)


Find more on Parallel for-Loops (parfor) in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!