Merge pull request 'Fix rectangle colors, attempt to draw rectangle on section' (#1) from karx/freesight:master into master

Reviewed-on: #1

better locks
This commit is contained in:
gallant 2023-09-29 12:57:21 -05:00
commit d84b1ececa

View file

@ -9,6 +9,7 @@ import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfRect; import org.opencv.core.MatOfRect;
import org.opencv.core.Rect; import org.opencv.core.Rect;
import org.opencv.core.Point;
import org.opencv.core.Scalar; import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
import org.openftc.easyopencv.OpenCvPipeline; import org.openftc.easyopencv.OpenCvPipeline;
@ -32,7 +33,7 @@ public class FreeSightPipeline extends OpenCvPipeline {
public Scalar lowHSV = new Scalar(0,0,0); public Scalar lowHSV = new Scalar(0,0,0);
public Scalar highHSV = new Scalar(0,0,0); public Scalar highHSV = new Scalar(0,0,0);
public Scalar outline = new Scalar(0,0,0); public Scalar outline = new Scalar(0,255,0);
public Prop colorState = Prop.NONE; public Prop colorState = Prop.NONE;
/** /**
@ -97,39 +98,54 @@ public class FreeSightPipeline extends OpenCvPipeline {
Imgproc.findContours(scaledThresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE); Imgproc.findContours(scaledThresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_NONE);
Mat threshRGB = new Mat();
int index = 0; Imgproc.cvtColor(threshold, threshRGB, Imgproc.COLOR_GRAY2BGR);
int area = 0; if (contours.size() > 0) {
for (int i = 0; i < contours.size(); i++) int index = 0;
{ int area = 0;
MatOfPoint bar = contours.get(i); for (int i = 0; i < contours.size(); i++)
int foo = bar.width() * bar.height();
if(foo > area)
{ {
index = i; MatOfPoint bar = contours.get(i);
area = foo; int foo = bar.width() * bar.height();
if(foo > area)
{
index = i;
area = foo;
}
} }
}
MatOfPoint contour = contours.get(index); MatOfPoint contour = contours.get(index);
Rect boundingRect = Imgproc.boundingRect(contour); Rect boundingRect = Imgproc.boundingRect(contour);
// center is ( x + w ) / 2 // center is ( x + w ) / 2
int point = (boundingRect.x + boundingRect.width) / 2; // int point = (boundingRect.x + boundingRect.width) / 2;
int point = boundingRect.x + boundingRect.width / 2;
Imgproc.rectangle( Imgproc.rectangle(
threshold, threshRGB,
boundingRect, boundingRect,
outline outline
); );
if(point < width / 3) int bigX;
positionState = Side.LEFT; if(point < width / 3) {
else if(point > width / 1.5) positionState = Side.LEFT;
positionState = Side.RIGHT; bigX = 0;
else }
positionState = Side.MIDDLE; else if(point > width / 1.5) {
positionState = Side.RIGHT;
bigX = width * 2 / 3;
}
else {
positionState = Side.MIDDLE;
bigX = width / 3;
}
Imgproc.rectangle(threshRGB, new Rect(bigX, 0, width / 3, height), outline);
Imgproc.circle(threshRGB, new Point(boundingRect.x + boundingRect.width / 2, boundingRect.y + boundingRect.height / 2), 10, outline);
Imgproc.putText(threshRGB, positionState.toString(), new Point(boundingRect.x + boundingRect.width / 2, boundingRect.y + boundingRect.height / 2), Imgproc.FONT_ITALIC, 0.5, new Scalar(255, 0, 255));
}
//list of frames to reduce inconsistency, not too many so that it is still real-time, change the number from 5 if you want //list of frames to reduce inconsistency, not too many so that it is still real-time, change the number from 5 if you want
if (frameList.size() > 5) { if (frameList.size() > 5) {
@ -142,7 +158,8 @@ public class FreeSightPipeline extends OpenCvPipeline {
scaledMask.release(); scaledMask.release();
mat.release(); mat.release();
masked.release(); masked.release();
return threshold; threshold.release();
return threshRGB;
} }
} }