大家学了图像处理之后,是不是就想学以致用。而要用于生活中,最好的方式就是和摄像头搭配,实现实时图像处理。今天就给大家分享MATLAB实时图像处理的基础方法,希望能帮你快速入门。
by 今天不飞了
视频教学地址
《玩儿起来吧》MATLAB实时图像处理(一)如何获取实时图像 《玩儿起来吧》MATLAB实时图像处理(二)如何实时处理图像 《玩儿起来吧》MATLAB实时图像处理(三)最简单的人脸检测与追踪 《玩儿起来吧》MATLAB实时图像处理(四)目标追踪
一、如何获取实时图像
- 有摄像头的小伙伴
这篇博客有基本的摄像头配置方法 《MATLAB摄像头实时图像处理》
%% 配置摄像头
% -----------------------------------------
% 查看适配器
disp(imaqhwinfo)
% 查看设备及其支持的格式
info = imaqhwinfo('winvideo');
disp(info.DeviceInfo.SupportedFormats)
% --------你要是知道就不要运行这两句-------
%% 生成对象并同步画面
obj = videoinput('winvideo',1,'MJPG_640x480');
h = preview(obj);
%% 实时显示(处理)
figure('Position',[800,400,1000,500])
while ishandle(h)
tic
% 获取影像
frame = getsnapshot(obj); % 获取帧
% 一顿疯狂处理
img = rgb2gray(frame);
% 显示
subplot(121),imshow(frame)
subplot(122),imshow(img)
drawnow
t = toc;
disp(round(1/t))
end
- 没有摄像头的小伙伴
看这里【MATLAB】没有摄像头怎么做实时图像处理
二、如何实时处理图像
答案就是,利用while语句,对获取到的影像“一顿疯狂处理”。 请自行加入自己的算法。
%% 实时显示(处理)
% 生成对象并开启摄像机
obj = videoinput('winvideo',1,'MJPG_640x480');
h1 = preview(obj);
h2 = figure('Position',[100,50,1680,800]);
% 开始
while ishandle(h1)&&ishandle(h2)
tic
% 获取影像
frame = getsnapshot(obj); % 获取帧
frame = im2double(frame(:,1+80:end-80,:));
% 一顿疯狂处理
% --------------------------------
img1 = DoSomethingCrazy1(frame);
img2 = DoSomethingCrazy2(frame);
img3 = frame.*img2*2;
% --------------------------------
img = cat(2,img1,cat(3,img2,img2,img2),img3);
% 保存
imshow(img)
drawnow
t = toc;
disp(round(1/t))
end
function ed = DoSomethingCrazy1(frame)
% 归一化彩色空间
gray = rgb2gray(frame);
mask = double(gray>0.05);
imsum = sqrt(sum(frame.^2,3));
ed = frame./imsum.*mask;
end
function ed = DoSomethingCrazy2(frame)
% 灰度边缘
core1 = [1,1,1;0,0,0;-1,-1,-1;];
core2 = [1,1,0;1,0,-1;0,-1,-1;];
frame = rgb2gray(frame);
im1 = imfilter(frame,core1);
im2 = imfilter(frame,core1');
im3 = imfilter(frame,core2);
im4 = imfilter(frame,core2');
ed = max(abs(cat(3,im1,im2,im3,im4)),[],3);
end
三、简单的人脸识别
核心函数vision.CascadeObjectDetector ,使用方法演示代码如下
function findme()
% 创建GUI
Fig = figure('Position',[100,150,980,500]);
Pnl1 = uipanel(Fig,'Position',[0.05,0.17,0.9,0.8]);
Pnl2 = uipanel(Fig,'Position',[0.05,0.05,0.9,0.1]);
Axes1 = axes(Pnl1,'Position',[0,0,1/2,1]);
Axes2 = axes(Pnl1,'Position',[1/2,0,1/2,1]);
Bt = uicontrol(Pnl2,'style','togglebutton','String','人脸检测','Fontsize',16,...
'Units','normalized','Position',[2/5,0,1/5,1],'Callback',@FaceDetection);
drawnow
% 开启摄像头
Hcamera = [];
Hobj = [];
if isempty(Hcamera)
Hobj = videoinput('winvideo',1,'MJPG_640x480');
Hcamera = preview(Hobj);
frame = getsnapshot(Hobj); % 获取帧
[rows,cols,~] = size(frame);
end
% 人脸识别
flag = 0;
faceDetector = vision.CascadeObjectDetector();
gap = 2;
% 画
[Face,rp,cp] = drawface();
% 开始
while 1
if ishandle(Hcamera)
% 获取影像
frame = getsnapshot(Hobj); % 获取帧
frame = im2double(frame);
% 显示
imshow(frame,'Parent',Axes1)
% 检测
if flag
bboxx = face(frame);
if ~isempty(bboxx)
Px = bboxx(1,1);
Py = -bboxx(1,2);
Face.XData = Px;
Face.YData = Py;
end
hold(Axes1,'on')
for i = 1:size(bboxx,1)
bbox = bboxx(i,:);
rc = bbox+[-bbox(3)/4,-bbox(4)/4,bbox(3)/2,bbox(4)/2];
rectangle('Position',rc,'Curvature',0,...
'LineWidth',2,'LineStyle','--',...
'EdgeColor','y','Parent',Axes1)
end
hold(Axes1,'off')
end
drawnow
else
break
end
end
function FaceDetection(~,~)
flag = get(Bt,'Value');
end
function bboxx = face(frame)
frame = frame(1:gap:end,1:gap:end,:);
bboxx = step(faceDetector, frame);
bboxx = bboxx*gap;
end
function [h,rp,cp] = drawface()
rp = cols/2;
cp = -rows/2;
% 绘制
h = plot(rp,cp,'ro','MarkerSize', 35);
hold off
axis equal
axis([0,cols,-rows,0,0,1])
axis off
view([0,0,1])
set(gca,'looseInset',[0 0 0 0])
end
end
想加入b站表情包,看这篇把B站表情包植入MATLAB
四、简单的目标追踪
核心函数vision.PointTracker('MaxBidirectionalError',1) ,[point,validity] = tracker(frame) ,使用方法演示代码如下
function findme()
% 创建GUI
Fig = figure('Position',[500,450,980,500]);
Pnl1 = uipanel(Fig,'Position',[0.05,0.17,0.9,0.8]);
Pnl2 = uipanel(Fig,'Position',[0.05,0.05,0.9,0.1]);
Axes1 = axes(Pnl1,'Position',[0,0,1/2,1]);
Axes2 = axes(Pnl1,'Position',[1/2,0,1/2,1]);
Bt = uicontrol(Pnl2,'style','togglebutton','String','锁定目标','Fontsize',16,...
'Units','normalized','Position',[2/5,0,1/5,1],'Callback',@LockTarget);
drawnow
% 开启摄像头
Hcamera = [];
Hobj = [];
if isempty(Hcamera)
Hobj = videoinput('winvideo',1,'MJPG_640x480');
Hcamera = preview(Hobj);
frame = getsnapshot(Hobj); % 获取帧
end
% 人脸识别
flag = 0;
tracker = vision.PointTracker('MaxBidirectionalError',1);
gap = 1;
objectRegion = [0,0,0,0];
objFrame = [];
points = [];
% 开始
while 1
if ishandle(Hcamera)
% 获取影像
frame = getsnapshot(Hobj); % 获取帧
frame = im2double(frame);
if flag
% 检测
point = face(frame);
objimg = insertMarker(frame,point,'+','Color','green');
imshow(objimg,'Parent',Axes1)
else
% 普通显示
imshow(frame,'Parent',Axes1)
end
drawnow
else
break
end
end
function LockTarget(~,~)
% 降采样
frame = frame(1:gap:end,1:gap:end,:);
objFrame = frame;
% 确定目标位置
[x,y,~] = ginput(2);
objectRegion = [min(x),min(y),max(x)-min(x),max(y)-min(y)]/gap;
% 检测特征点
points = detectMinEigenFeatures(rgb2gray(frame),'ROI',objectRegion);
pointImage = insertMarker(frame, points.Location,'+','Color','green');
imshow(pointImage,'Parent',Axes2)
% 初始化跟踪器
release(tracker)
initialize(tracker, points.Location, frame);
flag = 1;
end
function point = face(frame)
% 降采样
frame = frame(1:gap:end,1:gap:end,:);
% 追踪
[point,validity] = tracker(frame);
point = point(validity,:);
% 更新目标
if size(point,1)<20
% 方法一
% x = point(:,1);
% y = point(:,2);
% objectRegion = [min(x),min(y),max(x)-min(x),max(y)-min(y)];
% points = detectMinEigenFeatures(rgb2gray(frame),'ROI',objectRegion);
% 方法二
release(tracker)
initialize(tracker, points.Location, objFrame);
[point,validity] = tracker(frame);
point = point(validity,:);
end
% 反算坐标
point = point*gap;
end
end
其他
- 不要直接照搬代码,你的摄像头型号参数不一定跟我一样,请根据实际情况修改。具体方法见以下视频或博客
《玩儿起来吧》MATLAB实时图像处理(一)如何获取实时图像 《MATLAB摄像头实时图像处理》 - 暂时没想到
|