MATLAB 图像处理实现追踪与标记 视频使用函数videoreader出错

MATLAB 图像处理实现追踪与标记 视频使用函数videoreader出错,第1张

应该是视频在前面,处理图像在后面吧? 你后面的那个是每隔5帧截取一张图片储存并以i命名。 想要连接起来其实比较简单旅森的,就是在处理图片处加一个大的for循环就可以了。 把视频程序放前面。截图储存后再读取处理,这里要注意的是你每隔5帧截一个图片,是不是有点太频了?还有弄个短点的视频,5秒左右。这样不会太浪费计纳源算机资源,如果太大了可能会蓝屏或者死机哦。 其实,连在一起很简单的。就这样;(我在我机子上运行了一次,储存地址改了,你自己改回来吧) clcclear allclose allmov = VideoReader('C:\Users\epwqe_000\Desktop\MatlabAsa\sssss.avi')%改地址 fnum = mov.NumberOfFrames% for i = 1:5:fnum frame = read(mov, i)imshow(frame) imwrite(frame,strcat('C:\Users\epwqe_000\Desktop\MatlabAsa\New Folder\',num2str(i),'.jpg'),'jpg')%改地址 img=strcat(num2str(i),'.jpg')I = imread(img) [Ix,Iy,Iz]=size(I)if Ix>400&Iy>300 I=imresize(I,[400,Iy*400/Ix],'nearest')end figure imshow(I)%输出图像 title('normal image') I=double(I)%将I转换成double类型 [hue,s,v]=rgb2hsv(I) %将RGB格式转换成HSV格式 cb=0.148*I(:,:,1)-0.291*I(:,:,2)+0.439*I(:,:,3)+128%将RGB转换为YCrCb格式 cr=0.439*I(:,:,1)-0.368*I(:,:,2)-0.071*I(:,:,3)+128 [w h]=size(I(:,:,1))%获取转化后的I图像 for i=1:w for j=1:h if 145<=cr(i,j)&cr(i,j)<=165&145<=cb(i,j)&cb(i,j)<=180&0.01<=hue(i,j)&hue(i,j)<=0.15 segment(i,j)=1elsesegment(i,j)=0end end end figure imshow(segment) skin=segment% 去除小像素联通区域 skin=bwareaopen(skin,round(w*h/900)) %dilating se=strel('square',5)%创建结构区域 skin=imdilate(skin,se)%膨胀作用im(:,:,1)=I(:,:,1).*skin im(:,:,2)=I(:,:,2).*skin im(:,:,3)=I(:,:,3).*skin figure imshow(uint8(im))title('skin areas') BW = skinL = bwlabel(BW,8)% BB = regionprops(L, 'BoundingBox'拆茄亩)%L等于几对应几 BB1=struct2cell(BB)%转换结构bb到bb1里 BB2=cell2mat(BB1)figure,imshow(uint8(I))title('result image') [s1 s2]=size(BB2)for k=3:4:s2-1 if (BB2(1,k)/BB2(1,k+1)) <1.8 &&.... (BB2(1,k)/BB2(1,k+1)) >0.4 &&.... (BB2(1,k)*BB2(1,k+1)) >1000 hold on rectangle('Position',[BB2(1,k-2),BB2(1,k-1),BB2(1,k),BB2(1,k+1)],'EdgeColor','r' )%指定位置添加矩形 end end end

obj = VideoReader(fileName)

numFrames = obj.NumberOfFrames

for i = 1 : numFrames

frame = read(obj,i)

bw = im2bw(frame,0.2)

[x,y] = find(bw==0)

x1 = min(x)%取边界

y1 = min(y)%取边界

x2 = max(x)%取边界毕岩备

y2 = max(y)%取边界

imshow(frame),hold on,rectangle('Position',[y1,x1,y2-y1,x2-x1],'edgecolor','r')%显示加叠加矩形框

imwrite(frame,strcat(num2str(i),'.jpg'),'jpg')%写每帧图片

end

上述代码中:

①调试看下numFrames =?是1还是n;如果n=1,那自然只有一帧;

②hold on所枣脊在那个句不要,看写每帧图片有几个图生成,如果有多个图就是卡手毁在显示的位置

下面是camshift物体追踪代码,需要你用avi视频测试,matlab对avi视频格式要求比较严格。但是可以试试mmreader函数读取视频。

% Adam Kukucka

% Zach Clay

% Marcelo Molina

% CSE 486 Project 3

function [ trackmov probmov centers ] = camshift

% ******************************************************************

% initialize vari ables

% ******************************************************************

rmin = 0%min row value for search window

rmax = 0%max row value for search window

cmin = 0%min col value for search window

cmax = 0%max col value for search window

numofframes = 0%number of frames in the avi

threshold = 1%threshold for convergence

centerold = [0 0]%for convergence... previous center of window

centernew = [0 0]%for convergence... new center of window

% ******************************************************************

% Pre code... load movie and select initial frame

% ******************************************************************

% prompt user for avi file name

%%%%%user_entry = input('Please enter an avi filename: ','s')

% load the avi file... handle is M

%%%%M = aviread(user_entry)

M=aviread('8888.avi')

% get number of frames

[dontneed numberofframes] = size(M)

% initialize matrix to hold center coordinates

imagecenters = zeros(numberofframes, 2)

% extract the first frame from the avi

Frame1 = M(1,1)

Image1 = frame2im(Frame1)

%%% ********** images(:, :, numberofframes) = G(:,:)

% get search window for first frame

[ cmin, cmax, rmin, rmax ] = select( Image1 )

cmin = round(cmin)

cmax = round(cmax)

rmin = round(rmin)

rmax = round(rmax)

wsize(1) = abs(rmax - rmin)

wsize(2) = abs(cmax - cmin)

% create histogram

% translate to hsv

hsvimage = rgb2hsv(Image1)

% pull out the h

huenorm = hsvimage(:,:,1)

% scale to 0 to 255

hue = huenorm*255

% set unit type

hue=uint8(hue)

% Getting Histogram of Image:

histogram = zeros(256)

for i=rmin:rmax

for j=cmin:cmax

index = uint8(hue(i,j)+1)

%count number of each pixel

histogram(index) = histogram(index) + 1

end

end

% ******************************************************************

% Algorithm from pdf

% ******************************************************************

aviobj1 = avifile('example3.avi')

aviobj2 = avifile('example4.avi')

% for each frame

for i = 1:200

disp('Processing frame')

disp(i)

Frame = M(1, i)

I = frame2im(Frame)

% translate to hsv

hsvimage = rgb2hsv(I)

% pull out the h

huenorm = hsvimage(:,:,1)

% scale to 0 to 255

hue = huenorm*255

% set unit type

hue=uint8(hue)

[rows cols] = size(hue)

% choose initial search window

% the search window is (cmin, rmin) to (cmax, rmax)

% create a probability map

probmap = zeros(rows, cols)

for r=1:rows

for c=1:cols

if(hue(r,c) ~= 0)

probmap(r,c)= histogram(hue(r,c))

end

end

end

probmap = probmap/max(max(probmap))

probmap = probmap*255

count = 0

rowcenter = 0 % any number just so it runs through at least twice

colcenter = 0

rowcenterold = 30

colcenterold = 30

% Mean Shift for 15 iterations or until convergence(the center doesnt

% change)

while (((abs(rowcenter - rowcenterold) >2) &&(abs(colcenter - colcenterold) >2)) || (count <15) )

%for j = 1:5

%disp('meanshift')

% disp(j)

rmin = rmin - 7 %increase window size and check for center

rmax = rmax + 7

cmin = cmin - 7

cmax = cmax + 7

rowcenterold = rowcenter%save old center for convergence check

colcenterold = colcenter

[ rowcenter colcenter M00 ] = meanshift(I, rmin, rmax, cmin,...

cmax, probmap)

% given image (I), search window(rmin rmax cmin cmax)

% returns new center (colcenter, rowcenter) for window and

% zeroth moment (Moo)

% redetermine window around new center

rmin = round(rowcenter - wsize(1)/2)

rmax = round(rowcenter + wsize(1)/2)

cmin = round(colcenter - wsize(2)/2)

cmax = round(colcenter + wsize(2)/2)

wsize(1) = abs(rmax - rmin)

wsize(2) = abs(cmax - cmin)

count = count + 1

end

% mark center on image

%save image

G = .2989*I(:,:,1)...

+.5870*I(:,:,2)...

+.1140*I(:,:,3)

trackim=G

%make box of current search window on saved image

for r= rmin:rmax

trackim(r, cmin) = 255

trackim(r, cmax) = 255

end

for c= cmin:cmax

trackim(rmin, c) = 255

trackim(rmax, c) = 255

end

aviobj1 = addframe(aviobj1,trackim)

aviobj2 = addframe(aviobj2,probmap)

%create image movie, and probability map movie

trackmov(:,:,i)= trackim(:,:)

probmov(:,:,i) = probmap(:,:)

% save center coordinates as an x, y by doing col, row

centers(i,:) = [colcenter rowcenter]

% Set window size = 2 * (Moo/256)^1/2

windowsize = 2 * (M00/256)^.5

% get side length ... window size is an area so sqrt(Area)=sidelength

sidelength = sqrt(windowsize)

% determine rmin, rmax, cmin, cmax

rmin = round(rowcenter-sidelength/2)

rmax = round(rowcenter+sidelength/2)

cmin = round(colcenter-sidelength/2)

cmax = round(colcenter+sidelength/2)

wsize(1) = abs(rmax - rmin)

wsize(2) = abs(cmax - cmin)

end

% end for loop

% Adam Kukucka

% Zach Clay

% Marcelo Molina

% CSE 486 Project 3

function [ rowcenter colcenter M00 ] = meanshift(I, rmin, rmax, cmin,...

cmax, probmap)

%inputs

% rmin, rmax, cmin, cmax are the coordiantes of the window

% I is the image

%outputs

% colcenter rowcenter are the new center coordinates

% Moo is the zeroth mean

% **********************************************************************

% initialize

% **********************************************************************

M00 = 0%zeroth mean

M10 = 0%first moment for x

M01 = 0%first moment for y

histdim = (0:1:255)% dimensions of histogram... 0 to 255, increment by 1

[rows cols] = size(I)

cols = cols/3% **********************8

% **********************************************************************

% Main code

% **********************************************************************

% determine zeroth moment

for c = cmin:cmax

for r = rmin:rmax

M00 = M00 + probmap(r, c)

end

end

% determine first moment for x(col) and y(row)

for c = cmin:cmax

for r = rmin:rmax

M10 = M10 + c*probmap(r,c)

M01 = M01 + r*probmap(r,c)

end

end

% determine new centroid

% x is cols

colcenter = M10/M00

% y is rows

rowcenter = M01/M00

% Adam Kukucka

% Zach Clay

% Marcelo Molina

% CSE 486 Project 3

function [ cmin, cmax, rmin, rmax ] = select( I )

%UNTITLED1 Summary of this function goes here

% Detailed explanation goes here

% for array... x is cols, y is rows

image(I)

k = waitforbuttonpress

point1 = get(gca,'CurrentPoint') %mouse pressed

rectregion = rbbox

point2 = get(gca,'CurrentPoint')

point1 = point1(1,1:2) % extract col/row min and maxs

point2 = point2(1,1:2)

lowerleft = min(point1, point2)

upperright = max(point1, point2)

cmin = round(lowerleft(1))

cmax = round(upperright(1))

rmin = round(lowerleft(2))

rmax = round(upperright(2))


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12407651.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存