Recently I am preparing for Low Poly
like this:
My first thing is to pick up some points of the input image.I want to try Sobel
for detection and I have read soble article.
Now I meet problem where I don't know how to implement sobel filter.Folowing is my try:
First -> Get gray data from input image
try {
mImage = ImageIO.read(new File("D:\\Documents\\Pictures\\engine.png"));
} catch (IOException e) {
e.printStackTrace();
}
mWidth = mImage.getWidth();
mHeight = mImage.getHeight();
mNewImage = new BufferedImage(mWidth, mHeight, mImage.getType());
mGrayData = new int[mWidth * mHeight];
// get pixel data from image
for (int i = 0; i < mHeight; i++) {
for (int j = 0; j < mWidth; j++) {
int rgb = mImage.getRGB(j, i);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
int grayLevel = (r + g + b) / 3;
mGrayData[i * mWidth + j] = grayLevel;
}
}
Then -> Calculate gradient of every point
private int[] getGradient() {
int[] gradient = new int[mWidth * mHeight];
for (int x=1;x<mWidth-1;x++){
for (int y=1;y<mHeight-1;y++){
int grayX = getGrayPoint(x+1,y-1)+2*getGrayPoint(x+1,y)+getGrayPoint(x+1,y+1)-
(getGrayPoint(x-1,y-1)+2*getGrayPoint(x-1,y)+getGrayPoint(x-1,y+1));
int grayY = (getGrayPoint(x-1, y+1) + 2*getGrayPoint(x,y+1)+getGrayPoint(x+1,y+1))-
(getGrayPoint(x-1,y-1) + 2*getGrayPoint(x,y-1) + getGrayPoint(x+1,y-1));
gradient[x+y*mWidth] = (int) Math.sqrt(grayX*grayX+grayY*grayY);
}
}
return gradient;
}
Last -> Create new image with above gradient
private void createImage() {
int[] gradient = getGradient();
for (int y = 1; y < mHeight - 1; ++y)
for (int x = 1; x < mWidth - 1; ++x)
mNewImage.setRGB(x,y,gradient[y * mWidth + x]);
File file = new File("D:\\Documents\\Pictures\\engine3.png");
if (!file.exists()) {
file.getParentFile().mkdir();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
ImageIO.write(mNewImage, "png", file);
} catch (IOException e) {
e.printStackTrace();
}
}
Original image
What it should look like?
What I got
It's so strange that the color is a little blue
Edit 1:
Now I get:
why is it so black?