OPenCv- Mapping Images - java

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] );

Related

How To Convert Image .dcm to Graycale in 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());
}
}
}

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

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()

Convert text to image file on Android

I have a text document (.txt). I want to convert it to an image (.png or .jpg). For example, black text on white background. How can I do that programmatically?
I think the proper way for multi-line text is this:
String text = "This \nis \nmultiline";
final Rect bounds = new Rect();
TextPaint textPaint = new TextPaint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
textPaint.getTextBounds(text, 0, text.length(), bounds);
StaticLayout mTextLayout = new StaticLayout(text, textPaint,
bounds.width(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
int maxWidth = -1;
for (int i = 0; i < mTextLayout.getLineCount(); i++) {
if (maxWidth < mTextLayout.getLineWidth(i)) {
maxWidth = (int) mTextLayout.getLineWidth(i);
}
}
final Bitmap bmp = Bitmap.createBitmap(maxWidth , mTextLayout.getHeight(),
Bitmap.Config.ARGB_8888);
bmp.eraseColor(Color.BLACK);// just adding black background
final Canvas canvas = new Canvas(bmp);
mTextLayout.draw(canvas);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
this (untested) code should get you on the right track.
void foo(final String text) throws IOException{
final Paint textPaint = new Paint() {
{
setColor(Color.WHITE);
setTextAlign(Paint.Align.LEFT);
setTextSize(20f);
setAntiAlias(true);
}
};
final Rect bounds = new Rect();
textPaint.getTextBounds(text, 0, text.length(), bounds);
final Bitmap bmp = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.RGB_565); //use ARGB_8888 for better quality
final Canvas canvas = new Canvas(bmp);
canvas.drawText(text, 0, 20f, textPaint);
FileOutputStream stream = new FileOutputStream(...); //create your FileOutputStream here
bmp.compress(CompressFormat.PNG, 85, stream);
bmp.recycle();
stream.close();
}
This is what you need:
http://mvnrepository.com/artifact/org.apache.xmlgraphics/xmlgraphics-commons/1.3.1
I can provide you sample code if you want.
Edit: simple example:
package v13;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.xmlgraphics.image.codec.png.PNGImageEncoder;
public class Deneme {
public static void main(String[]args){
JFrame jf = new JFrame();
jf.setVisible(true);
JPanel jp = new JPanel();
jf.add(jp);
JLabel jl = new JLabel("trial text");
jf.add(jl);
jf.setSize(300, 200);
JFileChooser jfc = new JFileChooser();
int temp = jfc.showSaveDialog(jfc);
if (temp == JFileChooser.APPROVE_OPTION) {
System.out.println(jfc.getSelectedFile());
Component myComponent = jf;
Dimension size = myComponent.getSize();
BufferedImage myImage = new BufferedImage(size.width,
size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
OutputStream out = new FileOutputStream(jfc
.getSelectedFile().getAbsolutePath()
+ ".png");
PNGImageEncoder encoder = new PNGImageEncoder(out, null);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}

Categories

Resources