一个用Matlab编写的延时摄影可执行文件

受到实验室考古清理的启发编写了这一程序。在考古清理过程中,需要利用延时摄影技术对清理全过程进行详细的记录以便准确定位所有文物的确切位置。由于清理是一项长期且精细的工作,因此要求相机或摄像头等设备处于长时间待机的状态(可能需要待机数个月甚至一年)。考虑到相机感光元件的脆弱程度以及照片的需求量打算采用高清摄像头进行拍摄。

当然这一程序最终没有实际使用,由于摄像头清晰度并不满足要求。另一问题是定时的拍摄方式可能会使得大部分图片拍摄到清理人员,因此最终采用了手动控制相机的方式。

代码实现功能包括:指定拍摄间隔(含本次获取图像所用的时间)、指定存储路径、生成.avi 文件、指定视频帧速率、指定图像大小、GUI编写、给出摄像头预览。

由于GUI与代码均为Matlab编写,故顺利运行程序还需安装Matlab运行环境(可在生成APP时直接打包)。

附说明:

1. 规定预览和拍摄不能同时进行(我没有试过,在代码里直接把这种操作禁止了,会有弹窗警告的)

2. 存储路径选择既可以点按钮,也可以在文本框中直接编辑

3. 清零按钮起到急停作用,不退出程序,将所有参数清零,工作停止,获取最后一张照片,文件保存

4. 视频只能生成avi文件,视频时长和大小不需编辑,图像质量可压缩,视频的时长取决于帧速率

5. 需要选择正确的素材文件格式

6. 默认存储路径"C:\Program Files\NWPU\Yanshisheying\application\Yanshisheying.exe"

完成后的界面如下:

一个用Matlab编写的延时摄影可执行文件

 

编写完APP代码后直接用自带的打包APP按钮即可生成exe文件,十分简单,可以自行百度。

一个用Matlab编写的延时摄影可执行文件

直接粘APP代码了,关于界面格式的部分没有给出,可以自己根据需要设置,而且也是可以自动生成的。只给出了各个按钮的函数,可根据需要调整。这个应用比较简单所以直接在应用中编写了所有函数而不再进行调用。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%This project was inspired by the research project relating to coffin M52 in Majiayuan%% 
%%, Gansu.                                                                             %%
%%By C_IkeyaSeki,                                                                      %%
%%Center for Materials and Conservation Research in Archaeology, NWPU.                 %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            dn=uigetdir('','请选择存储路径');
            if dn~=0
                app.TextArea.Value=dn;
                app.TextArea_3.Value='0';
                app.TextArea_2.Value='停止采集';
            end
        end

        % Value changed function: Button_2
        function Button_2ValueChanged(app, event)
            value = app.Button_2.Value;
            dn=app.TextArea.Value;
            if dn{1,1}==' '
                warndlg('请检查存储路径');
                app.Button_2.Value=0;
                return
            end
            per=app.EditField_3.Value+app.EditField_2.Value*60+app.EditField.Value*3600;%per为秒制时间间隔
            if per==0
                warndlg('请检查拍摄间隔输入');
                app.Button_2.Value=0;
                return
            end
            t=timer('Name','buttontimer','TimerFcn',{@getphoto,dn},'Period',per,'ExecutionMode','fixedSpacing');%每隔指定时间
            function vid=getphoto(buttontimer,ThisEvent,dn)
                imaqreset;
                vid=videoinput('winvideo',1);
                frame=getsnapshot(vid);
                fname=datestr(now,30);
                dnn=strcat(dn,'\',fname,'.jpg');
                %dnnew=sscanf(dnn,'%c');
                %disp(dnn{1,1});
                %disp(dnnew);
                imwrite(frame,dnn{1,1});
                app.TextArea_3.Value=num2str(get(t,'TasksExecuted'));
            end
            %t.TasksToExecute = 5;
            if value~=0
                start(t);
                app.TextArea_2.Value='采集中';
            else
                ts=timerfind('Name','buttontimer');
                stop(ts);
                app.TextArea_2.Value='停止采集'; 
                delete(ts); 
                return
            end
        end

        % Button pushed function: Button_3
        function Button_3Pushed(app, event)
            %急停按钮,停止所有记录,保存文件,不关闭预览窗口
            app.TextArea_3.Value='0';
            app.TextArea_2.Value='停止采集';
            app.TextArea.Value=' ';
            app.EditField_3.Value=0;
            app.EditField_2.Value=0;
            app.EditField.Value=0;
            app.Button_2.Value=0;
            ts=timerfind('Name','buttontimer');
            if ~isempty(ts)
                stop(ts);
            end
        end

        % Button pushed function: Button_4
        function Button_4Pushed(app, event)
            dn=uigetdir('','请选择存储路径');
            if dn~=0
                app.TextArea_6.Value=dn;
            end
        end

        % Button pushed function: Button_5
        function Button_5Pushed(app, event)
            dn=strcat(app.TextArea_7.Value,'\');
            aname=strcat(app.TextArea_6.Value,'\',app.TextArea_4.Value);
            [times,MBS]=VideofromPics(dn{1,1},app.DropDown.Value,aname{1,1},app.EditField_4.Value,app.EditField_5.Value);
            app.MBTextArea.Value=[num2str(MBS) 'MB'];
            if times<60
                app.TextArea_5.Value=[num2str(times) '秒'];
            else if times>=60&&times<3600
                    sec=rem(times,60);
                    min=(times-sec)/60;
                    app.TextArea_5.Value=[num2str(min) '分' num2str(sec) '秒'];
            else
                sec1=rem(times,3600);
                h=(times-sec1)/3600;
                sec=rem(sec1,60);
                min=(sec1-sec)/60;
                app.TextArea_5.Value=[num2str(h) '小时' num2str(min) '分' num2str(sec) '秒'];
            end
            end
            
        end

        % Button pushed function: Button_6
        function Button_6Pushed(app, event)
            dn=uigetdir('','请选择素材所在文件夹');
            if dn~=0
                app.TextArea_7.Value=dn;
            end
        end

        % Button pushed function: Button_8
        function Button_8Pushed(app, event)
            if app.Button_2.Value==1
                warndlg('正在采集中,请先停止采集,或关闭此窗口');
                return
            end
            imaqreset;
            vid=videoinput('winvideo',1);
            preview(vid);
            if app.Button_2.Value==1
                closepreview;
                return
            end
        end
    end