bonjour tout le monde je suis bloqué sur mon projet de fin d'étude dépuis quelques jours. je voudrai reconnitre les cases noircies par les étudiants sur une copie scannée en utilisant java et opencv. J'ai deja pu detecter tous les contours fermés de mon image en dessinant des petits rectangles autours d'eux voici l'image originale et le résultats des traitements que j'ai effectués ainsi que le code que j'ai utilisé. je voudrai analyser l'image et ressortir les résultats sous cette forme par exemple:
question1: a
question2: c
question3: d
question4: b
question5: e
voici mon code;
public class image extends JFrame{
private JLabel jLabelphoto = new JLabel("Photo");
private JComboBox<String> jComboBoxPhotos;
private JButton jButtonTraiter = new JButton("Noir/Blanc");
private JPanel sliderPanel = new JPanel();
private panelImages pi = new panelImages();
private Mat srcGray = new Mat();
private static final int MAX_THRESHOLD = 255;
private int threshold = 125;
public image(){
try {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
File f = new File("C:/Users/yves/Documents/NetBeansProjects/test/src/images");
String[] image = f.list();
jComboBoxPhotos = new JComboBox(image);
String nomImage = (String)jComboBoxPhotos.getSelectedItem();
System.out.println(nomImage);
Mat bi = Imgcodecs.imread("C:/Users/yves/Documents/NetBeansProjects/test/src/images/"+nomImage);
pi.setBi(bi);
JPanel jPanelN = new JPanel();
jPanelN.add(jLabelphoto);
jPanelN.add(jComboBoxPhotos);
jPanelN.add(jButtonTraiter);
this.add(jPanelN, BorderLayout.NORTH);
this.add(pi, BorderLayout.CENTER);
this.setBounds(10, 10, 800, 600);
this.setVisible(true);
jComboBoxPhotos.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String nomImage = (String)jComboBoxPhotos.getSelectedItem();
Mat bi = Imgcodecs.imread("C:/Users/yves/Documents/NetBeansProjects/test/src/images/"+nomImage);
pi.setBi(bi);
pi.repaint();
}
});
jButtonTraiter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
Mat bi = pi.getBi();
Imgproc.cvtColor(bi, srcGray, Imgproc.COLOR_BGR2GRAY);
Imgproc.blur(srcGray, srcGray, new Size(3, 3));
update();
}
});
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void update() {
Mat cannyOutput = new Mat();
Imgproc.Canny(srcGray, cannyOutput, threshold, threshold * 2);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(cannyOutput, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
MatOfPoint2f[] contoursPoly = new MatOfPoint2f[contours.size()];
Rect[] boundRect = new Rect[contours.size()];
for (int i = 0; i < contours.size(); i++) {
contoursPoly[i] = new MatOfPoint2f();
Imgproc.approxPolyDP(new MatOfPoint2f(contours.get(i).toArray()), contoursPoly[i], 0, true);
boundRect[i] = Imgproc.boundingRect(new MatOfPoint(contoursPoly[i].toArray()));
}
Mat drawing = Mat.zeros(cannyOutput.size(), CvType.CV_8UC3);
List<MatOfPoint> contoursPolyList = new ArrayList<>(contoursPoly.length);
for (MatOfPoint2f poly : contoursPoly) {
contoursPolyList.add(new MatOfPoint(poly.toArray()));
}
for (int i = 0; i < contours.size(); i++) {
Scalar color = new Scalar(255, 255, 255);
Imgproc.drawContours(drawing, contoursPolyList, i, color);
Imgproc.rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), color, 0);
}
if(Imgcodecs.imwrite("C:/Users/yves/Documents/NetBeansProjects/test/src/images/srcGray.png", drawing)){
System.out.println("ok");
pi.repaint();
}
}
public static void main(String[] args){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
new image();
}
}
toute aide me sera utile merci d'avance
voici l'image originale et l'image traitée:
correction automatique des qcm java opencv
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.