package roc;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class EditPicture {
public BufferedImage bgm;
int [] pix;
int [][] xiangsu;
int [] times;
int [] cdf;
int [][] output;
int cdfmax;
int cdfmin;
int pxmin;
int pxmax;
int width;
int height;
String FileName;
String FilePath;
JFrame frame;
JPanel change;
JButton junzhi;
JButton xianxing;
JButton weizhi;
LinePanel lpanel;
public EditPicture(File f) {
// TODO Auto-generated constructor stub
FileName = f.getName();
FilePath = f.getParentFile().getPath();
System.out.println(this.FilePath);
try {
bgm = ImageIO.read(new FileInputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
width = bgm.getWidth();
height = bgm.getHeight();
times = new int[256];
cdf = new int[256];
xiangsu = new int[width][height];
output = new int[width][height];
bgm = getGrayPicture(bgm);
outputImage(this.bgm,"Gray"); //得到灰度图
initMatrix(); //初始化像素矩阵 xiangsu【】【】 及 各个灰度出现频数数组times【】
//界面
{
frame = new JFrame();
lpanel = new LinePanel(this.pxmin,this.pxmax);
DrawImage dimg = new DrawImage(bgm);
change = new JPanel();
xianxing = new JButton("线性 变换");
//weizhi = new JButton("XXXXX");
//junzhi.setPreferredSize(new java.awt.Dimension(74, 32));
//change.add(junzhi);
//change.add(lpanel);
//change.add(weizhi);
frame.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
frame.add(lpanel);
frame.add(dimg);
frame.setSize(600, 800);
frame.setVisible(true);
lpanel.junheng.addActionListener(new action());
//junzhi.addActionListener(new action());
lpanel.lineButton.addActionListener(new action());
lpanel.DCTbutton.addActionListener(new action());
lpanel.fftButton.addActionListener(new action());
lpanel.Edge.addActionListener(new action());
}
}
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == lpanel.junheng){
junheng();
}
if(e.getSource() == lpanel.lineButton){
linestretch();
}
if(e.getSource() == lpanel.fftButton){
usefft();
}
if(e.getSource() == lpanel.DCTbutton){
usedct();
}
if(e.getSource() == lpanel.Edge){
String s = lpanel.jComboBox1.getSelectedItem().toString();
useEdge(s);
}
}
}
//BufferedImage 图像输出函数
public void outputImage(BufferedImage img, String headName)
{
//输出灰度图
try {
ImageIO.write(img, "jpg", new File(FilePath+"/"+headName+FileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//直方图均衡函数
public void junheng(){
//初始化 cdf数组
for(int i=0; i<256; i++){
cdf[i] = 0;
}
int past = -1;
while(times[++past] == 0); //获得最小灰度点
if(past < 256){
cdf[past] = times[past];
cdfmin = cdf[past];
}
for(int i=past+1; i<256; i++)
{
if(times[i] != 0){
cdf[i] += cdf[past]+times[i]; //累加
past = i;
}
}
cdfmax = cdf[past];
// 进行均衡变换
int wid, hgt ,mul;
wid = bgm.getWidth();
hgt = bgm.getHeight();
mul = wid*hgt;
for(int i=0; i<wid; i++){
for(int j=0; j<hgt; j++){
int hv = (int) Math.round(1.0*(cdf[xiangsu[i][j]]-cdfmin)/(mul-cdfmin)*255);
output[i][j] = hv;
}
}
//输出文件
BufferedImage xhere = null;
try {
xhere = createMatrixImage(output,"D://HT"+FileName);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//窗口显示
DrawImage dimg = new DrawImage(xhere);
JFrame usod = new JFrame();
usod.add(dimg);
usod.setSize(600,600);
usod.setLocation(600, 35);
usod.setVisible(true);
}
private void showImage(BufferedImage img,String title){
DrawImage dimg = new DrawImage(img);
JFrame usod = new JFrame();
usod.add(dimg);
usod.setTitle(title);
usod.setSize(600,600);
usod.setLocation(600, 35);
usod.setVisible(true);
}
private void showImage(Image img, String title){
BufferedImage xx ;
if (img instanceof BufferedImage)
{
xx = (BufferedImage) img;
}
else{
// Create a buffered image with transparency
xx = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = xx.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
}
// Return the buffered image
DrawImage dimg = new DrawImage(xx);
JFrame usod = new JFrame();
usod.setTitle(title);
usod.add(dimg);
usod.setSize(600,600);
usod.setLocation(600, 35);
usod.setVisible(true);
}
//线性变换
public void linestretch(){
System.out.println("lineS start");
int c;
int d;
//读取UI文本框的数字
String sc = lpanel.TextC.getText();
String sd = lpanel.TextD.getText();
if(sc.length()*sd.length() !=0){
c = Integer.parseInt(sc);
d = Integer.parseInt(sd);
}
else
return;
if(c > d || c <0 || d > 256)
return;
int wid, hgt ,mul;
wid = bgm.getWidth();
hgt = bgm.getHeight();
mul = wid*hgt;
//进行线性变换
for(int i=0; i<wid; i++){
for(int j=0; j<hgt; j++){
int hv = (int) (1.0*(d-c)/(this.pxmax-pxmin)*(this.xiangsu[i][j]-pxmin)+c);
output[i][j] = hv;
}
}
BufferedImage xhere = null;
try {
xhere = createMatrixImage(output,"D://Line"+FileName);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
DrawImage dimg = new DrawImage(xhere);
JFrame usod = new JFrame();
usod.add(dimg);
usod.setSize(600,600);
usod.setLocation(600, 35);
usod.setVisible(true);
}
//转换为灰度图
public BufferedImage getGrayPicture(BufferedImage originalImage)
{
BufferedImage grayPicture;
int imageWidth = originalImage.getWidth();
int imageHeight = originalImage.getHeight();
grayPicture = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_3BYTE_BGR);
ColorConvertOp cco = new ColorConvertOp(ColorSpace
.getInstance(ColorSpace.CS_GRAY), null);
cco.filter(originalImage, grayPicture);
return grayPicture;
}
//二维矩阵输出图像文件
public BufferedImage createMatrixImage(int[][] matrix, String filedir) throws IOException{
int cx = matrix.length;
int cy = matrix[0].length;
//填充矩形高宽
int cz = 1;
//生成图的宽度
int width = cx * cz;
//生成图的高度
int height = cy * cz;
OutputStream output = new
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
.rar (27个子文件)
灰度图
bin
roc
EditPicture.class 13KB
EditPicture$action.class 1KB
OpenFilePanel$action.class 948B
DrawImage.class 2KB
EdgeDetector.class 8KB
LinePanel.class 4KB
FFT.class 6KB
Edge.class 2KB
OpenFile.class 2KB
EdgeDetectorException.class 438B
OpenFilePanel.class 1KB
Histogram.class 736B
DCT.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
roc
EdgeDetector.java 14KB
Edge.java 2KB
DCT.java 872B
FFT.java 5KB
LinePanel.java 6KB
EdgeDetectorException.java 207B
OpenFilePanel.java 994B
Histogram.java 338B
DrawImage.java 997B
EditPicture.java 14KB
OpenFile.java 4KB
.project 385B
.classpath 301B
共 27 条
- 1
资源评论
- iwantit1112017-08-26java 图像处理 多种算法实现 有部分文档
roc665335
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功