Tôi đang phát triển một dự án nhận dạng hình dạng bằng cách sử dụng JavaCV và tôi đã tìm thấy một số mã OpenCV để xác định các hình dạng U trong một hình ảnh cụ thể. Tôi đã cố gắng chuyển đổi nó thành JavaCV nhưng nó không cung cấp cho cùng một đầu ra. Bạn có thể vui lòng giúp tôi chuyển đổi mã OpenCV này thành JavaCV không?opencv/javacv: Làm cách nào để lặp qua các đường nét để nhận dạng hình dạng?
Đây là mã OpenCV:
import cv2
import numpy as np
img = cv2.imread('sofud.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if 10 < w/float(h) or w/float(h) < 0.1:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Đây là kết quả mong muốn
Đây là mã chuyển đổi:
import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import java.io.File;
import javax.swing.JFileChooser;
public class TestBeam {
public static void main(String[] args) {
CvMemStorage storage=CvMemStorage.create();
CvSeq squares = new CvContour();
squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
JFileChooser f=new JFileChooser();
int result=f.showOpenDialog(f);//show dialog box to choose files
File myfile=null;
String path="";
if(result==0){
myfile=f.getSelectedFile();//selected file taken to myfile
path=myfile.getAbsolutePath();//get the path of the file
}
IplImage src = cvLoadImage(path);//hear path is actual path to image
IplImage grayImage = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
cvCvtColor(src, grayImage, CV_RGB2GRAY);
cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
CvSeq cvSeq=new CvSeq();
CvMemStorage memory=CvMemStorage.create();
cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
System.out.println(cvSeq.total());
for (int i = 0; i < cvSeq.total(); i++) {
CvRect rect=cvBoundingRect(cvSeq, i);
int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width();
if (10 < (w/h) || (w/h) < 0.1){
cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0);
//cvSeqPush(squares, rect);
}
}
CanvasFrame cnvs=new CanvasFrame("Beam");
cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
cnvs.showImage(src);
//cvShowImage("Final ", src);
}
}
Đây là sản phẩm mà tôi có. Xin ai đó có thể giúp tôi giải quyết vấn đề này?
Tôi không thấy bất kỳ C++ nào, vì vậy tôi đã xóa thẻ. Tôi giả định ví dụ đầu tiên là Python. –
Tôi có câu hỏi nhỏ liên quan đến câu hỏi này. Xin vui lòng một số có thể giải thích giá trị của "cvSeq.total()" phương pháp sau khi thực hiện "cvFindContours()" phương pháp? –