画像処理をGPUでおこないたいのですが、関数がサポートされていないので同じ処理ができるよう代用したいです。im2bw,bwareaopen
显示 更早的评论
https://www.mathworks.com/matlabcentral/fileexchange/28757-tracking-red-color-objects-using-matlab にある
redObjektTrack.m 赤色追跡のプログラムをGPUで動かしたいのですが、gpuArray(image)で画像をGPUに取り込んだ後 im2bwで輝度のしきいを設け、バイナリーイメージに変換する関数と、bwareaopenでnピクセル未満を削除する関数が使えず、GPUで実行できません。
同じように動く関数の代用や他に良い方法があれば、教えていただきたいです。よろしくお願いします。
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(camera_name, camera_id, format);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=200)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.18);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
hold off
end
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
% Clear all variables
3 个评论
Walter Roberson
2019-12-18
Which function cannot be executed on the GPU ?
james,k
2019-12-18
Walter Roberson
2019-12-18
diff_im = im2bw(diff_im,0.18);
can be replaced by
diff_im = diff_im >= uint8(0.18*255);
Instead of using bwareaopen(), do the regionprops() and ask for Area as well, and in your loop, ignore the blobs with Area less than 300.
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Image Preview and Device Configuration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!