elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  javaFX con opencv
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: javaFX con opencv  (Leído 1,841 veces)
HunterLuis

Desconectado Desconectado

Mensajes: 10


Ver Perfil
javaFX con opencv
« en: 26 Mayo 2015, 03:24 am »

Cuando ejecutan el programa Histogramas y modifican el tinte a izquierda o derecha cambia el color de la imagen como es de esperarse, pero, la imagen indexada en dos bits se ve alterada también, es decir, se ven más blancos o negros dependiendo del escalar que se sume a la matriz de tinta.


Se supone que la tinta no debería modificar los brillos o saturaciones, por lo tanto, ¿ porque sucede eso ?

¿ Que pasa si solo mezclamos dos canales brillo y saturación ?, ¿ es posible ?, si lo es enviar imágenes, si no lo es, ¿ como puedo hacer para obtener imágenes como si el canal H no existiera ?

Aca les dejo el codigo es Una aplicacion en javafx fxml:

Este es el FXMLHistograma (Vista):
------------------------------------------------------------------------------------------------
Código
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <?import javafx.geometry.*?>
  4. <?import javafx.scene.image.*?>
  5. <?import java.lang.*?>
  6. <?import java.util.*?>
  7. <?import javafx.scene.*?>
  8. <?import javafx.scene.control.*?>
  9. <?import javafx.scene.layout.*?>
  10.  
  11. <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="768.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="histograma.FXMLHistogramaController">
  12.   <center>
  13.      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER">
  14.         <children>
  15.            <VBox prefHeight="200.0" prefWidth="100.0">
  16.               <children>
  17.                  <Label text="Original" />
  18.                  <ImageView fx:id="aquired" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  19.                  <Separator prefWidth="200.0" />
  20.                  <ImageView fx:id="modified" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  21.                  <Separator prefWidth="200.0" />
  22.                  <ImageView fx:id="indexed" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  23.               </children>
  24.            </VBox>
  25.            <VBox prefHeight="200.0" prefWidth="100.0">
  26.               <children>
  27.                  <Label text="HSV" />
  28.                  <ImageView fx:id="h_channel" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  29.                  <Slider fx:id="slider_h" blockIncrement="5.0" majorTickUnit="50.0" max="127.0" min="-127.0" minorTickCount="1" onValueChange="#doRedraw" showTickLabels="true" showTickMarks="true" />
  30.                  <ImageView fx:id="s_channel" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  31.                  <Slider fx:id="slider_s" blockIncrement="5.0" majorTickUnit="50.0" min="-100.0" minorTickCount="1" onValueChange="#doRedraw" showTickLabels="true" showTickMarks="true" />
  32.                  <ImageView fx:id="v_channel" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  33.                  <Slider fx:id="slider_v" blockIncrement="5.0" majorTickUnit="50.0" min="-100.0" minorTickCount="1" onValueChange="#doRedraw" showTickLabels="true" showTickMarks="true" />
  34.               </children>
  35.            </VBox>
  36.            <VBox prefHeight="200.0" prefWidth="100.0">
  37.               <children>
  38.                  <Label text="HISTOGRAMAS" />
  39.                  <ImageView fx:id="histogram_h" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  40.                  <Separator prefWidth="200.0" />
  41.                  <ImageView fx:id="histogram_s" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  42.                  <Separator prefWidth="200.0" />
  43.                  <ImageView fx:id="histogram_v" fitHeight="200.0" fitWidth="320.0" pickOnBounds="true" preserveRatio="true" />
  44.               </children>
  45.            </VBox>
  46.         </children>
  47.      </HBox>
  48.   </center>
  49.   <bottom>
  50.      <HBox prefHeight="100.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
  51.   </bottom>
  52. </BorderPane>
-----------------------------------------------------------------------------------------------

(Controlador) FXMLHistogramaController:
-----------------------------------------------------------------------------------------------
Código
  1. package histograma;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.ResourceBundle;
  8. import javafx.fxml.FXML;
  9. import javafx.fxml.Initializable;
  10. import javafx.scene.control.Slider;
  11. import javafx.scene.image.Image;
  12. import javafx.scene.image.ImageView;
  13. import org.opencv.core.Core;
  14. import org.opencv.core.CvType;
  15. import org.opencv.core.Mat;
  16. import org.opencv.core.MatOfByte;
  17. import org.opencv.core.MatOfFloat;
  18. import org.opencv.core.MatOfInt;
  19. import org.opencv.core.Point;
  20. import org.opencv.core.Scalar;
  21. import org.opencv.highgui.Highgui;
  22. import org.opencv.imgproc.Imgproc;
  23.  
  24. /**
  25.  * FXML Controller class
  26.  *
  27.  * @author eduardodisanti
  28.  */
  29. public class FXMLHistogramaController implements Initializable {
  30.  
  31.    public Scalar ROJO = new Scalar(0,0,255);
  32.    public Scalar VERDE = new Scalar(0,255,0);
  33.    public Scalar AZUL = new Scalar(255,0,0);
  34.    public Scalar BLANCO = new Scalar(255,255,255);
  35.    public Scalar NEGRO = new Scalar(0,0,0);
  36.    @FXML
  37.    private Slider slider_h;
  38.    @FXML
  39.    private Slider slider_s;
  40.    @FXML
  41.    private Slider slider_v;
  42.  
  43.    /**
  44.      * Initializes the controller class.
  45.      * @param url
  46.      * @param rb
  47.      */
  48.  
  49.    @FXML
  50.    private ImageView aquired;
  51.    @FXML
  52.    private ImageView modified;
  53.    @FXML
  54.    private ImageView indexed;
  55.    @FXML
  56.    private ImageView h_channel;
  57.    @FXML
  58.    private ImageView s_channel;
  59.    @FXML
  60.    private ImageView v_channel;
  61.    @FXML
  62.    private ImageView histogram_h;
  63.    @FXML
  64.    private ImageView histogram_s;
  65.    @FXML
  66.    private ImageView histogram_v;
  67.  
  68.  
  69.    @Override
  70.    public void initialize(URL url, ResourceBundle rb) {
  71.  
  72.        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  73.  
  74.        redraw();
  75.    }
  76.  
  77.    private Image convertirDeMat(Mat m, String type) {
  78.  
  79.        MatOfByte byteMat = new MatOfByte();
  80.  
  81.        Highgui.imencode(type, m, byteMat);
  82.        Image img = new Image(new ByteArrayInputStream(byteMat.toArray()));
  83.  
  84.        return(img);
  85.    }
  86.  
  87.    @FXML
  88.    private void doRedraw() {
  89.  
  90.        redraw();
  91.    }
  92.  
  93.    private void redraw() {
  94.  
  95.        int rango = 256;
  96.  
  97.        Mat aquired_mat;
  98.        Mat modified_mat;
  99.        Mat indexed_mat;
  100.        Mat hsv_mat = new Mat();
  101.        Mat h_channel_mat;
  102.        Mat s_channel_mat;
  103.        Mat v_channel_mat;
  104.        Mat histogram_h_mat;
  105.        Mat histogram_s_mat;
  106.        Mat histogram_v_mat;
  107.  
  108.        aquired_mat = Highgui.imread("C:\\opencv\\opencv-2.4.8\\canarias-spill-aquired.png");
  109.        if(!aquired_mat.empty()) {
  110.          aquired.setImage(convertirDeMat(aquired_mat, ".png"));
  111.          Imgproc.cvtColor(aquired_mat, hsv_mat, Imgproc.COLOR_BGR2HSV);
  112.  
  113.  
  114.          List<Mat> hsv_channel = new ArrayList<>();
  115.            Core.split(hsv_mat, hsv_channel);
  116.            Mat ch_h = hsv_channel.get(0).clone();
  117.            Mat ch_s = hsv_channel.get(1).clone();
  118.            Mat ch_v = hsv_channel.get(2).clone();
  119.  
  120.            h_channel.setImage(convertirDeMat(ch_h, ".png"));
  121.            s_channel.setImage(convertirDeMat(ch_s, ".png"));
  122.            v_channel.setImage(convertirDeMat(ch_v, ".png"));
  123.  
  124.            ch_h = addScalarToMat(ch_h, slider_h.getValue());
  125.            ch_s = addScalarToMat(ch_s, slider_s.getValue());
  126.            ch_v = addScalarToMat(ch_v, slider_v.getValue());
  127.  
  128.            List<Mat> hue = new ArrayList<>();
  129.            hue.add(ch_h);
  130.            histogram_h_mat = compute_histogram(hue, 256, rango, false);                                    
  131.            draw_histogram(histogram_h, histogram_h_mat, rango, VERDE);
  132.  
  133.            List<Mat> sat = new ArrayList<>();
  134.            sat.add(ch_s);
  135.            histogram_s_mat = compute_histogram(sat, 256, rango, false);                        
  136.            draw_histogram(histogram_s, histogram_s_mat, rango, AZUL);
  137.  
  138.            List<Mat> par = new ArrayList<>();
  139.            par.add(ch_v);
  140.            histogram_v_mat = compute_histogram(par,256,rango,false);
  141.            draw_histogram(histogram_v, histogram_v_mat,rango,BLANCO);
  142.  
  143.            modified_mat = new Mat(aquired_mat.rows(), aquired_mat.cols(), aquired_mat.type());
  144.  
  145.            hsv_channel.set(0, ch_h);
  146.            hsv_channel.set(1, ch_s);
  147.            hsv_channel.set(2, ch_v);
  148.  
  149.            Core.merge(hsv_channel, modified_mat);
  150.            Imgproc.cvtColor( ch_s,  modified_mat, Imgproc.COLOR_HSV2BGR);
  151.            modified.setImage(convertirDeMat(modified_mat, ".png"));
  152.  
  153.            Mat gray_mat = new Mat();
  154.            Imgproc.cvtColor(modified_mat, gray_mat, Imgproc.COLOR_RGB2GRAY);
  155.            indexed_mat = new Mat();
  156.            Imgproc.threshold(gray_mat, indexed_mat, 127, 255, Imgproc.THRESH_BINARY);
  157.            indexed.setImage(convertirDeMat(indexed_mat, ".png"));
  158.        }  
  159.  
  160.    }
  161.    private void draw_histogram(ImageView imview, Mat histogram_mat, int rango, Scalar color) {
  162.  
  163.        double maxval = compute_max_histogram_value(histogram_mat);
  164.        double ancho = (float)imview.getFitWidth();
  165.        double ancho_barra = (float)imview.getFitWidth() / (float)rango;
  166.        double alto  = imview.getFitHeight();
  167.  
  168.        Mat histogram = new Mat((int)alto, (int)ancho, CvType.CV_32SC3, new Scalar(0,0,0));
  169.  
  170.  
  171.        float x = 0;
  172.        for(int i=0;i<rango;i++) {
  173.            double[] y = histogram_mat.get(i, 0);
  174.  
  175.            double yy = alto - (alto * y[0] / maxval);
  176.            Core.rectangle(histogram, new Point(x, alto), new Point(x + ancho_barra, yy), color, (int)ancho_barra);
  177.            x+=ancho_barra;
  178.        }        
  179.        imview.setImage(convertirDeMat(histogram, ".png"));
  180.    }
  181.   private double compute_max_histogram_value(Mat mat) {
  182.  
  183.        double maxval = Double.MIN_VALUE;
  184.  
  185.        for(int i=0;i<mat.rows();i++) {
  186.                if(mat.get(i, 0)[0]>maxval) {
  187.                    maxval = mat.get(i, 0)[0];
  188.                }
  189.        }
  190.  
  191.        return(maxval);
  192.   }
  193.    private Mat compute_histogram(List<Mat> mat_list, int tamanio, int rango, boolean accumulate) {
  194.  
  195.        MatOfInt histSize = new MatOfInt(tamanio);
  196.        final MatOfFloat histRange = new MatOfFloat(0f, rango);
  197.  
  198.        Mat histograma = new Mat();
  199.        Imgproc.calcHist(mat_list, new MatOfInt(0),new Mat(), histograma, histSize, histRange, accumulate);
  200.  
  201.        return histograma;
  202.    }
  203.    private Mat addScalarToMat(Mat mat, double value) {
  204.  
  205.        Mat ret = mat.clone();
  206.  
  207.        for(int row = 0; row < mat.rows(); row++) {
  208.            for(int col = 0; col < mat.cols(); col++) {
  209.                double aval[] = mat.get(row, col);
  210.                double newval = value + aval[0];
  211.                if(newval>255) {
  212.                    newval = 255;
  213.                } else {
  214.                    if(newval<0) {
  215.                       newval = 0;
  216.                  }
  217.                }
  218.                aval[0] = newval;
  219.                ret.put(row, col, aval);
  220.           }  
  221.        }
  222.        return(ret);
  223.    }
  224. }
  225. ------------------------------------------------------------------------------------------------
  226. Y (el Modelo) Histograma:
  227. ------------------------------------------------------------------------------------------------
  228. public class Histograma extends Application {
  229.  
  230.    @Override
  231.    public void start(Stage stage) throws Exception {
  232.        Parent root = FXMLLoader.load(getClass().getResource("FXMLHistograma.fxml"));
  233.  
  234.        Scene scene = new Scene(root);
  235.        stage.setTitle("Escoger Histograma");
  236.        stage.setScene(scene);
  237.        stage.show();
  238.    }
  239.  
  240.    /**
  241.      * @param args the command line arguments
  242.      */
  243.    public static void main(String[] args) {
  244.        launch(args);
  245.    }
  246.  
  247. }
------------------------------------------------------------------------------------------------
Bueno chicos este es el codigo si hay alguna solocion para mis dudas porfavor no duden en responder gracias.

Mod: Los códigos deben ir en etiquetas GeSHi


« Última modificación: 3 Junio 2015, 13:13 pm por engel lex » En línea

HunterLuis

Desconectado Desconectado

Mensajes: 10


Ver Perfil
Re: javaFX con opencv
« Respuesta #1 en: 26 Mayo 2015, 03:34 am »

Me olvidaba pueden trabajar con cualquier imagen yo como se daran cuenta estoy trabajando con otra imagen.


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
OpenCv
Programación C/C++
hojanico19 0 2,497 Último mensaje 17 Julio 2012, 19:57 pm
por hojanico19
[JavaFX] Manual para aprender JavaFX
Java
jaxoR 1 10,566 Último mensaje 17 Noviembre 2013, 20:16 pm
por Mitsu
[JavaEE - JavaFx] RESTful con JPA (CRUD) y cliente en JavaFX
Java
Usuario Invitado 2 4,231 Último mensaje 10 Abril 2015, 22:43 pm
por Usuario Invitado
opencv en consola
Programación C/C++
williamfj 0 1,357 Último mensaje 19 Mayo 2015, 02:20 am
por williamfj
opencv
Java
HunterLuis 0 1,549 Último mensaje 23 Mayo 2015, 02:53 am
por HunterLuis
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines