3 changed files with 21 additions and 128 deletions
-
109src/src/main/java/com/my/graphiteDigesterBg/MyApplication.java
-
38src/src/main/java/com/my/graphiteDigesterBg/move/MoveMoveTubeRackFromHeatPlateToLiquidPlate.java
-
2src/src/main/java/com/my/graphiteDigesterBg/resource/ResHeatingTubeRackSlot.java
@ -1,118 +1,9 @@ |
|||
package com.my.graphiteDigesterBg; |
|||
import org.opencv.core.*; |
|||
import org.opencv.highgui.HighGui; |
|||
import org.opencv.imgcodecs.Imgcodecs; |
|||
import org.opencv.imgproc.Imgproc; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.SpringApplication; |
|||
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
@SpringBootApplication |
|||
public class MyApplication { |
|||
public static void main(String[] args) { |
|||
SpringApplication.run(MyApplication.class, args); |
|||
} |
|||
|
|||
private static void test() { |
|||
System.load("D:/ProgramFiles/OpenCV/opencv/build/java/x64/opencv_java490.dll"); |
|||
var srcImg = Imgcodecs.imread("D:/image.png"); |
|||
HighGui.imshow("preview", srcImg); |
|||
HighGui.waitKey(100); |
|||
|
|||
// resize to 800x800 |
|||
var size = new Size(800, 800); |
|||
var resizedImg = new Mat(800, 800, CvType.CV_8UC3); |
|||
Imgproc.resize(srcImg, resizedImg, size); |
|||
var resizedSrcImg = resizedImg.clone(); |
|||
|
|||
// slots |
|||
var leftOffset = 100; |
|||
var topOffset = 100; |
|||
var xDistance = 200; |
|||
var yDistance = 200; |
|||
int radius = 80; |
|||
int thickness = 5; |
|||
List<MatOfPoint> slotContours = new ArrayList<>(); |
|||
Scalar color = new Scalar(0, 0, 255); |
|||
for ( var i=0; i<16; i++) { |
|||
var centerX = leftOffset + (i%4)*xDistance; |
|||
var centerY = topOffset + (i/4)*yDistance; |
|||
Point center = new Point(centerX, centerY); |
|||
Imgproc.circle(resizedImg, center, radius, color, thickness); |
|||
|
|||
List<Point> pointsList = new ArrayList<>(); |
|||
for (int angle = 0; angle <= 360; angle += 10) { |
|||
double x = center.x + radius * Math.cos(Math.toRadians(angle)); |
|||
double y = center.y + radius * Math.sin(Math.toRadians(angle)); |
|||
pointsList.add(new Point(x, y)); |
|||
} |
|||
|
|||
// Convert List<Point> to MatOfPoint |
|||
MatOfPoint matOfPoint = new MatOfPoint(); |
|||
matOfPoint.fromList(pointsList); |
|||
slotContours.add(matOfPoint); |
|||
} |
|||
HighGui.imshow("preview", resizedImg); |
|||
HighGui.waitKey(100); |
|||
|
|||
var grayImg = new Mat(); |
|||
Imgproc.cvtColor(resizedSrcImg, grayImg, Imgproc.COLOR_BGR2GRAY); |
|||
HighGui.imshow("preview", grayImg); |
|||
HighGui.waitKey(100); |
|||
|
|||
var blurImg = new Mat(); |
|||
Imgproc.blur(grayImg, blurImg, new org.opencv.core.Size(30, 30)); |
|||
HighGui.imshow("preview", blurImg); |
|||
HighGui.waitKey(100); |
|||
|
|||
var wbImg = new Mat(); |
|||
Imgproc.threshold(blurImg, wbImg, 0, 255, Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU); |
|||
HighGui.imshow("preview", wbImg); |
|||
HighGui.waitKey(100); |
|||
|
|||
List<MatOfPoint> contours = new ArrayList<>(); |
|||
Imgproc.findContours(wbImg, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); |
|||
|
|||
var resultImg = new Mat(800, 800, CvType.CV_8UC3); |
|||
// 绘制轮廓 |
|||
var contourColor = new Scalar(255, 255, 255); |
|||
for ( var contour : contours ) { |
|||
Imgproc.drawContours(resultImg, List.of(contour), -1, contourColor, 3); |
|||
HighGui.imshow("preview", resultImg); |
|||
HighGui.waitKey(100); |
|||
} |
|||
|
|||
List<Integer> errorSlotIndexList = new ArrayList<>(); |
|||
var slotContourColor = new Scalar(0, 0, 255); |
|||
var slotContoursErrorColor = new Scalar(0, 255, 255); |
|||
for ( var index=0; index<16; index++ ) { |
|||
var slotContour = slotContours.get(index); |
|||
var isErrorSlot = false; |
|||
Rect slotRect = Imgproc.boundingRect(slotContour); |
|||
|
|||
for ( var contour : contours ) { |
|||
Rect contourRect = Imgproc.boundingRect(contour); |
|||
if ( slotRect.contains(contourRect.tl()) && slotRect.contains(contourRect.br()) ) { |
|||
errorSlotIndexList.add(index); |
|||
isErrorSlot = true; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
if ( isErrorSlot ) { |
|||
Imgproc.drawContours(resultImg, List.of(slotContour), -1, slotContoursErrorColor, 3); |
|||
} else { |
|||
Imgproc.drawContours(resultImg, List.of(slotContour), -1, slotContourColor, 3); |
|||
} |
|||
HighGui.imshow("preview", resultImg); |
|||
HighGui.waitKey(100); |
|||
} |
|||
// System.out.println("error slot index list: " + errorSlotIndexList); |
|||
|
|||
HighGui.imshow("preview", resultImg); |
|||
HighGui.waitKey(100); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue