
正文
java图像分割代码 java实现图像处理
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
opencv或matlab的meanshift对图像分割,怎么生成类似下图的效果
整个项目的结构图:
编写DetectFaceDemo.java,代码如下:
[java] view
plaincopyprint?
package com.njupt.zhb.test;
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;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// 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));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
package com.njupt.zhb.test;
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;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
public class DetectFaceDemo {
public void run() {
System.out.println("\nRunning DetectFaceDemo");
System.out.println(getClass().getResource("lbpcascade_frontalface.xml").getPath());
// Create a face detector from the cascade file in the resources
// directory.
//CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
//Mat image = Highgui.imread(getClass().getResource("lena.png").getPath());
//注意:源程序的路径会多打印一个‘/’,因此总是出现如下错误
/*
* Detected 0 faces Writing faceDetection.png libpng warning: Image
* width is zero in IHDR libpng warning: Image height is zero in IHDR
* libpng error: Invalid IHDR data
*/
//因此,我们将第一个字符去掉
String xmlfilePath=getClass().getResource("lbpcascade_frontalface.xml").getPath().substring(1);
CascadeClassifier faceDetector = new CascadeClassifier(xmlfilePath);
Mat image = Highgui.imread(getClass().getResource("we.jpg").getPath().substring(1));
// 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));
}
// Save the visualized detection.
String filename = "faceDetection.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, image);
}
}
3.编写测试类:
[java] view
plaincopyprint?
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
package com.njupt.zhb.test;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello, OpenCV");
// Load the native library.
System.loadLibrary("opencv_java246");
new DetectFaceDemo().run();
}
}
//运行结果:
//Hello, OpenCV
//
//Running DetectFaceDemo
///E:/eclipse_Jee/workspace/JavaOpenCV246/bin/com/njupt/zhb/test/lbpcascade_frontalface.xml
//Detected 8 faces
//Writing faceDetection.png
相关问答
Q1: 求一个Java切割图片的函数
package com.supben.util;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.log4j.Logger;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageUtil {
private static final Logger log = Logger.getLogger(ImageUtil.class);
/**
* 切割图片
* @param x 截点横坐标 (从左开始计数)
* @param y 截点纵坐标 (从上开始计数)
* @param width 截取java图像分割代码的宽度
* @param height 截取的长度
* @param oldpath 图片位置
* @param newpath 新生成的图片位置
*/
public static void cutImage(int x, int y, int width, int height, String oldpath, String newpath) {
FileInputStream is = null;
ImageInputStream iis = null;
//这个是获取图片扩展名的方法java图像分割代码,比如java图像分割代码:jpg。java图像分割代码我这里有现成的,如果没有,自己实现
String imgType = StringUtil.getExt(oldpath);
try {
is = new FileInputStream(oldpath);
IteratorImageReader it = ImageIO.getImageReadersByFormatName(imgType);
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is);
reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();
Point p = new Point();
p.setLocation(x, y);
Dimension d = new Dimension();
d.setSize(width, height);
Rectangle rect = new Rectangle(p, d);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(0, param);
ImageIO.write(bi, imgType, new File(newpath));
is.close();
iis.close();
} catch (Exception e) {
log.error(e);
}
}
/**
* 缩略图片
* @param oldpath 原图片
* @param newpath 新生成的图片存放地址
* @param wdith 缩略后的宽
* @param height 缩略后的高
*/
public static void scaleImage(String oldpath, String newpath, int wdith, int height) {
// 获取老的图片
File oldimg = new File(oldpath);
try {
BufferedImage bi = ImageIO.read(oldimg);
Image Itemp = bi.getScaledInstance(wdith, height, BufferedImage.SCALE_SMOOTH);
BufferedImage thumbnail = new BufferedImage(wdith, height, BufferedImage.TYPE_INT_RGB);
thumbnail.getGraphics().drawImage(Itemp, 0, 0, null);
// 缩略后的图片路径
File newimg = new File(newpath);
FileOutputStream out = new FileOutputStream(newimg);
// 绘图
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(thumbnail);
param.setQuality(1.0f, false);
encoder.encode(thumbnail);
out.close();
bi.flush();
bi = null;
} catch (IOException e) {
log.error(e);
}
}
public static void main(String[] args) {
scaleImage("D:/2.jpg", "D:/3.jpg", 50, 50);
}
}
Q2: 想从一张Jpg图像中识别出若干黑色区域,用于对答题卡的涂卡部分进行自动算分。求Java图像分割与识别技术
其实不用想得太难。
处理图像像素用BufferImage类。
首先,你得确定指定java图像分割代码的答题卡涂卡java图像分割代码的每个可涂选点的物理像素位置,这一步可以用一张空白的答题卡进行人工采集数据,做成一个数据库形式就可以了。定义好这些可涂选点定义对应的选项A\B\C\D。
然后你从扫描仪得到图像,把图像载入,进行像素分析,假设扫描答题卡放置的方向是正确的,因此不用考虑识别答题卡放置方向。再来,好样的,从之前采集的数据库得到一个可涂选的像素坐标位置,用此去识别答题卡上对应的涂选框是否涂选,以此类推逐行识别,因为你之前已经在数据库里面定义好了对应的涂选点的含义了,在这里只要往数据库一查就知道(考生涂选的是什么)了!
下一步只要将试卷上考生涂选的选项转换为数据保存到考生考试数据库。
至于如何识别考生涂选的方块,一般答题卡的涂选选框就是一个固定的大小,你只要知道它高多少像素,宽多少像素。考生涂选选项笔迹时一般都和这个选框大小的区域差不多,只要统计这个位置之内的比较黑的颜色数量占此涂选框的面积%50以上,可以认为这个是考试涂选的
选项。
还有个建议:一般答题卡上面都有一些用来给答题卡识别软件使用的定位点,你抓住这些规则就应该能更准确定位到答题卡涂选选项的位置。
Q3: 怎么用java切割出不规则图形的图片
//用基本图形拼呀
import java.awt.Graphics;
import javax.swing.*;
public class IrregulaShape extends JPanel{
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawRect(100, 50,100, 100);
g.drawArc(100, 25, 50, 50, 0, 180);
// g.fillRect(100, 50, 100, 100);
// g.fillOval(100, 25, 50, 50);
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("不规则java图像分割代码的java图像分割代码!");
IrregulaShape j = new IrregulaShape();
jFrame.add(j);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setSize(500,500);
jFrame.setLocationRelativeTo(null);
jFrame.setVisible(true);
}
}
Q4: 如何用java实现切割一张图片
BufferedImage类有一个getSubimage()方法java图像分割代码,以下来自API
public BufferedImage getSubimage(int x,
int y,
int w,
int h)
返回由指定矩形区域定义的子图像。返回的 BufferedImage 与源图像共享相同的数据数组。
参数java图像分割代码:
x - 指定矩形区域左上角的 X 坐标
y - 指定矩形区域左上角的 Y 坐标
w - 指定矩形区域的宽度
h - 指定矩形区域的高度
返回java图像分割代码:
BufferedImagejava图像分割代码,它是此 BufferedImage 的子图像。
抛出java图像分割代码:
RasterFormatException - 如果指定区域不包含在此 BufferedImage 中
Q5: 如何将一张大图片切割成若干张指定大小的小图片
在photoshop里面,用“切片”工具(快捷键是K),样式选“固定大小”-然后指定48x32的尺寸,然后一个个的切就好了,很简单。弄好之后,在顶部菜单,点“文件(file)”-“存储为web所用格式(save for web)”-ctrl+a全选-保存-保存类型:image only,就好了
java图像分割代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现图像处理、java图像分割代码的信息别忘了在本站进行查找喔。






