RLPark 1.0.0
Reinforcement Learning Framework in Java

HoughLines.java

Go to the documentation of this file.
00001 package rlpark.plugin.opencv.samples;
00002 
00003 import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
00004 import static com.googlecode.javacv.cpp.opencv_core.CV_RGB;
00005 import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
00006 import static com.googlecode.javacv.cpp.opencv_core.cvCreateMemStorage;
00007 import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
00008 import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
00009 import static com.googlecode.javacv.cpp.opencv_core.cvLine;
00010 import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
00011 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_GRAY2BGR;
00012 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_HOUGH_MULTI_SCALE;
00013 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_HOUGH_PROBABILISTIC;
00014 import static com.googlecode.javacv.cpp.opencv_imgproc.CV_HOUGH_STANDARD;
00015 import static com.googlecode.javacv.cpp.opencv_imgproc.cvCanny;
00016 import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
00017 import static com.googlecode.javacv.cpp.opencv_imgproc.cvHoughLines2;
00018 
00019 import javax.swing.JFrame;
00020 
00021 import com.googlecode.javacpp.Pointer;
00022 import com.googlecode.javacv.CanvasFrame;
00023 import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
00024 import com.googlecode.javacv.cpp.opencv_core.CvPoint;
00025 import com.googlecode.javacv.cpp.opencv_core.CvPoint2D32f;
00026 import com.googlecode.javacv.cpp.opencv_core.CvSeq;
00027 import com.googlecode.javacv.cpp.opencv_core.IplImage;
00028 
00036 public class HoughLines {
00037 
00041     public static void main(String[] args) {
00042 
00043         String fileName = args.length >= 1 ? args[0] : "pic1.png"; // if no params provided, compute the defaut image
00044         IplImage src = cvLoadImage(fileName, 0);
00045         IplImage dst;
00046         IplImage colorDst;
00047         CvMemStorage storage = cvCreateMemStorage(0);
00048         CvSeq lines = new CvSeq();
00049 
00050         CanvasFrame source = new CanvasFrame("Source");
00051         CanvasFrame hough = new CanvasFrame("Hough");
00052         if (src == null) {
00053             System.out.println("Couldn't load source image.");
00054             return;
00055         }
00056 
00057         dst = cvCreateImage(cvGetSize(src), src.depth(), 1);
00058         colorDst = cvCreateImage(cvGetSize(src), src.depth(), 3);
00059 
00060         cvCanny(src, dst, 50, 200, 3);
00061         cvCvtColor(dst, colorDst, CV_GRAY2BGR);
00062 
00063         /*
00064          * apply the probabilistic hough transform
00065          * which returns for each line deteced two points ((x1, y1); (x2,y2))
00066          * defining the detected segment
00067          */
00068         if (args.length == 2 && args[1].contentEquals("probabilistic")) { 
00069             System.out.println("Using the Probabilistic Hough Transform");
00070             lines = cvHoughLines2(dst, storage, CV_HOUGH_PROBABILISTIC, 1, Math.PI / 180, 40, 50, 10);
00071             for (int i = 0; i <= lines.total(); i++) {
00072                 // from JavaCPP, the equivalent of the C code:
00073                 // CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
00074                 // CvPoint first=line[0], second=line[1]
00075                 // is:
00076                 // CvPoint first=line.position(0), secon=line.position(1);
00077 
00078                 Pointer line = cvGetSeqElem(lines, i);
00079                 CvPoint pt1  = new CvPoint(line).position(0);
00080                 CvPoint pt2  = new CvPoint(line).position(1);
00081 
00082                 System.out.println("Line spotted: ");
00083                 System.out.println("\t pt1: " + pt1);
00084                 System.out.println("\t pt2: " + pt2);
00085                 cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0); // draw the segment on the image
00086             }
00087         }
00088         /*
00089          * Apply the multiscale hough transform which returns for each line two float parameters (rho, theta)
00090          * rho: distance from the origin of the image to the line
00091          * theta: angle between the x-axis and the normal line of the detected line
00092          */
00093         else if(args.length==2 && args[1].contentEquals("multiscale")){
00094                         System.out.println("Using the multiscale Hough Transform"); //
00095             lines = cvHoughLines2(dst, storage, CV_HOUGH_MULTI_SCALE, 1, Math.PI / 180, 40, 1, 1);
00096             for (int i = 0; i < lines.total(); i++) {
00097                 CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
00098 
00099                 float rho=point.x();
00100                 float theta=point.y();
00101 
00102                 double a = Math.cos(theta), b = Math.sin(theta);
00103                 double x0 = a * rho, y0 = b * rho;
00104                 CvPoint pt1 = new CvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = new CvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
00105                 System.out.println("Line spoted: ");
00106                 System.out.println("\t rho= " + rho);
00107                 System.out.println("\t theta= " + theta);
00108                 cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
00109             }
00110         }
00111         /*
00112          * Default: apply the standard hough transform. Outputs: same as the multiscale output.
00113          */
00114         else {
00115             System.out.println("Using the Standard Hough Transform");
00116             lines = cvHoughLines2(dst, storage, CV_HOUGH_STANDARD, 1, Math.PI / 180, 90, 0, 0);
00117             for (int i = 0; i < lines.total(); i++) {
00118                 CvPoint2D32f point = new CvPoint2D32f(cvGetSeqElem(lines, i));
00119 
00120                 float rho=point.x();
00121                 float theta=point.y();
00122 
00123                 double a = Math.cos(theta), b = Math.sin(theta);
00124                 double x0 = a * rho, y0 = b * rho;
00125                 CvPoint pt1 = new CvPoint((int) Math.round(x0 + 1000 * (-b)), (int) Math.round(y0 + 1000 * (a))), pt2 = new CvPoint((int) Math.round(x0 - 1000 * (-b)), (int) Math.round(y0 - 1000 * (a)));
00126                 System.out.println("Line spotted: ");
00127                 System.out.println("\t rho= " + rho);
00128                 System.out.println("\t theta= " + theta);
00129                 cvLine(colorDst, pt1, pt2, CV_RGB(255, 0, 0), 3, CV_AA, 0);
00130             }
00131         }
00132         source.showImage(src);
00133         hough.showImage(colorDst);
00134 
00135         source.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00136         hough.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
00137     }
00138 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark