vid0bj = VideoReader('image1-1.avi');

mkdir workingDir
ii = 1;
img = zeros(256, 576, 122);

while hasFrame(vid0bj)
    img(:, :, ii) = readFrame(vid0bj);
    filename = [sprintf('%03d', ii) '.jpg'];
    fullname = fullfile('workingDir', filename);
    imwrite(img(:,:,ii), fullname)
    
    ii = ii+1;
end


imageNames = dir(fullfile('workingDir', '*.jpg'));
imageNames = {imageNames.name}';

outputVideo = VideoWriter(fullfile('workingDir', 'image1-1kai.avi'));
outputVideo.FrameRate = vid0bj.FrameRate;
open(outputVideo)



NumFrame = length(imageNames);
%SampleRate = round(NumFrame/30);
%jj = 0;



for ii = 1:NumFrame
    
    img(:, :, ii) = imread(fullfile('workingDir', imageNames{ii}));
    BW = edge(img(:,:,ii), 'Canny');
    NZ = BW.^2;
    BT = NZ(:);
    SM(ii) = sum(BT);
    A = SM(ii)
end



for ii = 1:4:NumFrame
    I1 = img(:, :, ii);
    I2 = img(:, :, ii+1);
    I3 = img(:, :, ii+2);
    I4 = img(:, :, ii+3);
    
    if max(A) == SM(ii)
        writeVideo(outputVideo, I1);
    end
    
     if max(A) == SM(ii+1)
        writeVideo(outputVideo, I2);
     end
    
      if max(A) == SM(ii+2)
        writeVideo(outputVideo, I3);
      end
    
       if max(A) == SM(ii+3)
        writeVideo(outputVideo, I4);
       end
    
       ii = ii+1;
end

    
    

close(outputVideo)

vid0bjavi = VideoReader(fullfile('workingDir', 'image1-1kai.avi'));

ii = 1;
while hasFrame(vid0bjavi)
    mov(ii) = im2frame(readFrame(vid0bjavi));
    ii = ii+1;
end

figure
imshow(mov(1).cdata, 'Border', 'tight')

movie(mov,1,vid0bjavi.FrameRate)





    
    