List of usage examples for org.opencv.core Mat put
public int put(int row, int col, byte[] data)
From source file:MainShapeConversion.java
public static void main(String[] args) { try {//from w w w. j ava 2 s . c o m System.loadLibrary(Core.NATIVE_LIBRARY_NAME); File input = new File("D://teste.png"); BufferedImage image = ImageIO.read(input); byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3); mat.put(0, 0, data); Mat mat1 = new Mat(image.getWidth(), image.getHeight(), CvType.CV_8UC3); Core.flip(mat, mat1, -1); //-1 invert , 1 normal byte[] data1 = new byte[mat1.rows() * mat1.cols() * (int) (mat1.elemSize())]; mat1.get(0, 0, data1); BufferedImage image1 = new BufferedImage(mat1.cols(), mat1.rows(), 5); image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1); File output = new File("D://hsv.jpg"); ImageIO.write(image1, "jpg", output); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }
From source file:NeuQuant.java
License:Open Source License
public Mat createPalette() throws IOException { Mat palette = Mat.zeros(netsize, 1, CvType.CV_8UC3); for (int i = 0; i < netsize; i++) { int bb = colormap[i][0]; int gg = colormap[i][1]; int rr = colormap[i][2]; palette.put(i, 0, new double[] { bb, gg, rr }); }//from w w w. j a v a2 s .c om return palette; }
From source file:ImagemScreen.java
public static void main(String[] args) throws InterruptedException, AWTException, IOException { JFrame f = new JFrame("Image Example"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);/*from w w w .ja v a 2s . c om*/ } }); ImagemScreen si = new ImagemScreen(); f.add("Center", si); // JComboBox choices = new JComboBox(si.getDescriptions()); choices.setActionCommand("SetFilter"); choices.addActionListener(si); // JComboBox formats = new JComboBox(si.getFormats()); formats.setActionCommand("Formats"); formats.addActionListener(si); // JComboBox opcao = new JComboBox(si.getComando()); opcao.setActionCommand("Escolher"); opcao.addActionListener(si); // JPanel panel = new JPanel(); panel.add(opcao); panel.add(choices); panel.add(new JLabel("Salvar como")); panel.add(formats); f.add("South", panel); f.pack(); f.setVisible(true); System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // load native library of opencv byte[] bgrPixelData = ImagemScreen.GetCurrentScreenImage(); Mat screenFrame = new Mat(600, 600, CvType.CV_8UC3); screenFrame.put(0, 0, bgrPixelData); // Create a blank output image, that we will draw onto. Mat outputFrame = new Mat(screenFrame.size(), CvType.CV_8UC3); // Save output and display the openCV Mat image onto the screen. //Gravando imagem no disco try { // ImageToScreen.DrawImageToScreen("c:\\temp\\wtf.png", outputFrame); BufferedImage teste = createBufferedImage(outputFrame); //Obtemos o contexto grfico dessa imagem Graphics2D g2d = teste.createGraphics(); File outputfile = new File("f:\\saved.png"); boolean write = ImageIO.write(teste, "png", outputfile); if (write) { System.out.println("Tudo OK!"); } g2d.dispose(); } catch (IOException e) { System.out.println("Error: " + e); } //Liberamos o contexto. }
From source file:Questao2.java
void ruidoGaussiano() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat original_Bgr = image.clone(); // cria uma imagem e inicializa com valores aleatorios Mat mGaussian_noise = new Mat(original_Bgr.size(), original_Bgr.type()); System.out.print("Valor Principal: "); int mean = in.nextInt(); System.out.print("Desvio Padro: "); int desv = in.nextInt(); // randn(matriz destino, mean value, desvio padrao) randn(mGaussian_noise, mean, desv);// www . j a v a2 s.co m // aplicacao do ruido: original(clone) + mGaussian_noise for (int m = 0; m < original_Bgr.rows(); m++) { for (int n = 0; n < original_Bgr.cols(); n++) { double[] val = new double[3]; for (int i = 0; i < original_Bgr.get(m, n).length; i++) { val[i] = original_Bgr.get(m, n)[i] + mGaussian_noise.get(m, n)[i]; } original_Bgr.put(m, n, val); } } // normalize(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida) normalize(original_Bgr, original_Bgr, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); // salva resultado do ruido gaussiano na imagem "gaussian.jpg" Imgcodecs.imwrite("gaussian.jpg", original_Bgr); showResult("gaussian.jpg"); }
From source file:Questao2.java
void ruidoSalPimenta() { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // obtem clone da matriz original Mat saltPepper_img = image.clone(); // cria matriz para o ruido e inicializa com valor aleatorios Mat mSaltPepper_noise = new Mat(saltPepper_img.size(), saltPepper_img.type()); // randn(matriz destino, valor principal (espectativa), desvio padrao) randn(mSaltPepper_noise, 0, 255);/*from ww w . j a v a 2 s . c om*/ System.out.print("Valor Mnimo: "); int min = in.nextInt(); System.out.print("Valor Mximo: "); int max = in.nextInt(); // utilizando da matriz de numeros aleatorios, verifica valores // muito baixos e os substituem por zero na matriz resultante (copia da original) // e os valores muito altos sao substituidos por 255 for (int m = 0; m < saltPepper_img.rows(); m++) { for (int n = 0; n < saltPepper_img.cols(); n++) { double[] val = new double[3]; if (mSaltPepper_noise.get(m, n)[0] < min && mSaltPepper_noise.get(m, n)[1] < min && mSaltPepper_noise.get(m, n)[2] < min) { for (int i = 0; i < saltPepper_img.get(m, n).length; i++) { val[i] = 0; } saltPepper_img.put(m, n, val); } if (mSaltPepper_noise.get(m, n)[0] > max && mSaltPepper_noise.get(m, n)[1] > max && mSaltPepper_noise.get(m, n)[2] > max) { for (int i = 0; i < saltPepper_img.get(m, n).length; i++) { val[i] = 255; } saltPepper_img.put(m, n, val); } } } // normalize(matriz entrada, matriz saida, valor minimo, valor maximo, tipo de normalizacao, tipo da imagem de saida) normalize(saltPepper_img, saltPepper_img, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC3); /** * Salva imagem resultante em saltapepper.jpg */ Imgcodecs.imwrite("saltpepper.jpg", saltPepper_img); showResult("saltpepper.jpg"); }
From source file:Fiji_OpenCV.java
License:Creative Commons License
public void process(int[] pixels) { int channels = 3; byte[] buf = new byte[pixels.length * channels]; for (int i = 0; i < pixels.length; i++) { buf[i * channels] = (byte) (0x000000ff & (pixels[i])); buf[i * channels + 1] = (byte) (0x000000ff & (pixels[i] >>> 8)); buf[i * channels + 2] = (byte) (0x000000ff & (pixels[i] >>> 16)); }/*from w w w. j a va2 s. com*/ Mat image = new Mat(width, height, CvType.CV_8UC3); image.put(0, 0, buf); // Create a face detector from the cascade file in the resources // directory. CascadeClassifier faceDetector = new CascadeClassifier( getClass().getResource("/opencv/data/haarcascades/haarcascade_frontalface_alt2.xml").getPath()); // Detect faces in the image. // MatOfRect is a special container class for Rect. MatOfRect faceDetections = new MatOfRect(); faceDetector.detectMultiScale(image, faceDetections); System.out.println(String.format("Detected %s faces", faceDetections.toArray().length)); // Draw a bounding box around each face. for (Rect rect : faceDetections.toArray()) { Core.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0)); } image.get(0, 0, buf); for (int i = 0; i < pixels.length; i++) { pixels[i] = 0x80000000 + ((int) (buf[i * channels + 2]) << 16) + ((int) (buf[i * channels + 1]) << 8) + ((int) (buf[i * channels + 0])); } this.ip = new ColorProcessor(width, height, pixels); }
From source file:TelaTakeFoto.java
/** * Mtodo img2Mat : BufferedImage to Mat/* ww w . j a v a2 s . com*/ * * @param in * @return */ public static Mat img2Mat(BufferedImage in) { Mat out = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC3); byte[] data = new byte[in.getWidth() * in.getHeight() * (int) out.elemSize()]; int[] dataBuff = in.getRGB(0, 0, in.getWidth(), in.getHeight(), null, 0, in.getWidth()); for (int i = 0; i < dataBuff.length; i++) { data[i * 3] = (byte) ((dataBuff[i])); data[i * 3 + 1] = (byte) ((dataBuff[i])); data[i * 3 + 2] = (byte) ((dataBuff[i])); } out.put(0, 0, data); return out; }
From source file:OCV__LoadLibrary.java
License:Open Source License
/** * a color data of ImageJ -> a CV_8UC3 data of OpenCV * @param src_ar a color data of ImageJ (int[]) * @param dst_cv_8uc3 CV_8UC3 data of OpenCV * @param imw width of image/*from ww w .ja v a2 s.c om*/ * @param imh height of image */ public static void intarray2mat(int[] src_ar, Mat dst_cv_8uc3, int imw, int imh) { if ((dst_cv_8uc3.width() != imw) || (dst_cv_8uc3.height() != imh) || src_ar.length != imw * imh) { IJ.error("Wrong image size"); } for (int y = 0; y < imh; y++) { for (int x = 0; x < imw; x++) { int ind = x + imw * y; byte b = (byte) (src_ar[ind] & 0xff); byte g = (byte) ((src_ar[ind] >> 8) & 0xff); byte r = (byte) ((src_ar[ind] >> 16) & 0xff); dst_cv_8uc3.put(y, x, new byte[] { b, g, r }); } } }
From source file:OCV_WarpAffine.java
License:Open Source License
@Override public void run(ImageProcessor ip) { int imw = ip.getWidth(); int imh = ip.getHeight(); Size size = new Size((double) imw, (double) imh); Mat mat = new Mat(2, 3, CvType.CV_64FC1); for (int i = 0; i < 2; i++) { mat.put(i, 0, new double[] { Double.valueOf(rt.getStringValue(0, i).replaceAll("\"|'", "")) }); mat.put(i, 1, new double[] { Double.valueOf(rt.getStringValue(1, i).replaceAll("\"|'", "")) }); mat.put(i, 2, new double[] { Double.valueOf(rt.getStringValue(2, i).replaceAll("\"|'", "")) }); }/* w w w . j a v a 2 s. co m*/ if (ip.getBitDepth() == 8) { byte[] srcdst_ar = (byte[]) ip.getPixels(); Mat src_mat = new Mat(imh, imw, CvType.CV_8UC1); Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC1); src_mat.put(0, 0, srcdst_ar); Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]); dst_mat.get(0, 0, srcdst_ar); } else if (ip.getBitDepth() == 16) { short[] srcdst_ar = (short[]) ip.getPixels(); Mat src_mat = new Mat(imh, imw, CvType.CV_16UC1); Mat dst_mat = new Mat(imh, imw, CvType.CV_16UC1); src_mat.put(0, 0, srcdst_ar); Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]); dst_mat.get(0, 0, srcdst_ar); } else if (ip.getBitDepth() == 24) { int[] srcdst_ar = (int[]) ip.getPixels(); Mat src_mat = new Mat(imh, imw, CvType.CV_8UC3); Mat dst_mat = new Mat(imh, imw, CvType.CV_8UC3); OCV__LoadLibrary.intarray2mat(srcdst_ar, src_mat, imw, imh); Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]); OCV__LoadLibrary.mat2intarray(dst_mat, srcdst_ar, imw, imh); } else if (ip.getBitDepth() == 32) { float[] srcdst_ar = (float[]) ip.getPixels(); Mat src_mat = new Mat(imh, imw, CvType.CV_32FC1); Mat dst_mat = new Mat(imh, imw, CvType.CV_32FC1); src_mat.put(0, 0, srcdst_ar); Imgproc.warpAffine(src_mat, dst_mat, mat, size, FLAGS_INT[flags_ind]); dst_mat.get(0, 0, srcdst_ar); } else { IJ.error("Wrong image format"); } }
From source file:MainBox.java
public static void main(String[] args) { try {/* w ww .ja va 2 s .com*/ int kernelSize = 9; //ao aumentar o valor a imagem fica mais embassada System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Mat source = Highgui.imread("D://teste_gray.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat destination = new Mat(source.rows(), source.cols(), source.type()); Mat kernel = Mat.ones(kernelSize, kernelSize, CvType.CV_32F); for (int i = 0; i < kernel.rows(); i++) { for (int j = 0; j < kernel.cols(); j++) { double[] m = kernel.get(j, j); for (int k = 0; k < m.length; k++) { m[k] = m[k] / (kernelSize * kernelSize); } kernel.put(i, j, m); } } Imgproc.filter2D(source, destination, -1, kernel); Highgui.imwrite("D://Box.jpg", destination); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); } }