Example usage for org.opencv.core Mat elemSize

List of usage examples for org.opencv.core Mat elemSize

Introduction

In this page you can find the example usage for org.opencv.core Mat elemSize.

Prototype

public long elemSize() 

Source Link

Usage

From source file:pipeline.TextRegion.java

public static String SplitFiles(File fileIn) {
    String result = "";
    try {//from  w  w w.  ja v  a 2s .  c o  m
        String nomeFile = fileIn.getName();
        //System.out.println("il nome del file  "+nomeFile);
        FileInputStream in = new FileInputStream("src/pipeline/receivedImg/" + nomeFile);
        JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
        BufferedImage image = decoder.decodeAsBufferedImage();
        in.close();

        TextRecognition myget = new TextRecognition(image);
        LinkedList boxes = myget.getTextBoxes();

        String nomeFileOut = "src/pipeline/outputImg/" + Global.getJPGNameFile() + " out.jpg";
        FileOutputStream out = new FileOutputStream(nomeFileOut);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(myget.isolateText(boxes));
        out.close();

        //parte con opencv

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        File f = new File("src/pipeline/receivedImg/" + nomeFile);
        BufferedImage imageFile = ImageIO.read(f);

        byte[] data = ((DataBufferByte) imageFile.getRaster().getDataBuffer()).getData();
        Mat mat = new Mat(imageFile.getHeight(), imageFile.getWidth(), CvType.CV_8UC3);
        mat.put(0, 0, data);
        int tolleranza = 15;

        for (int i = 0; i < boxes.size(); i++) {
            TextRegion app = (TextRegion) boxes.get(i);
            //             System.out.println("RIGA: "+i+"  -> "+app.x1 +" "+app.x2 +" "+app.y1 +" "+app.y2 +" ");
            Rect roi1 = new Rect(app.x1 - tolleranza, app.y1 - tolleranza, app.x2 - app.x1 + tolleranza,
                    app.y2 - app.y1 + 2 * tolleranza);
            Mat mat1 = new Mat(mat, roi1);

            byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())];
            mat1.get(0, 0, data1);
            BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), BufferedImage.TYPE_3BYTE_BGR);
            image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);

            String nomeFileUscrita = "src/pipeline/outputImg/" + i + Global.getJPGNameFile() + " uscita.jpg";
            File tmp = new File(nomeFileUscrita);
            File output = new File(nomeFileUscrita);
            ImageIO.write(image1, "jpg", output);
            result += (i + 1) + ")" + OCR_Processing.performOCR_String2Text(output);
            tmp.delete();

        }
        f.delete();
        File foo = new File(nomeFileOut);
        foo.delete();

    } catch (Exception e) {
        System.out.println("Exception: " + e);
    }

    return result;

}

From source file:TarHadoop.TarToSeqFile.java

License:Apache License

/** Performs the conversion. */
public void execute(String inputFolder) throws Exception {
    SequenceFile.Writer output = null;
    try {//from ww  w . j  a  v a 2s .  c o m
        output = openOutputFile();
        DirectoryStructure obj = new DirectoryStructure();
        List<String> listOfImgPath = obj.getAllImgPaths(inputFolder);
        int i = 0;
        //String prevName="";
        //String value="";
        Text key = new Text();
        for (Iterator<String> iterator = listOfImgPath.iterator(); iterator.hasNext();) {
            String imgPath = (String) iterator.next();

            Mat img1 = Highgui.imread(imgPath);

            String[] tokens = imgPath.split("/");
            String currName = tokens[tokens.length - 2];
            System.out.println(i + " of imgPath = " + currName + "   " + listOfImgPath.size());

            byte[] data = new byte[(int) img1.total() * (int) img1.elemSize()];
            img1.get(0, 0, data);

            key = new Text(currName + "," + img1.rows() + "," + img1.cols());
            BytesWritable value = new BytesWritable(data);
            output.append(key, value);

            /*if(i==0)
               prevName=currName;
            if(currName.compareTo(prevName)==0){
               byte[] data=new byte[(int)img1.total()*(int)img1.elemSize()];
               img1.get(0,0,data);
               String x=new String(data,"UTF-8");
               value+=x+"###";
            }
            else{
               key=new Text(imgPath+","+img1.rows()+","+img1.cols());
               hadoopvalue=new Text(value);
               output.append(key, hadoopvalue);
               prevName=currName;
               value="";
            }*/
            ++i;
        }
    } finally {
        //if (input != null) { input.close(); }
        if (output != null) {
            output.close();
        }
    }
}

From source file:tk.year.opencv.demo.ui.ImageProvider.java

License:Open Source License

@Override
public Image get() {

    final Mat src = matProvider.get();
    final Mat dst = new Mat();
    Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2BGR);

    byte[] bytes = new byte[dst.cols() * dst.rows() * (int) dst.elemSize()];
    dst.get(0, 0, bytes);/*from  www  .  jav a2s . c o m*/

    final BufferedImage out = new BufferedImage(dst.cols(), dst.rows(), BufferedImage.TYPE_3BYTE_BGR);
    out.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), bytes);

    return out;
}

From source file:video.PictureView.java

public static BufferedImage mat2Img(Mat in) {
    BufferedImage out;/*from  w w  w.j a v a  2 s  .  com*/
    int width = in.cols();
    int height = in.height();
    byte[] data = new byte[width * height * (int) in.elemSize()];
    int type;
    in.get(0, 0, data);

    if (in.channels() == 1) {
        type = BufferedImage.TYPE_BYTE_GRAY;
    } else {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }

    out = new BufferedImage(width, height, type);

    out.getRaster().setDataElements(0, 0, width, height, data);
    return out;
}

From source file:video.PictureView.java

public static Mat bufferedImageToMat(BufferedImage in) {
    Mat out;
    byte[] data;/*w w w  .  j  av a2 s . c  om*/
    int r, g, b;
    int height = in.getHeight();
    int width = in.getWidth();
    if (in.getType() == BufferedImage.TYPE_INT_RGB || in.getType() == BufferedImage.TYPE_INT_ARGB) {
        out = new Mat(height, width, CvType.CV_8UC3);
        data = new byte[height * width * (int) out.elemSize()];
        int[] dataBuff = in.getRGB(0, 0, width, height, null, 0, width);
        for (int i = 0; i < dataBuff.length; i++) {
            data[i * 3 + 2] = (byte) ((dataBuff[i] >> 16) & 0xFF);
            data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
            data[i * 3] = (byte) ((dataBuff[i]) & 0xFF);
        }
    } else if (in.getType() == BufferedImage.TYPE_3BYTE_BGR) {
        out = new Mat(height, width, CvType.CV_8UC3);
        data = new byte[height * width * (int) out.elemSize()];
        int[] dataBuff = in.getRGB(0, 0, width, height, null, 0, width);
        for (int i = 0; i < dataBuff.length; i++) {
            data[i * 3 + 2] = (byte) ((dataBuff[i]) & 0xFF);
            data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
            data[i * 3] = (byte) ((dataBuff[i] >> 16) & 0xFF);
        }
    } else {
        out = new Mat(height, width, CvType.CV_8UC1);
        data = new byte[height * width * (int) out.elemSize()];
        int[] dataBuff = in.getRGB(0, 0, width, height, null, 0, width);
        for (int i = 0; i < dataBuff.length; i++) {
            r = (byte) ((dataBuff[i] >> 16) & 0xFF);
            g = (byte) ((dataBuff[i] >> 8) & 0xFF);
            b = (byte) ((dataBuff[i]) & 0xFF);
            data[i] = (byte) ((0.21 * r) + (0.71 * g) + (0.07 * b)); //luminosity
        }
    }
    out.put(0, 0, data);
    return out;
}