在我的JLabel中,我可以绘制一些矩形,然后在该JLabel中按下按钮后,将启动视频的对象标识符,并绘制边界框。
问题在于矩形是使用Awt绘制的,而对象标识符使用opencv。对于这些矩形,我必须检查边界框是否在其中,但是在opencv上AWT的坐标不相同,因此我需要缩放它们。
尝试使用比例尺,或仅使用awt,但我没有成功。
这是我绘制矩形标签的功能。
public void drawToBackground() {
if (backgroundImg != null) {
Graphics g = backgroundImg.getGraphics();
g.setColor(FINAL_DRAWING_COLOR);
int x = Math.min(startPt.x, endPt.x);
int y = Math.min(startPt.y, endPt.y);
int width = Math.abs(startPt.x - endPt.x);
int height = Math.abs(startPt.y - endPt.y);
g.drawRect(x, y, width, height);
g.dispose();
places.add(new Place(x,y,width,height,("place"+places.size())));
System.out.println(x);
System.out.println(y);
System.out.println(width);
System.out.println(height);
startPt = null;
repaint();
}
}
对象标识符的功能。
while (true) {
if (cap.read(frame)) {
Mat blob = Dnn.blobFromImage(frame, 0.00392, sz, new Scalar(0), true, false); // We feed
// one
// frame
// of
// video
// into
// the
// network
// at a
// time,
// we
// have
// to
// convert
// the
// image
// to a
// blob.
// A
// blob
// is a
// pre-processed
// image
// that
// serves
// as
// the
// input.//
net.setInput(blob);
net.forward(result, outBlobNames); // Feed forward the model to get output //
// outBlobNames.forEach(System.out::println);
// result.forEach(System.out::println);
float confThreshold = 0.6 f; // Insert thresholding beyond which the model will detect
// objects//
List < Integer > clsIds = new ArrayList < > ();
List < Float > confs = new ArrayList < > ();
List < Rect > rects = new ArrayList < > ();
for (int i = 0; i < result.size(); ++i) {
// each row is a candidate detection, the 1st 4 numbers are
// [center_x, center_y, width, height], followed by (N-4) class probabilities
Mat level = result.get(i);
for (int j = 0; j < level.rows(); ++j) {
Mat row = level.row(j);
Mat scores = row.colRange(5, level.cols());
Core.MinMaxLocResult mm = Core.minMaxLoc(scores);
float confidence = (float) mm.maxVal;
Point classIdPoint = mm.maxLoc;
if (confidence > confThreshold) {
int centerX = (int)(row.get(0, 0)[0] * frame.cols()); // scaling for
// drawing the
// bounding
// boxes//
int centerY = (int)(row.get(0, 1)[0] * frame.rows());
int width = (int)(row.get(0, 2)[0] * frame.cols());
int height = (int)(row.get(0, 3)[0] * frame.rows());
int left = centerX - width / 2;
int top = centerY - height / 2;
clsIds.add((int) classIdPoint.x);
confs.add((float) confidence);
rects.add(new Rect(left, top, width, height));
}
}
}
float nmsThresh = 0.5 f;
MatOfFloat confidences = new MatOfFloat(Converters.vector_float_to_Mat(confs));
Rect[] boxesArray = rects.toArray(new Rect[0]);
MatOfRect boxes = new MatOfRect(boxesArray);
MatOfInt indices = new MatOfInt();
Dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThresh, indices); // We draw the
// bounding
// boxes for
// objects
// here//
int[] ind = indices.toArray();
int j = 0;
for (int i = 0; i < ind.length; ++i) {
int idx = ind[i];
Rect box = boxesArray[idx];
Imgproc.rectangle(frame, box.tl(), box.br(), new Scalar(0, 0, 255), 2);
Imgproc.rectangle(frame, new Point(0, 0), new Point(300, 300),
new Scalar(255, 0, 0), 2);
Imgproc.rectangle(frame, new Point(20, 20), new Point(40, 40),
new Scalar(255, 0, 0), 2);
int left = (int) box.tl().y;
int top = (int) box.tl().x;
int bottom = (int) box.br().x;
int right = (int) box.br().y;
ArrayList < String > places = check_places(vidlabel.places, 30, top, bottom, left,
right);
if (places.get(0) == "out") { //CHECK FOR MISSED
//Imgproc.rectangle(frame, box.tl(), box.br(),new Scalar(255, 0, 0), 2);
System.out.println(vidlabel.places.size());
for (Place place: vidlabel.places) {
Imgproc.rectangle(frame, new Point(50, 70), new Point(150, 160), new Scalar(255, 0, 0), 2); // 62 55 1167 590
}
System.out.println(left);
System.out.println(top);
System.out.println(right);
System.out.println(bottom);
}
String joined = String.join(" ", places);
String value = ((map_classes.get(String.valueOf((int) clsIds.get(i)))) + " " +
String.valueOf(left) + " " + String.valueOf(top) + " " +
String.valueOf(right) + " " + String.valueOf(bottom) +
String.valueOf(num_frame) + " " + joined);
writer.println(value);
// System.out.println(value);
pos_array.add(value);
// i=j;
// System.out.println(idx);
}
// Imgcodecs.imwrite("D://out.png", image);
// System.out.println("Image Loaded");
ImageIcon image = new ImageIcon(Mat2bufferedImage(frame)); // setting the results into a
// frame and
// initializing it //
vidlabel.setIcon(image);
vidlabel.repaint();
// System.out.println(j);
// System.out.println("Done");
num_frame++;
} else {
btnNewButton.setEnabled(true);
}
}