中级农民
- 积分
- 112
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-6-13
- 最后登录
- 1970-1-1
|
本帖最后由 小亩_0718 于 2020-6-8 01:27 编辑
Project1 - Part 1 _ PixImage.java
public class PixImage {
//instance variables
Pixel [][] image;
private final int width;
private final int height;
//static variables
private static final int[][] gxCons={{1,0,-1},{2,0,-2},{1,0,-1}};
private static final int[][] gyCons={{1,2,1},{0,0,0},{-1,-2,-1}};
//constructor
public PixImage(int width, int height) {
this.image = new Pixel [width][height];
for (int i = 0; i<width; i++){
for (int j = 0; j<height; j++){
image[j]=new Pixel();
}
}
this.width=width;
this.height=height;
}
//getter & setter methods
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public short getRed(int x, int y) {
return this.image[x][y].red;
}
public short getGreen(int x, int y) {
return this.image[x][y].green;
}
public short getBlue(int x, int y) {
return this.image[x][y].blue;
}
public void setPixel(int x, int y, short red, short green, short blue) {
this.image[x][y].red = red;
this.image[x][y].green = green;
this.image[x][y].blue = blue;
}
//boxBlur method
- public PixImage boxBlur(int numIterations) {
- if (this.width==0||this.height==0){
- return this;
- }
- PixImage source = this;
- for(int k=0;k<numIterations;k++){
- PixImage target = new PixImage(this.width,this.height);
- for(int i=0;i<source.width;i++){
- for(int j=0; j<source.height;j++){
- short r=source.calculateBlurRed(i,j);
- short g=source.calculateBlurGreen(i,j);
- short b=source.calculateBlurBlue(i,j);
- target.setPixel(i,j,r,g,b);
- }
- }
- source=target;
- }
- return source;
- }
-
- private short calculateBlurRed(int x, int y) { //calling obj is PixelImage
- short sum = 0, count = 0;
- for(int i=x-1;i<=x+1;i++){
- for(int j=y-1;j<=y+1;j++){
- if(i>=0 && i<this.width && j>=0 && j < this.height) {
- count++;
- sum+=this.getRed(i,j);
- }
- }
- }
- return (short)(sum/count);
- }
- private short calculateBlurGreen(int x, int y) {
- short sum = 0, count = 0;
- for(int i=x-1;i<=x+1;i++){
- for(int j=y-1;j<=y+1;j++){
- if(i>=0 && i<this.width && j>=0 && j < this.height) {
- count++;
- sum+=this.getGreen(i,j);
- }
- }
- }
- return (short)(sum/count);
- }
- private short calculateBlurBlue(int x, int y) {
- short sum = 0, count = 0;
- for(int i=x-1;i<=x+1;i++){
- for(int j=y-1;j<=y+1;j++){
- if(i>=0 && i<this.width && j>=0 && j < this.height) {
- count++;
- sum+=this.getBlue(i,j);
- }
- }
- }
- return (short)(sum/count);
- }
复制代码
//mag2Gray method
- private static short mag2gray(long mag) {
- short intensity = (short) (30.0 * Math.log(1.0 + (double) mag) - 256.0);
- // Make sure the returned intensity is in the range 0...255, regardless of
- // the input value.
- if (intensity < 0) {
- intensity = 0;
- } else if (intensity > 255) {
- intensity = 255;
- }
- return intensity;
- }
复制代码
//sobelEdges metho
- public PixImage sobelEdges() {
- PixImage copy = new PixImage(this.width,this.height);
- //loop every pixel,
- for (int i = 0; i<this.width; i++){
- for (int j = 0; j<this.height; j++){
- //set R,G,B value on pixel
- short value=mag2gray(this.calculateEnergy(i,j));
- copy.setPixel(i,j,value,value,value);
- }
- }
- return copy;
- }
复制代码
//calculateEnergy method
- private long calculateEnergy(int x, int y){
- long gxSumRed=0,gySumRed=0;
- long gxSumGreen=0,gySumGreen=0;
- long gxSumBlue=0,gySumBlue=0;
- for(int i = 0; i<3; i++){
- for(int j = 0; j<3;j++){
- int m=i+(x-1),n=j+(y-1);
- if(m<0){ //if m is invalid, let m equals to 0
- m=0;
- }else if(m>=this.width){
- m=this.width-1;
- }
- if(n<0){ //if n is invalid, let n equals to 0
- n=0;
- }else if(n>=this.height){
- n=this.height-1;
- }
- int red=this.image[m][n].red;
- int green=this.image[m][n].green;
- int blue=this.image[m][n].blue;
- gxSumRed+=gxCons[i][j]*red;
- gySumRed+=gyCons[i][j]*red;
- gxSumGreen+=gxCons[i][j]*green;
- gySumGreen+=gyCons[i][j]*green;
-
- gxSumBlue+=gxCons[i][j]*blue;
- gySumBlue+=gyCons[i][j]*blue;
- }
- }
- long energy = 0;
- energy += gxSumRed * gxSumRed;
- energy += gySumRed * gySumRed;
- energy += gxSumGreen * gxSumGreen;
- energy += gySumGreen * gySumGreen;
- energy += gxSumBlue * gxSumBlue;
- energy += gySumBlue * gySumBlue;
-
- return energy;
- }
复制代码
[/i][/i][/i][/i][/i][/i][i][i]
---------------------------------------------Pixel.java----------
- public class Pixel {
- short red;
- short green;
- short blue;
- }
复制代码
[/i][/i]
|
|