Java OpenVC人员计数不起作用
由于某种原因,当我试图找到这些人时,他们没有被发现。我尝试将python从这里移植到javaJava OpenVC人员计数不起作用,java,android,opencv,Java,Android,Opencv,由于某种原因,当我试图找到这些人时,他们没有被发现。我尝试将python从这里移植到java class videoCapture { public void run() { // create new classes to meet classes requirement VideoCapture cap = new VideoCapture(getClass().getResource("/peopleCounter.avi").g
class videoCapture {
public void run() {
// create new classes to meet classes requirement
VideoCapture cap = new VideoCapture(getClass().getResource("/peopleCounter.avi").getPath());
BackgroundSubtractorMOG2 mog2 = Video.createBackgroundSubtractorMOG2();
Mat frame = new Mat();
AtomicInteger countU = new AtomicInteger(0);
AtomicInteger countD = new AtomicInteger(0);
for (int i = 0; i < 19; i++) {
System.out.println(i);
System.out.println(cap.get(i));
}
double width = cap.get(3);
double height = cap.get(3);
double frameA = width * height;
double areaTH = frameA / 250;
System.out.println("area threshold" + areaTH);
double lineUp = 2 * (height / 5);
double lineDown = 3 * (height / 5);
double up_limit = 1 * (height / 5);
double down_limit = 4 * (height / 5);
System.out.println("Red line y" + lineUp);
System.out.println("Blue line y:" + lineDown);
Point pt1 = new Point(0, lineDown);
Point pt2 = new Point(width, lineDown);
List<MatOfPoint> ptsList1 = new ArrayList();
ptsList1.add(new MatOfPoint(pt1, pt2));
Point pt3 = new Point(0, lineUp);
Point pt4 = new Point(width, lineUp);
List<MatOfPoint> ptsList2 = new ArrayList();
ptsList2.add(new MatOfPoint(pt3, pt4));
Point pt5 = new Point(0, up_limit);
Point pt6 = new Point(width, up_limit);
List<MatOfPoint> ptsList3 = new ArrayList();
ptsList3.add(new MatOfPoint(pt5, pt6));
Point pt7 = new Point(0, down_limit);
Point pt8 = new Point(width, down_limit);
List<MatOfPoint> ptsList4 = new ArrayList();
ptsList4.add(new MatOfPoint(pt7, pt8));
/*ArrayList<Point> test = new ArrayList<Point>(Arrays.asList(new Point(20,3), new Point(2,33)));
System.out.println(test);
test.add(new Point(10,33));
System.out.println(test);*/
int font = Core.FONT_HERSHEY_SIMPLEX;
List<Person> persons = new ArrayList<Person>();
int maxPAge = 5;
AtomicInteger pid = new AtomicInteger(1);
Person mary = new Person(10, 10, 10, 10);
int frameCount = 0;
while (cap.read(frame)) {
System.out.println(frameCount++);
for (int i = 0; i < persons.size(); i++) {
Person pers = persons.get(i);
pers.ageOne();
}
Mat fgMask = new Mat();
mog2.apply(frame, fgMask, 0.1);
Mat output = new Mat();
frame.copyTo(output, fgMask);
Imgproc.threshold(fgMask, output, 200, 255, Imgproc.THRESH_BINARY);
Mat kernelOp = new Mat(new Size(3, 3), CvType.CV_8UC1, new Scalar(255));
Mat kernelC1 = new Mat(new Size(11, 11), CvType.CV_8UC1, new Scalar(255));
Imgproc.morphologyEx(fgMask, output, Imgproc.MORPH_OPEN, kernelOp);
Imgproc.morphologyEx(fgMask, output, Imgproc.MORPH_CLOSE, kernelC1);
List<MatOfPoint> contours = new ArrayList<>();
//Imgproc.findContours(frame, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.findContours(fgMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours.size(); i++) {
double area = Imgproc.contourArea(contours.get(i));
if (area > areaTH) {
Moments M = Imgproc.moments(contours.get(i));
double cx = M.get_m10() / M.get_m00();
double cy = M.get_m01() / M.get_m00();
Rect rectVal = Imgproc.boundingRect(contours.get(i));
AtomicBoolean newBool = new AtomicBoolean(true);
IntStream.range((int) up_limit, (int) down_limit).forEachOrdered(n -> {
for (int h = 0; h < persons.size(); h++) {
Person pers = persons.get(h);
if (Math.abs(cx - 1) <= width && Math.abs(cy - 1) <= height) {
newBool.set(false);
pers.updateCoords((int) cx, (int) cy);
if (pers.goingUp(lineDown, lineUp) == true) {
countU.getAndIncrement();
} else if (pers.goingDown(lineDown,lineUp) == true) {
countD.getAndIncrement();
}
} if (pers.getState() == '1') {
if (pers.getDir() == "down" && pers.getY() < down_limit) {
pers.setDone();
} else if (pers.getDir() == "up" && pers.getY() < down_limit) {
pers.setDone();
}
}
}
if (newBool.get() == true) {
Person p = new Person(pid.get() ,(int) cx,(int) cy, maxPAge);
persons.add(p);
pid.getAndIncrement();
}
});
Imgproc.circle(frame, new Point((int) cx, (int) cy), 5, new Scalar (0,0,255),-1);
Imgproc.rectangle(frame, new Point(rectVal.x,rectVal.y), new Point(rectVal.x+rectVal.width, rectVal.y+rectVal.height),new Scalar (0,10,155), 2);
Imgproc.drawContours(frame, contours,-1,new Scalar (0,0,255),-1);
}
}
String strUp = "UP: " + countU;
String strDown = "Down" + countD;
Imgproc.polylines(frame, ptsList1,false,new Scalar(255,0,0),2);
Imgproc.polylines(frame, ptsList2,false,new Scalar(0,0,255),2);
Imgproc.polylines(frame, ptsList3,false,new Scalar(255,255,255),1);
Imgproc.polylines(frame, ptsList4,false,new Scalar(255,255,255),1);
Imgproc.putText(frame,strUp, new Point(10,40), font, .5,new Scalar(255,255,255),2);
Imgproc.putText(frame,strUp, new Point(10,40), font, .5,new Scalar(0,0,255),2);
Imgproc.putText(frame,strDown, new Point(10,40), font, .5,new Scalar(255,255,255),2);
Imgproc.putText(frame,strDown,new Point(10,90),font, .5, new Scalar(255,0,0), 1);
DisplayImage.displayImage(DisplayImage.Mat2BufferedImage(frame));
}
cap.release();
}
}
public class ScanTest {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new videoCapture().run();
}
}
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import java.util.ArrayList;
import java.util.List;
public class Person {
private int i;
private int x;
private int y;
private ArrayList<Point> tracks;
private Scalar rgb;
private boolean done;
private char state;
private int age;
private int maxAge;
private String dir;
public Person(int i, int xi, int yi, int maxAge) {
this.i = i;
this.x = xi;
this.y = yi;
this.tracks = new ArrayList<Point>();
this.rgb = new Scalar((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
this.done = false;
this.state = '0';
this.age = 0;
this.maxAge = maxAge;
this.dir = "Nil";
}
public Scalar getRGB() {
return rgb;
}
public ArrayList<Point> getTracks(){
return tracks;
}
public double getId() {
return i;
}
public char getState() {
return state;
}
public String getDir() {
return dir;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void updateCoords(int xn, int yn) {
this.age = 0;
this.tracks.add(new Point(x,y));
this.x = xn;
this.y = yn;
}
public void setDone() {
this.done = true;
}
public boolean goingUp(double midStart, double midEnd) {
if (tracks.size() >= 2) {
if (state == 0) {
if (tracks.get(-1).x < midEnd && tracks.get(0).x >= midEnd) {
this.state = '1';
this.dir = "up";
return true;
} else {
return false;
}
} else {
return false;
}
}return false;
}
public boolean goingDown(double midStart, double midEnd) {
if (tracks.size() >= 2) {
if (state == 0) {
if (tracks.get(-1).x > midStart && tracks.get(0).x <= midStart) {
this.state = '1';
this.dir = "down";
return true;
} else {
return false;
}
} else {
return false;
}
}return false;
}
public boolean ageOne(){
this.age++;
if (this.age > this.maxAge){
this.done = true;
return true;
}
return false;
}
}
类视频捕获{
公开募捐{
//创建新类以满足类要求
VideoCapture cap=新的视频捕获(getClass().getResource(“/peopleCounter.avi”).getPath();
BackgroundSubtractorMOG2=Video.createBackgroundSubtractorMOG2();
垫架=新垫();
AtomicInteger countU=新的AtomicInteger(0);
AtomicInteger countD=新的AtomicInteger(0);
对于(int i=0;i<19;i++){
系统输出打印LN(i);
系统输出打印LN(第get(i)章);
}
双倍宽度=cap.get(3);
双倍高度=盖特(3);
双框A=宽度*高度;
双面积=帧A/250;
System.out.println(“面积阈值”+areaTH);
双排列=2*(高度/5);
双线向下=3*(高度/5);
双上限=1*(高度/5);
双下极限=4*(高度/5);
系统输出打印项次(“红线y”+排列);
System.out.println(“蓝线y:+lineDown”);
点pt1=新点(0,下线);
点pt2=新点(宽度,下线);
List ptsList1=new ArrayList();
ptsList1.add(新的MatOfPoint(pt1,pt2));
点pt3=新点(0,排列);
点pt4=新点(宽度、排列);
List ptsList2=new ArrayList();
ptsList2.添加(新的MatOfPoint(pt3,pt4));
点pt5=新点(0,上限);
点pt6=新点(宽度,上限);
List ptsList3=new ArrayList();
ptsList3.添加(新的MatOfPoint(pt5,pt6));
点pt7=新点(0,下限);
点pt8=新点(宽度、下限);
List ptsList4=new ArrayList();
ptsList4.添加(新的MatOfPoint(pt7,pt8));
/*ArrayList测试=新的ArrayList(Arrays.asList(新点(20,3),新点(2,33));
系统输出打印LN(测试);
增加(新的点(10,33));
系统输出打印LN(测试)*/
int font=Core.font\u HERSHEY\u SIMPLEX;
List persons=new ArrayList();
int maxPAge=5;
AtomicInteger pid=新的AtomicInteger(1);
人物玛丽=新人(10,10,10,10);
int frameCount=0;
while(第二章(框架)){
System.out.println(frameCount++);
对于(int i=0;i面积){
矩M=Imgproc.矩(等高线.get(i));
双cx=M.get_m10()/M.get_m00();
double cy=M.get_m01()/M.get_m00();
Rect rectVal=Imgproc.boundingRect(contours.get(i));
AtomicBoolean newBool=新的AtomicBoolean(真);
IntStream.range((int)上限,(int)下限)。forEachOrdered(n->{
对于(inth=0;h=2){
如果(状态==0){
if(tracks.get(-1).x>midStart&&tracks.get(0.x this.maxAge){
this.done=true;
返回true;
}
返回false;
}
}
您好,由于某种原因,在我尝试查找人物时,没有检测到他们。我尝试将python从这里移植到java。我相信这与检测轮廓有关。有时人物周围有一个矩形,但很简单。您的问题到底是什么?也许是
areaTH
的问题,您可以尝试以下方法:areaTH=frameA/1000;