/** * Finds and extracts all contours in the given Mat. Optionally also removes contours with areas * below that of MIN_CONTOUR_AREA. * * @param mask A mask of all resistors in the image * @param originalImage The original image from which the mask was created * @param thresholdByArea If true, remove contours below threshold * @return The list a found contours */ private List<MatOfPoint> getContours(Mat mask, Mat originalImage, boolean thresholdByArea) { List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours( mask, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); // remove any remaining noise by only keeping contours which area > threshold if (thresholdByArea) { for (int i = 0; i < contours.size(); i++) { double area = Imgproc.contourArea(contours.get(i)); if (area < MIN_CONTOUR_AREA || area > 6000) { contours.remove(i); i--; } } } Mat drawing = Mat.zeros(originalImage.size(), CvType.CV_8U); for (int i = 0; i < contours.size(); i++) { Scalar color = new Scalar(255, 255, 255); Imgproc.drawContours(drawing, contours, i, color, 4, 8, hierarchy, 0, new Point()); } paintBR(drawing); return contours; }
public static Mat getCCH(Mat image) { ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours( image, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); Mat chainHistogram = Mat.zeros(1, 8, CvType.CV_32F); int n = 0; MatOfPoint2f approxCurve = new MatOfPoint2f(); for (MatOfPoint contour : contours) { // get the freeman chain code from the contours int rows = contour.rows(); // System.out.println("\nrows"+rows+"\n"+contour.dump()); int direction = 7; Mat prevPoint = contours.get(0).row(0); n += rows - 1; for (int i = 1; i < rows; i++) { // get the current point double x1 = contour.get(i - 1, 0)[1]; double y1 = contour.get(i - 1, 0)[0]; // get the second point double x2 = contour.get(i, 0)[1]; double y2 = contour.get(i, 0)[0]; if (x2 == x1 && y2 == y1 + 1) direction = 0; else if (x2 == x1 - 1 && y2 == y1 + 1) direction = 1; else if (x2 == x1 - 1 && y2 == y1) direction = 2; else if (x2 == x1 - 1 && y2 == y1 - 1) direction = 3; else if (x2 == x1 && y2 == y1 - 1) direction = 4; else if (x2 == x1 + 1 && y2 == y1 - 1) direction = 5; else if (x2 == x1 + 1 && y2 == y1) direction = 6; else if (x2 == x1 + 1 && y2 == y1 + 1) direction = 7; else System.out.print("err"); double counter = chainHistogram.get(0, direction)[0]; chainHistogram.put(0, direction, ++counter); System.out.print(direction); } } System.out.println("\n" + chainHistogram.dump()); Scalar alpha = new Scalar(n); // the factor Core.divide(chainHistogram, alpha, chainHistogram); System.out.println("\nrows=" + n + " " + chainHistogram.dump()); return chainHistogram; }
/** * Creates Resistor objects for all resistors extracted from given contours. Optionally, also * displays a bounding rectangle for all contours in the top left frame of the GUI. * * @param contours The contours defining the resistors * @param image The image from which the contours were extracted * @param showBoundingRect If true draws a bounding rectange for each contour * @return A list of Resistor objects */ private List<Resistor> extractResistorsFromContours( List<MatOfPoint> contours, Mat image, boolean showBoundingRect) { List<Mat> extractedResistors = new ArrayList<Mat>(); List<Rect> boundingRect = new ArrayList<Rect>(); List<Resistor> resistors = new ArrayList<Resistor>(); for (int i = 0; i < contours.size(); i++) { // bounding rectangle boundingRect.add(Imgproc.boundingRect(contours.get(i))); Mat mask = Mat.zeros(image.size(), CvType.CV_8U); Imgproc.drawContours(mask, contours, i, new Scalar(255), Core.FILLED); Mat contourRegion; Mat imageROI = new Mat(); image.copyTo(imageROI, mask); contourRegion = new Mat(imageROI, boundingRect.get(i)); extractedResistors.add(contourRegion); // the center of the resistor as a point within the original captured image Point resistorCenterPoint = findCenter(contours.get(i)); // create a new resistor entry Resistor r = new Resistor(resistorCenterPoint, contourRegion); resistors.add(r); } if (showBoundingRect) { Mat drawing = new Mat(); image.copyTo(drawing); for (int i = 0; i < contours.size(); i++) { Core.rectangle( drawing, boundingRect.get(i).tl(), boundingRect.get(i).br(), new Scalar(0, 0, 255), 2); } paintTL(drawing); } return resistors; }
/** * @param inputImg * @return Mat */ public static Mat kmeans(Mat inputImg) { Mat rgba = inputImg; Mat tempMat = inputImg; rgba = new Mat(inputImg.cols(), inputImg.rows(), CvType.CV_8UC3); inputImg.copyTo(rgba); List<Mat> hsv_planes_temp = new ArrayList<Mat>(3); Core.split(tempMat, hsv_planes_temp); double threshValue1 = PreProcessingOperation.getHistAverage(inputImg, hsv_planes_temp.get(0)); sample.util.Estimate.setFirstHistAverageValue(threshValue1); System.out.println("Defore eqau " + threshValue1); System.out.println( Estimate.getBlueAverage() + " ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"); if (threshValue1 > 140) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 31 / 100, 0); System.out.println("11"); } else { rgba.convertTo(rgba, -1, 10d * 40 / 100, 0); System.out.println("12"); } } else if (threshValue1 > 135) { rgba.convertTo(rgba, -1, 10d * 32 / 100, 0); System.out.println("21"); } else if (threshValue1 > 125) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("31"); } else { rgba.convertTo(rgba, -1, 10d * 37 / 100, 0); System.out.println("32"); } } else if (threshValue1 > 120) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); System.out.println("41"); } else if (threshValue1 > 110) { if (Estimate.getBlueAverage() > 110) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("51"); } } else if (threshValue1 > 100) { if (Estimate.getBlueAverage() > 107) { rgba.convertTo(rgba, -1, 10d * 24 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("61"); } else if (Estimate.getBlueAverage() > 90) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 5); System.out.println("62"); } } else if (threshValue1 > 50) { if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 3); System.out.println("81"); } else if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 27 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("82"); } else if (Estimate.getBlueAverage() > 130) { rgba.convertTo(rgba, -1, 10d * 30 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("83"); } else if (Estimate.getBlueAverage() > 70) { rgba.convertTo(rgba, -1, 10d * 29 / 100, 0); rgba = PreProcessing.Dilate(rgba, 9); System.out.println("84"); } } else if (threshValue1 > 30) { if (Estimate.getBlueAverage() > 190) { rgba.convertTo(rgba, -1, 10d * 25 / 100, 0); System.out.println("91"); } else if (Estimate.getBlueAverage() > 160) { rgba.convertTo(rgba, -1, 10d * 35 / 100, 0); System.out.println("92"); } } else { if (Estimate.getBlueAverage() > 240) { rgba.convertTo(rgba, -1, 10d * 24 / 100, 0); System.out.println("7"); } else { rgba.convertTo(rgba, -1, 10d * 17 / 100, 0); System.out.println("7"); } } tempMat.release(); Mat mHSV = new Mat(); Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGBA2RGB, 3); Imgproc.cvtColor(rgba, mHSV, Imgproc.COLOR_RGB2HSV, 3); List<Mat> hsv_planes = new ArrayList<Mat>(3); Core.split(mHSV, hsv_planes); Mat channel = hsv_planes.get(0); channel = Mat.zeros(mHSV.rows(), mHSV.cols(), CvType.CV_8UC1); hsv_planes.set(2, channel); Core.merge(hsv_planes, mHSV); mHSV.convertTo(mHSV, CvType.CV_8UC1); mHSV = Histogram(mHSV); /* Mat clusteredHSV = new Mat(); mHSV.convertTo(mHSV, CvType.CV_32FC3); TermCriteria criteria = new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER,100,0.1); Core.kmeans(mHSV, 1, clusteredHSV, criteria, 20, Core.KMEANS_PP_CENTERS); Mat hsvImg = new Mat(); List<Mat> hsvPlanes = new ArrayList<>(); Mat thresholdImg = new Mat(); int thresh_type = Imgproc.THRESH_BINARY_INV; hsvImg.create(mHSV.size(), CvType.CV_8U); Imgproc.cvtColor(mHSV, hsvImg, Imgproc.COLOR_BGR2HSV); Core.split(hsvImg, hsvPlanes); Imgproc.threshold(hsvPlanes.get(1), thresholdImg, 0 , 200 , thresh_type); double threshValue = PreProcessingOperation.getHistAverage(hsvImg, hsvPlanes.get(0)); Estimate.setSecondHistAverageValue(threshValue); System.out.println("After equa " + Estimate.getSecondHistAverageValue());*/ Imgproc.threshold(mHSV, mHSV, 0, 150, Imgproc.THRESH_BINARY_INV); // mHSV.convertTo(mHSV, CvType.CV_8UC1); return mHSV; }
/** * Extracts and classifies colour bands for each Resistor. Each ColourBand object is instantiated * and linked to their parent Resistor object. * * @param resistorList A list of Resistor objects from which to extract the colour bands * @param paintDebugInfo If ture, the extracted colour band ROIs are displayed on the GUI */ private void extractColourBandsAndClassify(List<Resistor> resistorList, boolean paintDebugInfo) { if (resistorList.size() > 0) { for (int r = 0; r < resistorList.size(); r++) { Mat resImg = resistorList.get(r).resistorMat; Mat imgHSV = new Mat(); Mat satImg = new Mat(); Mat hueImg = new Mat(); // convert to HSV Imgproc.cvtColor(resImg, imgHSV, Imgproc.COLOR_BGR2HSV); ArrayList<Mat> channels = new ArrayList<Mat>(); Core.split(imgHSV, channels); // extract channels satImg = channels.get(1); // saturation hueImg = channels.get(0); // hue // threshold saturation channel Mat threshedROISatBands = new Mat(); // ~130 sat thresh val Imgproc.threshold(satImg, threshedROISatBands, SAT_BAND_THRESH, 255, Imgproc.THRESH_BINARY); // threshold hue channel Mat threshedROIHueBands = new Mat(); // ~50 hue thresh val Imgproc.threshold(hueImg, threshedROIHueBands, HUE_BAND_THRESH, 255, Imgproc.THRESH_BINARY); // combine the thresholded binary images Mat bandROI = new Mat(); Core.bitwise_or(threshedROIHueBands, threshedROISatBands, bandROI); // find contours in binary ROI image ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); Imgproc.findContours( bandROI, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0)); // remove any remaining noise by only keeping contours which area > threshold for (int i = 0; i < contours.size(); i++) { double area = Imgproc.contourArea(contours.get(i)); if (area < MIN_BAND_AREA) { contours.remove(i); i--; } } // create a ColourBand object for each detected band // storing its center, the contour and the bandROI for (int i = 0; i < contours.size(); i++) { MatOfPoint contour = contours.get(i); // extract this colour band and store in a Mat Rect boundingRect = Imgproc.boundingRect(contour); Mat mask = Mat.zeros(bandROI.size(), CvType.CV_8U); Imgproc.drawContours(mask, contours, i, new Scalar(255), Core.FILLED); Mat imageROI = new Mat(); resImg.copyTo(imageROI, mask); Mat colourBandROI = new Mat(imageROI, boundingRect); // instantiate new ColourBand object ColourBand cb = new ColourBand(findCenter(contour), contour, colourBandROI); // cluster the band colour cb.clusterBandColour(BAND_COLOUR_K_MEANS); // classify using the Lab colourspace as feature vector Mat sampleMat = new Mat(1, 3, CvType.CV_32FC1); // create a Mat contacting the clustered band colour sampleMat.put(0, 0, cb.clusteredColourLAB[0]); sampleMat.put(0, 1, cb.clusteredColourLAB[1]); sampleMat.put(0, 2, cb.clusteredColourLAB[2]); Mat classifiedValue = new Mat(1, 1, CvType.CV_32FC1); Mat neighborResponses = new Mat(); // dont actually use this Mat dists = new Mat(); // dont actually use this // classify knn.find_nearest(sampleMat, 3, classifiedValue, neighborResponses, dists); // cast classified value into Colour enum and store cb.classifiedColour = ColourEnumVals[(int) classifiedValue.get(0, 0)[0]]; // add the band to the parent resistor resistorList.get(r).bands.add(cb); } // paint the extracted band ROIs if (paintDebugInfo) { Mat finalBandROIMask = Mat.zeros(bandROI.size(), CvType.CV_8U); for (int i = 0; i < contours.size(); i++) { Scalar color = new Scalar(255, 255, 255); Imgproc.drawContours( finalBandROIMask, contours, i, color, -1, 4, hierarchy, 0, new Point()); } Mat colourROI = new Mat(); resImg.copyTo(colourROI, finalBandROIMask); paintResistorSubRegion(colourROI, r); } } } }
/** * Determines which pieces are kings * * @param in Mat image of board */ public void determineKings(Mat in) { int playSquares = 32; Mat dst = new Mat(in.rows(), in.cols(), in.type()); in.copyTo(dst); Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2GRAY); // change to single color Mat canny = new Mat(); Imgproc.Canny(dst, canny, 100, 200); // make image a canny image that is only edges; 2,4 // lower threshold values find more edges List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); // holds nested contour information Imgproc.findContours( canny, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); // Imgproc.RETR_LIST, TREE // draw contour image Mat mask = new Mat(); mask = Mat.zeros(dst.size(), dst.type()); Imgproc.drawContours( mask, contours, -1, new Scalar(255, 255, 255), 1, 8, hierarchy, 2, new Point()); Highgui.imwrite("contours.jpg", mask); ArrayList occupied = new ArrayList<Integer>(); for (int i = 0; i < playSquares; i++) { if (board[i] != 0) occupied.add(i); } for (int i = 0; i < contours.size(); i++) // assuming only contours are checker pieces { // determine if it should be a king // use Rect r = Imgproc.boundingRect then find height of it by r.height // Get bounding rect of contour Rect bound = Imgproc.boundingRect(contours.get(i)); if (bound.height > in.rows() / 8) { // board[(int) occupied.get(0)]++; // make it a king // occupied.remove(0); } } // or apply to each region of interest /* // keep track of starting row square int parity = 0; // 0 is even, 1 is odd, tied to row number int count = 0; // row square int rowNum = 0; // row number, starting at 0 int vsegment = in.rows() / 8; // only accounts 8 playable int hsegment = in.cols() / 12; // 8 playable, 2 capture, 2 extra int offset = hsegment * 2; // offset for playable board // For angle of camera int dx = 48; hsegment -= 8; // Go through all playable squares for (int i = 0; i < playSquares; i++) { // change offset depending on the row if (parity == 0) // playable squares start on immediate left offset = hsegment * 3 + dx; else // playable squares start on 2nd square from left offset = hsegment * 2 + dx; // find where roi should be Point p1 = new Point(offset + count * hsegment, rowNum * vsegment); // top left point of rectangle (x,y) Point p2 = new Point(offset + (count + 1) * hsegment, (rowNum + 1) * vsegment); // bottom right point of rectangle (x,y) // create rectangle that is board square Rect bound = new Rect(p1, p2); // frame only includes rectangle Mat roi = new Mat(in, bound); Imgproc.cvtColor(roi, roi, Imgproc.COLOR_BGR2GRAY); // change to single color Mat canny = new Mat(); Imgproc.Canny(roi, canny, 2, 4); // make image a canny image that is only edges; 2,4 // lower threshold values find more edges List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Mat hierarchy = new Mat(); // holds nested contour information Imgproc.findContours(canny, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // Imgproc.RETR_LIST, TREE // Get bounding rect of contour Rect rect = Imgproc.boundingRect(contours.get(0)); if (rect.height > in.rows() / 8) { board[i]++; // make it a king } count += 2; if (count == 8) { parity = ++parity % 2; // change odd or even count = 0; rowNum++; hsegment += 1; dx -= 6; } }*/ }
public Point findLaser(Mat inputFrame) { Mat mHsv = new Mat(); Imgproc.cvtColor(inputFrame, mHsv, Imgproc.COLOR_RGB2HSV); // Find laser center Mat center = new Mat(); Core.inRange(mHsv, new Scalar(0, 0, 250), new Scalar(180, 16, 255), center); Mat h = new Mat(); List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); Imgproc.findContours(center, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); center.release(); Mat center_mask = Mat.zeros(inputFrame.rows(), inputFrame.cols(), CvType.CV_8U); if (contours.size() > 0) { for (int i = 0; i < contours.size(); i++) { int radius = 10; // Point[] cont_pos = contours.get(i).toArray(); Moments m = Imgproc.moments(contours.get(i)); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); Core.circle(center_mask, p, radius * 2, new Scalar(255), -1); } } // Find halo Mat ranged = new Mat(); Core.inRange(mHsv, new Scalar(100, 32, 225), new Scalar(150, 255, 255), ranged); mHsv.release(); // Mat f_frame =ranged.clone(); // Find halo around bright dot Core.bitwise_and(ranged, center_mask, ranged); center_mask.release(); // Find biggest resulting contour for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } contours.clear(); Imgproc.findContours(ranged, contours, h, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE); h.release(); ranged.release(); if (contours.size() > 0) { MatOfPoint biggest_cont = contours.get(0); double cont_size = Imgproc.contourArea(biggest_cont); for (int i = 1; i < contours.size(); i++) { MatOfPoint cur = contours.get(i); if (Imgproc.contourArea(cur) > cont_size) { biggest_cont = cur; cont_size = Imgproc.contourArea(cur); } } Moments m = Imgproc.moments(biggest_cont); Point p = new Point(); p.x = m.get_m10() / m.get_m00(); p.y = m.get_m01() / m.get_m00(); for (int i = 1; i < contours.size(); i++) { contours.get(i).release(); } biggest_cont.release(); return p; } else { return null; } }