Parallel operation processing using parfor for fast operation
조회 수: 6 (최근 30일)
이전 댓글 표시
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('http://192.168.0.51:8090/?action=stream');
vid2 = ipcam('http://192.168.0.52:8090/?action=stream');
%%
win = 50;
while inf
tic
% 카메라 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,:);
end
end
end
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);
[j1,i1]=find(img_skel_1==1);
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);
[j2,i2]=find(img_skel_2==1);
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];
figure(1)
plot(x,y,'.','MarkerFaceColor',[0,0,1])
toc
end
댓글 수: 2
Raymond Norris
2022년 12월 14일
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?
채택된 답변
Alvaro
2023년 1월 23일
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.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!