Saturday, November 15, 2014

Reading table from word document with multiple paras in cell with Apache POI

In previous post I explained about the reading of text from table having only one paragraph. But always this is not the case. Lot of documents daily we are processing has multiple paragraphs in cell. By using the method described in earlier we will get all the text from the cell. But we will miss the formatting of the paragraphs. If we want to get the individual paragraphs from the cell we can use the below line of code.

for ( XWPFParagraph para : cell.getParagraphs() )
{
}

The above for loop will give the one para at every iteration from the cell in the order they are typed in table cell.

The consolidated code to get all the text from the table with paragraphs is

fis = new FileInputStream("D:/POI/ex1.docx");
            doc = new XWPFDocument(fis);
            tables = doc.getTables();
            for ( XWPFTable table : tables )
            {
                for ( XWPFTableRow row : table.getRows() )
                {
                    for ( XWPFTableCell cell : row.getTableCells() )
                    {
                            for ( XWPFParagraph para : cell.getParagraphs() )
                            {
                                    System.out.println(para.getText());
                            }
                    }
                    System.out.println("");
                }
            }

Reading table data from word document with Apache POI frame work

In previous posts reading word document I explained how we can use POI frame work to read the plain text data from the word document. Always this is not the case some times we may encounter tables in the document.
POI frame work has the built in support to read the tables data. It is so nice that we can read the data row by row and cell by cell. Java collection framework made it so easy to iterate over the table. Irrespective of the number of tables in the word document we can process the word document.
It is so simple to write Java code for reading the tables data. we want to read the table from word document so we need one input stream to open the word document for us. The Java code to accomplish this will look like

FileInputStream fis = new FileInputStream("")

After getting the data stream we need to use this in POI frame work. Just get the word document object in Java code by creating the object of XWPFDocument by passing the object of FileInputStream as the argument of the XWPFDocument constructor.

We will get the object of the word document. The following LOC will do this

XWPFDocument doc = new XWPFDocument(fis)

The object supports lot of methods to work on word document. In this post I will explain how to use this object to get the data present in the table. It is so simple by using the getTables method on the XWPFDocument object we will get all the tables of the word document which we are loaded in FileInputStream. This implies that we need one collection to hold all the tables. In fact the getTables method will return the List of the tables. This look like

List<XWPFTable>  tables = doc.getTables()

After getting the list of tables it is easy for us to iterate through all the tables of the word document. You may get doubt the size of the tables in the document may vary then how we can iterate with simple loop. It is so easy we are no where hard coding the number of columns or the number of rows of the table.

Every table object has the methods to get the number of rows and columns of the table. We can use Java for all loop to achieve this easily. The code to iterate through the tables look like

for ( XWPFTable table : tables )
{


The code inside the above for loop can use table(one table returned by the for loop from the list) as the object pointing the real table in word document. This table object has so many methods one is to get the number of rows of the table

for ( XWPFTableRow row : table.getRows() )
{
}

we can use row the local variable of the above for loop to get cell and in turn to get the data present in that cell.

for ( XWPFTableCell cell : row.getTableCells() )
{
   System.out.print(cell.getText());
}

The method getText will return the text data by removing any formatting of the text.
The following code snippet is useful for reading and printing the table data as table on the Java console.
fis = new FileInputStream("D:/POI/ex1.docx");
            doc = new XWPFDocument(fis);
            tables = doc.getTables();
            for ( XWPFTable table : tables )
            {
                for ( XWPFTableRow row : table.getRows() )
                {
                    for ( XWPFTableCell cell : row.getTableCells() )
                    {
                        System.out.print(cell.getText());
                        System.out.print("\t");
                    }
                    System.out.println("");
                }
            }
It looks like so simple but we have one problem in  this code that is the formatting of the data is lost. And the cell may contain multiple paragraphs and tables as well we will see how to get the data with out lose of formatting in the next post.

Saturday, September 27, 2014

Reading documents containing Images with POI

In previous post on POI frame work I discussed on reading the word docs having only paragraphs having text. This is not very useful cause lot of documents will have formatting contains Images tables etc.
For  example consider document having images in between text paragraphs. Every object embedded inside the document can be accessed as the paragraph even Images. So these are also become part of paragraph. While accessing the document from the code some times we are not interested in images. by using following code we will get only the text containing in the paragraphs.

try
        {
            FileInputStream fis = new FileInputStream("");
            HWPFDocument doc = new HWPFDocument(fis);
            Range range = doc.getRange();
            int numOfParas = range.numParagraphs();
            for(int i = 0; i < numOfParas; i++)
            {
                Paragraph para = range.getParagraph(i);           
                if(!para.text().trim().equalsIgnoreCase(""))
                {
                    System.out.println(para.text());
                }
            }
        }
        catch(FileNotFoundException fnfe)
        {
            fnfe.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }

This is same as the code used to read the simple word docs without any images. Just we added one condition while displaying the text. If the paragraph contains image only then there will not be any text if we run the .trim().equalsIgnoreCase("") on this paragraph text we will get Boolean true as result because there is not text.

Readind word document with Apache POI

Reading the files is very easy with Java. we can read any type of file. But each file has it's own formatting and the features. consider word document and the excel sheet each has it's own formatting. we can use buffered reader and any other type of readers available in java but these are not feasible while handling the documents containing lot of formatting.
We have sophisticated framework developed by Apache called POI to process the any type of document generated using MS office. Apache provides the ready made jars just we have to include them in our class path and we can use the functionality provide by them.
Following example provides the very basic functionality of reading the word document containing single paragraph single line.

public static void main(String[] args)
    {
        try
        {
            FileInputStream fis = new FileInputStream("C:/Practice/Reading.doc");
            HWPFDocument doc = new HWPFDocument(fis);
            Range range = doc.getRange();
            int numOfParas = range.numParagraphs();
            for(int i = 0; i < numOfParas; i++)
            {
                Paragraph para = range.getParagraph(i);
                System.out.println(para.text());
            }
        }
        catch(FileNotFoundException fnfe)
        {
            fnfe.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

HWPFDocument is the wrapper containing all the data structures of the word document. The variable doc of type HWPFDocument points to the instance of the word document pointed by the HWPFDocument class. HWPFDocument takes the File or path to the word document as string. The variable range is of type Range contains all the data of the word document except the header and footer section. By using range we can read all the data present in the word document. The method numParagraphs employed on range gives the total number of paragraphs of the word document. getParagraph  method returns the paragraph of provided index.

Saturday, June 21, 2014

Pulse Width Modulation with Microcontroller

Pulse width modulation is the type of modulation where we alter the duty cycle of the square wave. we vary the on period and the off period of the square wave to generate the variable duty cycle. This ultimately changes the average voltage output of the square wave.


we can simulate the above behaviour using micro controller programmed to alter the voltage of the output pin. For certain period of the time the output will be heigh and for some period it will be low causing square wave generation. we can alter the time for which the output is heigh this simulates the PWM behaviour.

The circuit has four switches each one will change the on period of the square wave. based on the closed switch on period will be changed in the micro controller. The code which does this as fallows:

#include <regx52.h>
#define OutPut P2_0
#define Sw1 P1_0
#define Sw2 P1_1
#define Sw3 P1_2
#define Sw4 P1_3
int Period = 0;

void delay1ms()
{                        int c=0;
while(c<1000)
            {
                          TMOD=0x01;
                          TH0=0xFC;
                          TL0=0x66;
                          TR0= 1;
                          while(!TF0);
                                    TR0=0;
                                    TF0=0;
                                     c++;
            }
}

void main()
{
int j,i;

Sw1 = 1;
Sw2 = 1;
Sw3 = 1;
Sw4 = 1;

OutPut = 0;

while(1)
{
  if(!Sw1)
  {
  Period = 1;
  }
  if(!Sw2)
  {
  Period = 2;
  }
  if(!Sw3)
  {
Period = 3;
  }
  if(!Sw4)
  {
Period = 4;
  }
  OutPut = 1;
  for(i=0;i<Period;i++)
  {
  delay1ms();
  }
  OutPut = 0;
  for(j=0;j<4-Period;j++)
  {
  delay1ms();
  }

}
}

Total time period of the square wave is 4 seconds. Timer is used to generate the 1 ms delay. 

Saturday, May 10, 2014

Extract frame from video when user presses a key in Open CV

Capturing the frames from video using Open CV is very simple. Just we need to use cvSaveImage method from Open CV library. But some times we need to capture the images interactively like when the user presses a key. For doing this we can use the function kbhit(). this function returns non zero values when there is something in the keyboard buffer. So by using this we can interactively extract the frames.

By using the fallowing code we can accomplish this.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
IplImage * img;
CvCapture * v;
v = cvCreateFileCapture("E:/v.mp4");
int k = 0;
char text[10];
while(true)
{
if(_kbhit())
{
sprintf(text,"%s%d%s","E:/Image",k,".jpeg");
cvSaveImage(text,img);
getch();
}
else
{
img= cvQueryFrame(v);
cvWaitKey(0);
k++;
}
}
_getch();
}

we have one while loop which continuously reads the frames from the video. The if block will be executed when the user presses a key from keyboard else else block gets executed. In else block we are extracting the frames from the video we have one delay loop as well else the the computer reads all the frames one by one with in fraction of seconds.

To change the saved image file name every time we are using char buffor. By using sprintf we are changing the file name. cvSaveImage saves the image on the specifed path supplied as first argument and the image to be saved as second argument. getch() is used to clear the buffor else the if block executed continuously.

Thursday, May 1, 2014

RGB to YUV format conversion using Open CV and C Language

You may think why we need to convert the image from one format to another format. There are lot of advantages if we convert the format for transmission as well as for display purpose. Some times processing of the one format of images i s easy compared to another. Generally images are processed by algorithms in YUV domain and then converted to RGB domain for Display purpose. While capturing the images camera uses RGB format b ut for storing we use YUV format for compression. When we need to display them we will again convert them to the RGB format. Generally YUV format images require the less band width compared to the RGB. Generally color correction is done in the RGB color space and contrast enhancement is done in the YUV color space. Y component used for carrying the brightness of the image and remaining two components are used for the color representation.

Fallowing code explains how we can achieve the above things:

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
    IplImage *img=cvLoadImage("E:/test.jpg");
    IplImage *Dimg=cvCreateImage(cvSize(img->width,img->height),img->depth,img->nChannels);
    //VUY
    IplImage *Y=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
    IplImage *U=cvCreateImage(cvSize(img->width,img->height),img->depth,3);
    IplImage *V=cvCreateImage(cvSize(img->width,img->height),img->depth,3);

    for(int i=0;i<img->width*img->height*3;i+=3)
    {
        int y=0.257*img->imageData[i+2]+0.504*img->imageData[i+1]+0.098*img->imageData[i]+16;
        int u=-0.148*img->imageData[i+2]-0.291*img->imageData[i+1]+0.439*img->imageData[i]+128;
        int v=img->imageData[i+2]*0.439-0.368*img->imageData[i+1]-0.071*img->imageData[i]+128;
        Dimg->imageData[i]=(v<255||v>0)?v:(v>255?255:0);
        Dimg->imageData[i+1]=(u<255||u>0)?u:(u>255?255:0);
        Dimg->imageData[i+2]=(y<255||y>0)?y:(y>255?255:0);

        Y->imageData[i+2]=Dimg->imageData[i+2];
        U->imageData[i+1]=Dimg->imageData[i+1];
        V->imageData[i]=Dimg->imageData[i];
    }

    cvNamedWindow("Y",0);
    cvResizeWindow("Y",300,300);
    cvNamedWindow("U",0);
    cvResizeWindow("U",300,300);
    cvNamedWindow("V",0);
    cvResizeWindow("V",300,300);
    cvShowImage("Y",Y);
    cvShowImage("U",U);
    cvShowImage("V",V);
    cvWaitKey(0);
    _getch();
}

While calculating the YUV components some times we will encounter the under flow as well as over flow. so we have to make sure that the code does not breaks while running. So we have to tap all these things in our code.
Above code loads the image in to the code using open cv. Then the individual channels are extracted and then they are converted to the YUV domain and stored in the newly created image. To get the difference between the YUV components we represented the each channel separately in separate windows.

Wednesday, April 30, 2014

Splitting the Image in to three channels with Open CV and C Language


In image processing some tines we need to separate the channels of image. This is needful to achieve the required contrast of the image. Because some times if we remove the one or more channels from the image we can get better understanding of the image under consideration. In some cases we have to process only few channels of the image rather than the entire image. Open cv provides the functions to achieve the same. We can do the same thing with out using the functions of the open cv.

Fallowing code snippet is useful for splitting the image channels:

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
    IplImage *Simg;
    IplImage *Rimg;
    IplImage *Gimg;
    IplImage *Bimg;

    int nrows,ncols;

    Simg=cvLoadImage("E:/test.jpg");
    Rimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);
    Gimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);
    Bimg=cvCreateImage(cvSize(Simg->width,Simg->height),Simg->depth,3);

    cvNamedWindow("Red",0);
    cvNamedWindow("Green",0);
    cvNamedWindow("Blue",0);
    cvResizeWindow("Red",320,320);
    cvResizeWindow("Green",320,320);
    cvResizeWindow("Blue",320,320);

    nrows=Simg->height;
    ncols=Simg->width;
    
     for(int i=0;i<nrows*ncols*3;i++)
    {
        Rimg->imageData[i]=0;
        Gimg->imageData[i]=0;
        Bimg->imageData[i]=0;
    }
    for(int i=0;i<nrows*ncols*3;i+=3)
    {   
            Rimg->imageData[i+2]=Simg->imageData[i+2];
            Gimg->imageData[i+1]=Simg->imageData[i+1];
            Bimg->imageData[i]=Simg->imageData[i];
     }
   
    cvShowImage("Red",Rimg);
    cvShowImage("Green",Gimg);
    cvShowImage("Blue",Bimg);
    cvWaitKey(0);
    _getch();
}

for example consider the image with three channels. Load that image into the memory with the help of Open CV. Next create the three images with the same size,depth and channels. Fill the entire image with the zeros means with black pixels. Now extract the individual channels from the original image and store them in the newly created images. Open cv changes the order of the pixels to BGR instead of RGB.
If we apply the ->height and ->width on the image in Open CV we will get the number of rows and columns. But actually if the image is color we have columns more than what we get. that is if we have width as 20 the actual columns on the disk is 60 because each channel will store in separate byte. In memory they will be stored in interleaved manner. So to get the red pixels we need to get the alternate pixels with the gap of two.
By running the above code on the below image:
  

we will get the fallowing images:
 





LED flashing simulation in multisim

In real world Applications LED's play important role. They are mainly used as indicators. In embedded industry they are used to indicate the events. Embedded world mainly depends on the two things. One is embeddable processor and the software top run it. Every processor or micro controller needs little bit of power to operate and some circuit connections to make it useful.
In this post I will explain how we can use 8051 micro controller to control the behavior of LED. The simulation is done in multisim.
lf

We need to apply proper voltages to the controller so that it can operate. It needs one power source and sink. Any normal battery can be used as the power supply and the negative terminal of the same battery can be used as ground. Just connect the LED to the any port pin of the controller. But avoid to use port1 because for that port we need to supply external pull up resistors. Remaining all ports has inbuilt pull up resistors.
Now coming to the actual intention of the post we need to toggle the LED. For some time it will be on after that it is off for the same amount of time. It can be achieved by altering the output of the port pin. The software written in assembly or embedded C. The code snippet as fallows:
#include <reg52.h>
sbit pin = P1^5;
bit state;
void init(void);
void changeState(void);
void Wait(const unsigned int);
void main()
{
init();
while(1)
{
changeState();
Wait();
}
}
void init()
{
state = 0;
}
void changeState()
{
if (state == 1)
{
state = 0;
pin = 0;
}
else
{
state = 1;
pin = 1;
}
}
void Wait()
{
unsigned y;
{
for (y = 0; y <= 100; y++);
}
}
Before loading this code in the micro controller we need to convert this code into .hex file to dump in controller. We can use keil for the same. We need to include the header regx52.h it has all the ports and functions defined in it. Pin 5 is configured to operate and to control the LED.
Program has three functions. First one initializes the led state to zero. And the wait function creates the delay for some period of time. For that period LED remains in on/off state only. Change state function changes the global variable value so that in the next call to the wait function the status of the LED changes. As use val we have on main function to start and run the program.

Saturday, April 26, 2014

Color Image Rotation in C++ and with Open CV

In previous post explains how we can apply rotation to gray scale image that is single channel images. In this post I will explain the same for multy channel images aka color images.

In real time scenarios some times we need to apply the geometric transformation on the images like rotation etc. This process includes rotating the entire image around the center of the image. It is better to map the rotated image on other image like blank image. In digital computers images are treated as the matrices or two dimensional vectors.

Int his process we find some image points falling on the outside of the boundaries. There are lot of procedures to deal with this type of problems. One is leaving those points. Second one is plotting the rotated image on the larger canvas.

In digital computer this process becomes simple matrix multiplication. In geometry as well this process is represented as the matrix operations. In image processing this type of transformations are called affine transforms.

Mathematically whole precess can be represented in two steps.

x2=cos(t)*(x1-x0)-sin(t)*(y1-y0)+x0
y2=sin(t)*(x1-x0)+cos(t)*(y1-y0)+y0

x2 is the new coordinate of the rotated image corresponding to x1 of original image similarly y2.
t is the required angle of rotation.  x0 and y0 or the center coordinates of the image.

The fallowing code snippet written in open cv and c++ does the exactly same.

 #include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>

using namespace cv;
using namespace std;

#define PI 3.14159265

void main()
{
    int angle;
    Mat img;
    img=imread("E:/test.jpg");
    Mat nimg((img.rows),(img.cols),CV_8UC3,Scalar(0));
    Mat tm(2,2,CV_32SC1);
    Mat nc(2,2,CV_32SC1);
    Mat oc(2,2,CV_32SC1);
    cout<<"Enter the Rotation angle\n";
    cin>>angle;
    float cosine=cos(angle*PI/180.0);
    float sine=sin(angle*PI/180.0);

    float cx=img.cols/2.0;
    float cy=img.rows/2.0;

    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            int nx=(cosine*(i-cx))-(sine*(j-cy))+cx;
            int ny=(sine*(i-cx))+(cosine*(j-cy))+cy;
            if((nx>=0)&&(ny>=0)&&(nx<=nimg.rows)&&(ny<=nimg.cols))
            {
                           for( int c = 0; c < 3; c++ )
                {
                nimg.at<Vec3b>(nx,ny)[c]=img.at<Vec3b>(i,j)[c];
                }
            }
        }
    }
    imshow("Image",nimg);
    waitKey(100);
     _getch();
}

The difference is just we iterate through the three channels. In Open CV we will get the pixel intensities with .at<Vec3b> array and by using index we can get the individual channel intensities.  

Sunday, April 13, 2014

Image Rotation with Open cv and C++



In real time scenarios some times we need to apply the geometric transformation on the images like rotation etc. This process includes rotating the entire image around the center of the image. It is better to map the rotated image on other image like blank image. In digital computers images are treated as the matrices or two dimensional vectors.

Int his process we find some image points falling on the outside of the boundaries. There are lot of procedures to deal with this type of problems. One is leaving those points. Second one is plotting the rotated image on the larger canvas.

In digital computer this process becomes simple matrix multiplication. In geometry as well this process is represented as the matrix operations. In image processing this type of transformations are called affine transforms.

Mathematically whole precess can be represented in two steps.

x2=cos(t)*(x1-x0)-sin(t)*(y1-y0)+x0
y2=sin(t)*(x1-x0)+cos(t)*(y1-y0)+y0

x2 is the new coordinate of the rotated image corresponding to x1 of original image similarly y2.
t is the required angle of rotation.  x0 and y0 or the center coordinates of the image.

The fallowing code snippet written in open cv and c++ does the exactly same.

#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <conio.h>
#include <math.h>

using namespace cv;
using namespace std;

#define PI 3.14159265

void main()
{
    int angle;
    Mat img;
    img=imread("E:/test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    Mat nimg(img.rows,img.cols,CV_8UC1,Scalar(0));
    Mat tm(2,2,CV_32SC1);
    Mat nc(2,2,CV_32SC1);
    Mat oc(2,2,CV_32SC1);
    cout<<"Enter the Rotation angle\n";
    cin>>angle;
    float cosine=cos(angle*PI/180.0);
    float sine=sin(angle*PI/180.0);

    float cx=img.cols/2.0;
    float cy=img.rows/2.0;


    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            int nx=(cosine*(i-cx))-(sine*(j-cy))+cx;
            int ny=(sine*(i-cx))+(cosine*(j-cy))+cy;
            if((nx>=0)&&(ny>=0)&&(nx<=img.rows)&&(ny<=img.cols))
            {
            nimg.at<uchar>(nx,ny)=img.at<uchar>(i,j);
            }
        }
    }
   imshow("Image",nimg);
   waitKey(100);
    _getch();
}

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...