How To Convert Image .dcm to Graycale in Java - java

please i need help
I am trying to convert image .dcm to grayscale in java but i couldn't.
i wan't to know how to read image.dcm in java and convert it to grayscale.
note :: this code is already run for image .jpg,.png
package otsuthresholding;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class OtsuThresholding {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME );
File input = new File("p.dcm");
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.getHeight(),image.getWidth(),CvType.CV_8UC1);
Imgproc.cvtColor(mat, mat1, Imgproc.COLOR_RGB2GRAY);
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_BYTE_GRAY);
image1.getRaster().setDataElements(0, 0, mat1.cols(), mat1.rows(), data1);
File ouptut = new File("grayscale.dcm");
ImageIO.write(image1, "dcm", ouptut);
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Related

How can I crop Detected Face from the Video Streaming and store it in a folder using OpenCV Java?

I am using OpenCV 2.4.9, Java language and eclipse. My Face Detection code is given below. The question is how can I crop out the detected face and store it in a folder?
I've trying hard for it but couldn't get the required output.
package code03;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.VideoCapture;
import org.opencv.objdetect.CascadeClassifier;
public class VideoPanel2 extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
//***********************************************************************************************
private BufferedImage image;
int count = 1;
//***********************************************************************************************
public VideoPanel2()
{
super();
}
//***********************************************************************************************
public BufferedImage getimage()
{
return image;
}
//***********************************************************************************************
public void setimage(BufferedImage newimage)
{
image = newimage;
return;
}
//***********************************************************************************************
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (this.image==null) return;
g.drawImage(this.image,10,492,650,43,this.image.getWidth(),this.image.getHeight(), count, count, null);
}
//***********************************************************************************************
public void DatainIt() throws Exception{
JFrame frame = new JFrame("Face Detection");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
System.loadLibrary("opencv_java249");
CascadeClassifier faceDetector = new CascadeClassifier("./res/haarcascade_frontalface_alt.xml");
//CascadeClassifier faceDetector = new CascadeClassifier("./res/lbpcascade_frontalface.xml");
VideoPanel2 vidPanel = new VideoPanel2();
frame.setContentPane(vidPanel);
//BUTTON
JButton save = new JButton("Add a new Person");
//save.setBounds(2, 2, 30, 80);
JPanel pbutton = new JPanel();
pbutton.add(save);
//TextField
JTextField p_name = new JTextField(25);
frame.add(p_name);
frame.add(pbutton);
frame.setVisible(true);
save.addActionListener(this);
Mat webcam_image = new Mat();
MatToBufImg mat2Buf = new MatToBufImg();
VideoCapture capture = new VideoCapture(0);
if(capture.isOpened())
{
Thread .sleep(100); //Give time to webcam to initialize itself
while(true)
{
capture.read(webcam_image);
if(!webcam_image.empty())
{
frame.setSize(webcam_image.width()+40, webcam_image.height()+60);
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(webcam_image, faceDetections);
for(Rect rect : faceDetections.toArray())
{
Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255,0));
//Mat croppedImage = setimage(rect);
}
mat2Buf.setMatrix(webcam_image, ".jpg");
//Highgui.imwrite("webcam_image.jpg", faceDetections);
//File file = new File("Image" + "." + ".jpg");
//ImageIO.write((RenderedImage) webcam_image, ".jpg", file);
vidPanel.setimage(mat2Buf.getBufferedImage());
vidPanel.repaint();
// get the video stream
//BufferedImage bi = mat2Buf.getBufferedImage();//getimage();
//bi.getSubimage(arg0, arg1, arg2, arg3);
//File outputfile= new File("D:\\Java Project\\FaceRecognition\\src\\code03\\Face Database\\saved.jpg");
//ImageIO.write((RenderedImage) bi, "jpg", outputfile);
}
else
{
System.out.println("Problems with WebCam Capture");
break;
}
}
}//end if
capture.release();
}//end DatainIt()
//***********************************************************************************************
public static void main(String arg[]) throws Exception{
VideoPanel2 vid = new VideoPanel2();
vid.DatainIt();
}//end main
//***********************************************************************************************
public void actionPerformed(ActionEvent arg0) {
}
}//end of class
The code of other file used in this class is:
package code03;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.highgui.Highgui;
public class MatToBufImg{
Mat matrix;
MatOfByte mob;
String fileExten;
public MatToBufImg(){
}
public MatToBufImg(Mat amatrix, String fileExt){
matrix = amatrix;
fileExten = fileExt;
}
public void setMatrix(Mat amatrix, String fileExt){
matrix = amatrix;
fileExten = fileExt;
mob = new MatOfByte();
}
public BufferedImage getBufferedImage(){
Highgui.imencode(fileExten, matrix, mob);
byte[] byteArray = mob.toArray();
BufferedImage bufImage = null;
try{
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
}catch(Exception e){
e.printStackTrace();
}
return bufImage;
}
}
In your code, you are detecting Face rectangles as MatOfRect and drawing a rectangle on the video.
Core.rectangle(webcam_image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255,0));
Here, you already have the roi of the image you need. So crop the roi part of the frame as:
Mat faceROI = new Mat(webcam_image,rect);
Highgui.imwrite("Face_frameNumber_faceInImageNumber.jpeg", faceROI);
Also, please consider moving to Opencv 3.1/latest version. You will benefit from the new features such as optimisations and many algorithms which have been contributed over past year.
OPENCV 3 onwards:
Highgui is now broken to VideoIO and ImgCodecs. Also, Core.rectangle like functions has moved to ImgProc.

Java - Motion / Object detection (Human detection) using OpenCV

I got the video stream from cctv camera using opencv. Now I want to detect a simple motion / object from this video stream. For example, if any person come in any selected zone then rectangular border should be generated around him. I search some opencv tutorial, but I didn't got any proper solution or idea.I used the following code to display video stream.
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.highgui.VideoCapture;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
public class VideoStream
{
static BufferedImage tmpImg=null;
public static void main(String args[]) throws InterruptedException
{
System.out.println("opencv start..");
// Load native library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture camView=new VideoCapture();
camView.open("http://192.168.1.7:80/cgi-bin/view.cgi?chn=6&u=admin&p=");
if(!camView.isOpened())
{
System.out.println("Camera Error..");
}
else
{
System.out.println("Camera successfully opened");
}
videoCamera cam=new videoCamera(camView);
//Initialize swing components
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(cam);
frame.setSize(1024,768);
frame.setVisible(true);
while(camView.isOpened())
{
cam.repaint();
}
}
}
#SuppressWarnings("serial")
class videoCamera extends JPanel
{
VideoCapture camera;
Mat mat=new Mat();
public videoCamera(VideoCapture cam)
{
camera = cam;
}
public BufferedImage Mat2BufferedImage(Mat m)
{
int type = BufferedImage.TYPE_BYTE_GRAY;
if (m.channels() > 1)
{
type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels() * m.cols() * m.rows();
byte[] b = new byte[bufferSize];
m.get(0, 0, b); // get all the pixels
BufferedImage img = new BufferedImage(m.cols(), m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);
return img;
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Mat mat = new Mat();
camera.read(mat);
BufferedImage image = Mat2BufferedImage(mat);
g.drawImage(image,0,0,image.getWidth(),image.getHeight(), null);
}
}
Can any one knows how could we do that.
please check this code, but i implement it by python
import cv2, time, pandas
from datetime import datetime
first_frame = None
status_list = [None,None]
times = []
df=pandas.DataFrame(columns=["Start","End"])
video = cv2.VideoCapture('rtsp://admin:Paxton10#10.199.27.128:554')
while True:
check, frame = video.read()
status = 0
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21),0)
if first_frame is None:
first_frame=gray
continue
delta_frame=cv2.absdiff(first_frame,gray)
thresh_frame=cv2.threshold(delta_frame, 30, 255, cv2.THRESH_BINARY)[1]
thresh_frame=cv2.dilate(thresh_frame, None, iterations=2)
(cnts,_)=cv2.findContours(thresh_frame.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 200000:
continue
status=1
(x, y, w, h)=cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0), 3)
status_list.append(status)
status_list=status_list[-2:]
if status_list[-1]==1 and status_list[-2]==0:
times.append(datetime.now())
if status_list[-1]==0 and status_list[-2]==1:
times.append(datetime.now())
#cv2.imshow("Gray Frame",gray)
#cv2.imshow("Delta Frame",delta_frame)
imS = cv2.resize(thresh_frame, (640, 480))
cv2.imshow("Threshold Frame",imS)
imS = cv2.resize(frame, (640, 480))
cv2.imshow("Color Frame",imS)
#cv2.imshow("Color Frame",frame)
key=cv2.waitKey(1)
if key == ord('q'):
if status == 1:
times.append(datetime.now())
break
print(status_list)
print(times)
for i in range(0, len(times), 2):
df = df.append({"Start": times[i],"End": times[i+1]}, ignore_index=True)
df.to_csv("Times.csv")
video.release()
cv2.destroyAllWindows

Java MS Word and PDF conversion to image (png/jpg) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm looking for a free lib to convert from MS Word, WordPerfect and PDF to image.
Is anyone aware of any good, and up-to date JAVA library?
To convert PDF to images You can use PDFbox
Following is the code to convert PDF to images using pdfbox api
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageNode;
public List<String> generateImages(String pdfFile) throws IOException {
String imagePath = "/Users/$user/pdfimages/";
List <String> fileNames = new ArrayList<String>();
document = PDDocument.load(pdfFile); //// load pdf
node = document.getDocumentCatalog().getPages(); ///// get pages
List<PDPage> kids = node.getKids();
int count=0;
for(PDPage page : kids) { ///// iterate
BufferedImage img = page.convertToImage(BufferedImage.TYPE_INT_RGB,128);
File imageFile = new File(imagePath+ count++ + ".jpg");
ImageIO.write(img, "jpg", imageFile);
fileNames.add(imageFile.getName());
}
return fileNames;
}
Also another library ApachePOI can be used to convert PDF to Images
Here is the code sample
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class JavaApplication12 {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream is = new FileInputStream(“D:/Presentation1.ppt”);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, 1);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setColor(Color.white);
graphics.clearRect(0, 0, pgsize.width, pgsize.height);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
slide[i].draw(graphics);
// save the output
FileOutputStream out = new FileOutputStream(“slide-” + (i + 1) + “.png”);
javax.imageio.ImageIO.write(img, “png”, out);
out.close();
}
}
}
To convert MS Word to images you can have look at question posted here
Which uses JODConverter
JODConverter automates all conversions supported by OpenOffice.org, including
Any format to PDF
o OpenDocument (Text, Spreadsheet, Presentation) to PDF
o Word to PDF; Excel to PDF; PowerPoint to PDF
o RTF to PDF; WordPerfect to PDF; ...
And more
o OpenDocument Presentation (odp) to Flash; PowerPoint to Flash
o RTF to OpenDocument; WordPerfect to OpenDocument
o Any format to HTML (with limitations)
o Support for OpenOffice.org 1.0 and old StarOffice formats
//DOC to .jpeg
package org.doc;
import java.io.File;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
public class DocToImage {
public static void main(String[] args) {
try {
String sourcePath = "D://G.doc";
Document doc = new Document(sourcePath);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setJpegQuality(100);
options.setResolution(100);
options.setUseHighQualityRendering(true);
for (int i = 0; i < doc.getPageCount(); i++) {
String imageFilePath = "E://"+ "images" + File.separator + "img_" + i + ".jpeg";
options.setPageIndex(i);
doc.save(imageFilePath, options);
}
System.out.println("Done...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*Here is the link from where we can download latest Aspose word jar.
http://www.aspose.com/java/word-component.aspx*/
//PDF to .png
package org.pdf;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
public class PdfToImage {
public static void main(final String[] args) throws Exception {
String storagePath = "E://dd.pdf";
//Image Save Directory
String realPathtopdfImageSaveDir = "E://uploads/";
RandomAccessFile raf = new RandomAccessFile(storagePath, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
int numPgs = pdffile.getNumPages();
for (int i = 0; i < numPgs; i++) {
PDFPage page = pdffile.getPage(i);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
Image img = page.getImage(rect.width, rect.height, rect, null, true, true);
// save it as a file
BufferedImage bImg = toBufferedImage(img);
File yourImageFile = new File(realPathtopdfImageSaveDir +File.separator + "page_" + i + ".png");
ImageIO.write(bImg, "png", yourImageFile);
}
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
image = new ImageIcon(image).getImage();
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
int transparency = Transparency.OPAQUE;
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
} catch (HeadlessException e) {
System.out.println("The system does not have a screen");
}
if (bimage == null) {
int type = BufferedImage.TYPE_INT_RGB;
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
Graphics g = bimage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
// jar required pdf-renderer.jar

OPenCv- Mapping Images

I am trying to crop an face out of an image and I do that by first detecting the face then mapping the area to another image but something is wrong.
Here is my code if you could help:
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.objdetect.CascadeClassifier;
public class FaceDetector {
public static void main(String[] args) {
BufferedImage img = getImage("C:\\Users\\Yousra\\Desktop\\test4.jpg");
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
System.out.println("\nRunning FaceDetector");
CascadeClassifier faceDetector = new CascadeClassifier("D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml");
CascadeClassifier eyeDetector = new CascadeClassifier("D:\\CS\\opencv\\sources\\data\\haarcascades\\haarcascade_eye.xml");
Mat image = Highgui
.imread("C:\\Users\\Yousra\\Desktop\\test4.jpg");
MatOfRect faceDetections = new MatOfRect();
faceDetector.detectMultiScale(image, faceDetections);
if( faceDetections.toArray().length == 0){
// load("C:\\Users\\Yousra\\Desktop\\download.jpg") ){
System.out.println("not found");
}
System.out.println(String.format("Detected %s faces", faceDetections.toArray().length));
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));
}
WritableRaster cr = img.getRaster();
WritableRaster wr = img.copyData(null);
for(int b=0; b<94; b++){
for(int a=0; a<94; a++){
for(int h = faceDetections.toArray()[0].y; h< 60+ 94; h++){
for(int w = faceDetections.toArray()[0].x; w< 50+ 94; w++){
wr.setSample(b, a, 0, cr.getSample(w, h, 0));
}
}
}
}
BufferedImage img2= new BufferedImage(94, 94, BufferedImage.TYPE_INT_RGB);
img2.setData(wr);
JFrame frame = new JFrame("uiuxcu");
frame.getContentPane().add(new JLabel(new ImageIcon(img2)));
frame.pack();
frame.setVisible(true);
String filename = "ouput.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
public static BufferedImage getImage(String imageName) {
try {
File input = new File(imageName);
BufferedImage image = ImageIO.read(input);
return image;
} catch (IOException ie) {
System.out.println("Error:" + ie.getMessage());
}
return null;
}}
The cropped image is not accurate at all and it doesn't show the face
why not simply use submat http://docs.opencv.org/java/org/opencv/core/Mat.html#submat(org.opencv.core.Rect)
Mat face_cropped = image.submat( faceDetections.toArray()[0] );

Fill in and detect contour rectangles in Java OpenCV

I have an initial starting image through some processing that looks like this
What I want to do it is to fill in the contours so it looks somewhat like this
and find the best fit parallelograms of the two (or more) squares which would let me get each one of the four bounding lines like this
If anyone could point me to the right functions that would help, but I can't find anything helpful. I've tried many distorted rectangle correctors but couldn't get them to work.
Heres current source code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javacvtesting;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfInt;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
/**
*
* #author Arhowk
*/
public class JavaCVTesting {
/**
* #param args the command line arguments
*//*
*/
public static BufferedImage convert(Mat m){
Mat image_tmp = m;
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".png", 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();
}finally{
return bufImage;
}
}
public static Mat convert(BufferedImage i){
BufferedImage image = i;
byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
Mat mat = new Mat(image.getHeight(),image.getWidth(), CvType.CV_8UC3);
mat.put(0, 0, data);
return mat;
}
public static void show(BufferedImage i){
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(i)));
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Highgui.imread("D:\\0_image.png");
Imgproc.cvtColor(src, src, Imgproc.COLOR_BGR2HSV);
Mat dest = new Mat();
// Mat dest = new Mat(src.width(), src.height(), src.type());
Core.inRange(src, new Scalar(58,125,0), new Scalar(256,256,256), dest);
Mat erode = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
Mat dilate = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5,5));
Imgproc.erode(dest, dest, erode);
Imgproc.erode(dest, dest, erode);
Imgproc.dilate(dest, dest, dilate);
Imgproc.dilate(dest, dest, dilate);
List<MatOfPoint> contours = new ArrayList<>();
Imgproc.findContours(dest, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
Imgproc.drawContours(dest, contours, -1, new Scalar(255,255,0));
Panel p = new Panel();
p.setImage(convert(dest));
p.show();
}
}
To determine the outer contours you may use findContours with mode RETR_EXTERNAL:
List<MatOfPoint> contours = new ArrayList<>();
Mat dest = Mat.zeros(mat.size(), CvType.CV_8UC3);
Scalar white = new Scalar(255, 255, 255));
// Find contours
Imgproc.findContours(image, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
// Draw contours in dest Mat
Imgproc.drawContours(dest, contours, -1, white);
Saving the obtained Mat you will get:
To fill in the obtained contours:
for (MatOfPoint contour: contours)
Imgproc.fillPoly(dest, Arrays.asList(contour), white);
Saving the new resulting Mat you will get this:
And to find the best fit rectangle for each contour:
Scalar green = new Scalar(81, 190, 0);
for (MatOfPoint contour: contours) {
RotatedRect rotatedRect = Imgproc.minAreaRect(new MatOfPoint2f(contour.toArray()));
drawRotatedRect(dest, rotatedRect, green, 4);
}
public static void drawRotatedRect(Mat image, RotatedRect rotatedRect, Scalar color, int thickness) {
Point[] vertices = new Point[4];
rotatedRect.points(vertices);
MatOfPoint points = new MatOfPoint(vertices);
Imgproc.drawContours(image, Arrays.asList(points), -1, color, thickness);
}
Finally you get this resulting image:
sounds like you want floodfill()

Categories

Resources