package imagetools;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
/*
* Uses IQR and other Statistics by row to attempt to get rid of minor noise from an edge detected image
*
* This tool could be expanded to color by blurring out any small objects but the larger the correction, the worse the problem would
*be, try finding close colors on the other side and getting a smooth slope rather than a straight line.
*
*
* aevans 9.6.2013
*/
public class Declutter implements Proximity{
private Image image=Image.getInstance();
private byte[] bimage;
private int IQR = 0;
private int median = 0;
private int um = 0;
private int lm = 0;
private int lo = 0;
private int uo = 0;
/**
* Empty Constructor
*/
public Declutter() {
// TODO empty constructor
}
/**
* Constructor with byte array for image
* @param img
*/
public Declutter(byte[] img) {
// TODO constructor with image byte array and sets an image
bimage = img;
createFromBytes();
}
/**
* Constructor with image path
* @param ip
*/
public Declutter(String imagepath) {
// TODO constructor with image byte array and sets an image
createFromImagePath(imagepath);
}
/**
* Set the Buffered Image
*/
@Override
public void setImage(BufferedImage inimg) {
// TODO set image
image.setImage(inimg);
}
/**
* Return the image object
*/
@Override
public Image getImage(){
return image;
}
/**
* Get the Buffered Image
* @return
*/
public BufferedImage getBufferedImage() {
// TODO return image
return image.getImage();
}
/**
* Return byte array of image
* @return
*/
public byte[] getBimage() {
// TODO return image bytes
return bimage;
}
/**
* Set the buffered image from a byte array
* @param bi
*/
public void setByteArrayImage(byte[] bi) {
// TODO set bimage
bimage = bi;
createFromBytes();
}
/**
* Set Image from Image Path
* @param ip
*/
public void setImage(String imagepath) {
// TODO set image path and image from string
createFromImagePath(imagepath);
}
/**
* Set image path
*/
@Override
public void setPath(String inpath){
image.setPath(inpath);
}
/**
* Return the Image Path
* @return
*/
@Override
public String getPath() {
// TODO return image path
return image.getPath();
}
/**
* Create Buffered Image from Path
* @param inpath
*/
private void createFromImagePath(String inpath){
// TODO create image from path
Pattern p=Pattern.compile("(?mi)(\\.jpg|\\.jpeg|\\.gif|\\.bmp)");
Matcher m=p.matcher(inpath);
if(m.find()){
image.setPath(inpath);
try {
image.setImage(ImageIO.read(new File(image.getPath())));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try{
throw new FileTypeException("Image must be a jpg, gif, bmp, or jpeg");
}catch(FileTypeException e){
e.printStackTrace();
}
}
}
/**
* Create Image from byte array
*/
private void createFromBytes() {
// TODO set img from byte array using byte stream
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(bimage);
image.setImage(ImageIO.read(bis));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Private method that performs the declutter
*/
private void Declutter() {
// TODO perform decluttering using statistics
BufferedImage proxyimage=image.getImage();
// ArrayList of Widths for IQR
ArrayList<Double> arrs = new ArrayList<Double>();
// iterate down the image getting the different widths
int width = proxyimage.getWidth();
int height = proxyimage.getHeight();
// boolean for detecting width of edges
Boolean cswitch = false;
// width ints
int averagewidth = 0;
int start = -1;
int finds = 0;
Color c = null;
// collect cursory statistics
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
c = new Color(proxyimage.getRGB(x, y));
if (cswitch == false & start == -1
& ((c.getRed() + c.getBlue() + c.getGreen()) / 3) > 50) {
start = x;
cswitch = true;
} else if (cswitch == true & start > -1) {
finds++;
averagewidth += (x - start);
arrs.add(((x - start) * 1.0));
cswitch = false;
start = -1;
if ((x + 1) < width) {
c = new Color(proxyimage.getRGB((x + 1), y));
}
// get to the other side of the edge
while ((x + 1) < width
& ((c.getRed() + c.getBlue() + c.getGreen()) / 3) > 50) {
x++;
}
}
}
}
// perform IQR on the integer list
// IQR iqr = new IQR(arrs);
// perform the declutter (any part of a line that is an outlier is
// eradicated unless it is attached to an object that
// is not an outlier
// this is used for images already having had edge detection performed
// on them
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
}
}
}
/**
* Save an image
*/
@Override
public void save() {
// TODO save the image if the path and image are set
image.save();
}
/**
* Delete an Image
*/
@Override
public void delete(){
image.delete();
}
}