Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package imageProcessing;
- import java.util.ArrayList;
- import org.opencv.core.*;
- import org.opencv.imgproc.Imgproc;
- import main.RunProgramm;
- public class ProcessInputImage {
- public boolean checkForCircle(Mat srcImg, Mat grayImg){
- Mat circles = new Mat();
- Imgproc.HoughCircles(grayImg, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 400);
- drawCircles(srcImg, circles);
- if (circles.cols() > 0){
- return true;
- }
- return false;
- }
- public boolean checkForTriangle(Mat srcImg, Mat invImg){
- Mat hierarchy = new Mat();
- ArrayList<MatOfPoint> contourList = new ArrayList<MatOfPoint>();
- Imgproc.findContours(invImg, contourList, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
- System.out.println("# of contours in contourList: "+contourList.size());
- MatOfPoint2f contourSimple = new MatOfPoint2f();
- MatOfPoint2f srcContour = new MatOfPoint2f();
- contourList.get(0).convertTo(srcContour, CvType.CV_32FC2);
- double contourPerimeter = Imgproc.arcLength(srcContour, true);
- Imgproc.approxPolyDP(srcContour, contourSimple, 0.05 * contourPerimeter, true);
- //Convert simplified contour back to MatOfPoint and put it back into the list
- contourSimple.convertTo(contourList.get(0), CvType.CV_32S);
- System.out.println("Corners: " + contourSimple.rows());
- if (contourSimple.rows() == 3){
- return true;
- }else{
- //debug: Draw simplified contour onto srcImg
- Imgproc.drawContours(srcImg, contourList, 0, new Scalar(0,0,255),1);
- ArrayList<Point> pointList = new ArrayList<Point>(),
- pointListFinal = new ArrayList<Point>();
- //Iterate over each vertex in simplified contour
- for(int i = 0; i < contourSimple.rows(); i++) {
- double[] coords = contourSimple.get(i, 0);
- Point p = new Point(coords[0],coords[1]);
- //And add them to a list of vertices
- pointList.add(p);
- //debug: Draw vertices onto srcImg
- Imgproc.circle(srcImg,p, 3, new Scalar(i*40,0,255-i*40),2);
- }
- boolean[] blacklist = new boolean[pointList.size()];
- //Compare all points with each other
- for (int i = 0; i < pointList.size(); i++) {
- for (int k = i + 1; k < pointList.size(); k++) {
- if (i != k) {
- Point p1 = pointList.get(i),
- p2 = pointList.get(k);
- //Calculate distance between point i <-> point k
- double dist = Math.sqrt(Math.pow((p2.x - p1.x), 2) + Math.pow((p2.y - p1.y), 2));
- //If distance is smaller than a given threshold value
- if(dist < 20) {
- //then blacklist point i. Every blacklisted point won't be copied in pointListFinal afterwards.
- blacklist[i] = true;
- }
- }
- }
- }
- //Construct final point list. These points don't have "duplicates" and might form a triangle.
- for(int i = 0; i < pointList.size(); i++) {
- boolean isBlacklisted = blacklist[i];
- //debug: print blacklist items in console
- System.out.println(isBlacklisted);
- if(!isBlacklisted)
- pointListFinal.add(pointList.get(i));
- }
- //debug: print final point list size
- System.out.println(pointListFinal.size());
- }
- return false;
- }
- public boolean checkForLine(Mat srcImg, Mat invImg){
- Mat a = new Mat();
- ArrayList<MatOfPoint> contourList = new ArrayList<MatOfPoint>();
- Imgproc.findContours(invImg, contourList, a, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
- if (contourList.size() == 1 ){
- Rect r = Imgproc.boundingRect(contourList.get(0));
- if (r.width >= 30 && r.height <= 60){
- return true;
- }
- }
- return false;
- }
- public void drawCircles (Mat img, Mat circles){
- for (int i = 0; i < circles.cols(); i++){
- double[] circleVector = circles.get(0, i);
- System.out.println(circleVector.length);
- Point center = new Point (circleVector[0], circleVector[1]);
- int radius = (int) Math.round(circleVector[2]);
- Imgproc.circle(img, center, radius, new Scalar(255,0,0), 10 );
- }
- }
- public void drawLines (Mat img, Mat lines){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement