Horizontal and verticical line detection of an image

2,189 views
Skip to first unread message

Meaw

unread,
Sep 18, 2013, 7:03:46 AM9/18/13
to jav...@googlegroups.com
I am sorry for asking this question here, Is there any method to detect horizontal and vertical line separately in an image like a table (having columns and rows)?
I have tried with  canny detection but couldn't find a correct method for detect them separately...can you please consider about this issue please.

Meaw

unread,
Sep 19, 2013, 11:41:42 PM9/19/13
to jav...@googlegroups.com
I have used this code here.. But it detect the both as together.and the letters inside the table.
I really wanted is to detect the vertical and horizontal line separately in two times,
 can anyone please consider about this issue please..I am really stuck here..Thank you








import javax.swing.JFrame;

import com.googlecode.javacpp.Pointer;
import com.googlecode.javacv.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;


public class SignatureDetector {

   private static String SOURCE_FILE = "images\\Source.jpg";
   private static String HOUGH_FILE = "images\\houghrImage.jpg";
    public static void main(String[] args) {

        String fileName = args.length >= 1 ? args[0] : "img\\ROI.jpg"; // if no params provided, compute the defaut image
        IplImage src = cvLoadImage(fileName, 0);
        IplImage dst;
        IplImage colorDst;
        CvMemStorage storage = cvCreateMemStorage(0);
        CvSeq lines = new CvSeq();

        CanvasFrame source = new CanvasFrame("Source");
        CanvasFrame hough = new CanvasFrame("Hough");
        if (src == null) {
            System.out.println("Couldn't load source image.");
            return;
        }

        dst = cvCreateImage(cvGetSize(src), src.depth(), 1);
        colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3);

        cvCanny(src, dst, 50, 200, 3);
        cvCvtColor(dst, colorDst, CV_GRAY2BGR);

        /*
         * apply the probabilistic hough transform
         * which returns for each line deteced two points ((x1, y1); (x2,y2))
         * defining the detected segment
         */
        if (args.length == 2 && args[1].contentEquals("probabilistic")) {
            System.out.println("Using the Probabilistic Hough Transform");
            lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, Math.PI / 18, 40, 1, 1);
            for (int i = 0; i < lines.total(); i++) {
                // Based on JavaCPP, the equivalent of the C code:
                // CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
                // CvPoint first=line[0], second=line[1]
                // is:
                Pointer line = cvGetSeqElem(lines, i);
                CvPoint pt1  = new CvPoint(line).position(0);
                CvPoint pt2  = new CvPoint(line).position(1);

                System.out.println("Line spotted: ");
                System.out.println("\t pt1: " + pt1);
                System.out.println("\t pt2: " + pt2);
                cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
            }
        }
       
        source.showImage(src);
        hough.showImage(colorDst);
       
        cvSaveImage(SOURCE_FILE, src);
        cvSaveImage(HOUGH_FILE, colorDst);

        source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

dlo...@googlemail.com

unread,
Sep 20, 2013, 12:47:50 PM9/20/13
to jav...@googlegroups.com
the function returns all the lines it detects, regardless of which orientation they have.
For you "horizontal" might mean "it is parallel to the x-axis" or it might mean "it's more aligned with the x-axis than it is with the y-axis".
You just have to check every line for it's angle in respect to the x-axis or the y-axis and decide for yourself if the angle to one of the axes is small enough for it to be a "row" or a "column" line.

Samuel Audet

unread,
Sep 21, 2013, 9:14:06 AM9/21/13
to jav...@googlegroups.com
What you are looking for sounds like the Hough transform for lines:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html

Meaw

unread,
Sep 21, 2013, 9:22:43 AM9/21/13
to jav...@googlegroups.com
Yeah..I use this one..this code detect vertical lines..But I want to detect horizontal lines also as separately...can I please have an idea about this..I have change the parameters but I couldnt get the expected result.....

code is......



import javax.swing.JFrame;

import com.googlecode.javacpp.Pointer;
import com.googlecode.javacv.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;


public class SignatureDetector {

   private static String SOURCE_FILE = "img\\Source6.jpg";
   private static String HOUGH_FILE = "img\\houghrImag6.jpg";

    public static void main(String[] args) {

        String fileName = args.length >= 1 ? args[0] : "img\\ROI.jpg"; // if no params provided, compute the defaut image
        IplImage src = cvLoadImage(fileName, 0);
        IplImage dst;
        IplImage colorDst;
        CvMemStorage storage = cvCreateMemStorage(0);
         CvSeq lines = new CvSeq();
    
        CanvasFrame source = new CanvasFrame("Source");
        CanvasFrame hough = new CanvasFrame("Hough");
        if (src == null) {
            System.out.println("Couldn't load source image.");
            return;
        }

        dst = cvCreateImage(cvGetSize(src), src.depth(), 1);
        colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3);

        cvCanny(src, dst, 180, 90, 3);

        cvCvtColor(dst, colorDst, CV_GRAY2BGR);

        /*
         * apply the probabilistic hough transform
         * which returns for each line deteced two points ((x1, y1); (x2,y2))
         * defining the detected segment
         */
     
            System.out.println("Using the Probabilistic Hough Transform");
            lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, 1, 180, 180, 3);
           // HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );

            for (int i = 0; i < lines.total(); i++) {
                // Based on JavaCPP, the equivalent of the C code:
                // CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
                // CvPoint first=line[0], second=line[1]
                // is:
               
                Pointer line = cvGetSeqElem(lines, i);
                CvPoint pt1  = new CvPoint(line).position(1);
                CvPoint pt2  = new CvPoint(line).position(0);
              
                //cvSobel(dst, colorDst,  0, 90, 3);

            
                System.out.println("Line spotted: ");
                System.out.println("\t pt1: " + pt1);
                System.out.println("\t pt2: " + pt2);
                cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
            }
       
       
        source.showImage(src);
        hough.showImage(colorDst);
       
        cvSaveImage(SOURCE_FILE, src);
        cvSaveImage(HOUGH_FILE, colorDst);

        source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


Reply all
Reply to author
Forward
0 new messages