Showing posts with label addframe. Show all posts
Showing posts with label addframe. Show all posts

Saturday, 17 March 2018

Square Root Decomposition to Solve Sum of array elements Range Queries in Competitive Programming

#include<iostream>
#include<cmath>
using namespace std;

int main() {
  int a[] ={1,5,2,4,6,1,3,5,7},n=9;  
  int bs=sqrt(n); //block_size
  int nob=ceil((double)n/bs);//number of blocks
  int res[nob],j=0,sum; //res array stores the block sum
  //printf("%d",nob);
  for(int i=0;i<nob;i++)
  {
      sum=0;
      for(int c=0;c<bs;c++,j++)
        sum=sum+a[j];
      res[i]=sum;  //updating block sum
  }
 //for(int i=0;i<nob;i++)
 //cout<<res[i]<<" ";
  int noq,ad,b_id;
  cin>>noq;
  while(noq--)
  {
      int l,r,opt;
      cin>>opt>>l>>r;
    // l--;r--;     
      switch(opt)
      {
          case 1: b_id=l/bs;
                  res[b_id]=res[b_id]-a[l]+r;
                  a[l]=r;
                  break;
          case 2:  ad=0;
                    while(l<r && l%bs != 0 && l!=0 ){
                        ad=ad+a[l];l++;}

                     while(l+bs-1<=r)
                    {
                        ad=ad+res[l/bs];  //l/bs will give the block index
                        l=l+bs;
                        // cout<<ad<<endl;
                    }
                    for(;l<=r;l++)
                        ad=ad+a[l];
                     cout<<ad<<endl;
      }

 }
  return 0;
}

Saturday, 12 July 2014

making a video out of images in matlab

=====================In older versions of MATLAB========================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
aviObj = avifile('C:\test.avi', 'Compression', 'MSVC'); %test.avi is the name of the video and gets stored
%addframe cannot be used for true color images, so convert to gray scale and then add 
%video quality is not good
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
gr=rgb2gray(img);
aviObj = addframe(aviObj, gr);
end
aviObj=close(aviObj);

===================In newer versions of MATLAB===========================
 clear all;
close all;
samples_dir='C:/data'; % data is folder name that has images

Files = dir([samples_dir , '\*.jpg']);

VideoFile='C:\data\MyVideo'; %path of the video file to be saved
writerObj = VideoWriter(VideoFile);
writerObj.FrameRate =1; %increase the frame rate, here i have used 1 frame per second
open(writerObj);
for t= 1:length(Files)
Frame=imread(strcat('C:\data','\',Files(t).name));
writeVideo(writerObj,im2frame(Frame));
end
close(writerObj);

%To play the video

videoFReader = vision.VideoFileReader('C:\data\MyVideo.avi');
videoPlayer = vision.VideoPlayer;
while ~isDone(videoFReader)
   frame = step(videoFReader);
   step(videoPlayer,frame);
end
release(videoFReader);
release(videoPlayer);
                                                  (or)
implay('C:/data/MyVideo.avi');
=============================================================================
Program to read images in a directory as a video
===========================================================================
samples_dir='C:/data'; % data is folder name that has images
files = dir([samples_dir , '\*.jpg']);
hVPlayer=vision.VideoPlayer;
for i=1:length(files)
f_bmp=[samples_dir '\' files(i).name];
img=imread(f_bmp);
step(hVPlayer,img);
end
release(hVPlayer);

====Reading a 'mpg' video inbuilt in MATLAB and displaying the 36th,72nd,108th and 141th frame.======
clear all;
close all;
videobj=mmreader('xylophone.mpg');
numframes=get(videobj,'numberofframes');

x=read(videobj,36);
y=read(videobj,72);
z=read(videobj,108);
k=read(videobj,141);
subplot(221);imshow(x);title('36th frame');
subplot(222);imshow(y);title('72th frame');
subplot(223);imshow(z);title('108th frame');
subplot(224);imshow(k);title('141th frame');
=====================================================================