public static void showResult(Mat img) { Imgproc.resize(img, img, new Size(640, 480)); MatOfByte matOfByte = new MatOfByte(); Highgui.imencode(".jpg", img, matOfByte); byte[] byteArray = matOfByte.toArray(); BufferedImage bufImage = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufImage = ImageIO.read(in); JFrame frame = new JFrame(); frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
Mat image_tmp = your image MatOfByte matOfByte = new MatOfByte(); Highgui.imencode(".jpg", image_tmp, matOfByte); byte[] byteArray = matOfByte.toArray(); BufferedImage bufImage = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufImage = ImageIO.read(in); } catch (Exception e) { e.printStackTrace(); }
public static BufferedImage getBufferedImage(Mat mat, String type) { BufferedImage bImg = null; MatOfByte bytemat = new MatOfByte(); if (SX.isNull(mat)) { mat = getNewMat(); } Imgcodecs.imencode(type, mat, bytemat); byte[] bytes = bytemat.toArray(); InputStream in = new ByteArrayInputStream(bytes); try { bImg = ImageIO.read(in); } catch (IOException ex) { log.error("getBufferedImage: %s error(%s)", mat, ex.getMessage()); } return bImg; }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
public static MatOfByte fromNativeAddr(long addr) { return new MatOfByte(addr); }
DatagramPacket receivePacket; //stores the data sent from server ... byte[] receivedImage = receivePacket.getData(); MatOfByte mob = new MatOfByte(receivedImage); Mat img = Highgui.imdecode(mob, Highgui.IMREAD_UNCHANGED);
// for writing MatOfByte bytes = new MatOfByte(desc.reshape(1, nrows * ncols * nchannels)); byte[] bytes_ = bytes.toArray(); String s = Base64.encodeToString(bytes_, Base64.DEFAULT); // for reading byte[] bytes_ = Base64.decode(s, Base64.DEFAULT); MatOfByte bytes = new MatOfByte(bytes_); Mat desc = new Mat(nrows * ncols * nchannels, 1, type); bytes.convertTo(desc, type); desc = desc.reshape(nchannels, nrows);
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB); DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);; DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING); //first image Mat img1 = Highgui.imread("<image1 path>"); Mat descriptors1 = new Mat(); MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); detector.detect(img1, keypoints1); descriptor.compute(img1, keypoints1, descriptors1); //second image Mat img2 = Highgui.imread("<image2 path>"); Mat descriptors2 = new Mat(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); detector.detect(img2, keypoints2); descriptor.compute(img2, keypoints2, descriptors2); //matcher should include 2 different image's descriptors MatOfDMatch matches = new MatOfDMatch(); matcher.match(descriptors1,descriptors2,matches); //feature and connection colors Scalar RED = new Scalar(255,0,0); Scalar GREEN = new Scalar(0,255,0); //output image Mat outputImg = new Mat(); MatOfByte drawnMatches = new MatOfByte(); //this will draw all matches, works fine Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
private static Mat streamToMat(InputStream stream) throws IOException { byte[] data = new byte[1024]; MatOfByte chunk = new MatOfByte(); MatOfByte buf = new MatOfByte(); int read; while ((read = stream.read(data)) > 0) { chunk.fromArray(data); Mat subchunk = chunk.submat(0, read, 0, 1); buf.push_back(subchunk); } return buf; }
MatOfByte byteMat = new MatOfByte(); Highgui.imencode(".bmp", mat, byteMat); return new Image(new ByteArrayInputStream(byteMat.toArray()));
@Override public void process(InputStream inputStream) throws IOException { try { byte[] imgData = IOUtils.toByteArray(inputStream); Mat image = Imgcodecs.imdecode(new MatOfByte(imgData), Imgcodecs.CV_LOAD_IMAGE_COLOR); detectFaces(session, context, original, image); } catch (Exception ex) { getLogger().error(ex.getMessage()); ex.printStackTrace(); } } });
@Override public void process(InputStream inputStream) throws IOException { try { byte[] imgData = IOUtils.toByteArray(inputStream); Mat image = Imgcodecs.imdecode(new MatOfByte(imgData), Imgcodecs.CV_LOAD_IMAGE_COLOR); cropImage(session, context, original, image); } catch (Exception ex) { getLogger().error(ex.getMessage()); ex.printStackTrace(); } } });
Scalar RED = new Scalar(255,0,0); Scalar GREEN = new Scalar(0,255,0); MatOfByte drawnMatches = new MatOfByte(); Features2d.drawMatches(rgba,keyPoints,rgba2,keyPoints2,matches,output,GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS); Bitmap imageMatched = Bitmap.createBitmap(output.cols(), output.rows(), Bitmap.Config.RGB_565);//need to save bitmap Utils.matToBitmap(output, imageMatched); imageView.setImageBitmap(imageMatched);
@Override public void process(OutputStream outputStream) throws IOException { Rect rectCrop = new Rect(x, y, width, height); Mat croppedImage = new Mat(image, rectCrop); MatOfByte updatedImage = new MatOfByte(); Imgcodecs.imencode(".jpg", croppedImage, updatedImage); outputStream.write(updatedImage.toArray()); } });