资源简介

MATLAB代码,实现背景减除目标检测,并判断鱼头朝向,鱼头截取

资源截图

代码片段和文件信息

function [ZnumOfFishes] = video2Z(videograyThresholdareaThresholdhandles)
    seqName = handles.seqName;
    load([seqName‘/‘seqName‘_background.mat‘]‘background‘);
    background = double(background);
    
    totalframes = video.NumberOfframes;
    tam = [video.Heightvideo.Width];
    X = repmat(1:tam(2)[tam(1) 1]);%处理大矩阵:以1:tam(2)的矩阵内容堆叠在[tam(1) 1]中,最终矩阵大小1*tam(1):tam(2)*1
    Y = repmat((1:tam(1))‘[1 tam(2)]);
    
    % alloct space
    Z = struct(‘centerCoor‘[]‘imageCroped‘[]);
    Z(1totalframes).centerCoor = [];
    numOfFishes = 0;
    
%     folder = ‘OriginalPic‘;
%     if ~exist(folder‘dir‘)
%         dos([‘mkdir ‘folder]);
%     end
    meanValue = [];
    for frame_act=1:totalframes %num
        set(handles.txtInfo‘String‘[‘video2Z:  ‘ num2str(frame_act) ‘/‘ num2str(totalframes)]);
%         disp([‘video2Z:  ‘ num2str(frame_act) ‘/‘ num2str(totalframes)]);
        frame=read(videoframe_act);
        if size(frame3)==3
            frame =rgb2gray(frame);
        end
        if isempty(meanValue)
            meanValue = mean(mean(frame));
        end
        frame(frame>meanValue + 10) = meanValue + 10;
        
        frame_doub=double(frame); %视频大小
        imgFilter2D=abs(frame_doub-background)>grayThreshold;%灰度阈值
        
        % 椭圆拟合
        manchas=bwconncomp(imgFilter2D); %背景减除算法得到目标数量
        tams=cellfun(@(x) length(x)manchas.PixelIdxList);%求元胞数组manchas.PixelIdxList每个元胞的长度:length(x)
        % 筛选面积大于 areaThreshold 的部分
        listapixels=manchas.PixelIdxList(tams > areaThreshold);
        

        if length(listapixels)>numOfFishes
            numOfFishes = length(listapixels);
        end
        
        for c_buenos=1:length(listapixels)

            imgFilter2D=false(manchas.ImageSize);%生成逻辑0矩阵
            imgFilter2D(listapixels{c_buenos})=true;%返回值为1
%             ind=find(imgFilter2D);       %返回非零元素所在位置
            x = X(imgFilter2D);
            y = Y(imgFilter2D);
            lim=[min(x) max(x) min(y) max(y)];

            
            [coorXcoorY~~thetaaxisLongaxisShort]=minOuterRect(imgFilter2D0);
            Z(frame_act).centerCoor(:c_buenos)= [sum(coorX(1:4:))/4sum(coorY(1:4:))/4];     % 中心坐标
            if axisLong/axisShort >3
                %% 计算鱼头的方向
                [nRownCol~] = 

评论

共有 条评论