Project_1的part1搞出来之后,很有成就感,后面三个就不想搞了...但还是简单搞了搞。
PixImage的3D矩阵来存每个像素块中的三原色。
最好在class一开始就set三个final constant分别是red、green和blue来增强code的readability。
public final static int RED = 0;
public final static int GREEN = 1;
public final static int BLUE = 2;
boxBlur的要考虑number of iteration,这个用if else就能处理
if (numIterations <= 0) {
return this;
} else {
// some operation code, processing by one time.
numIterations--;
boxBlurPixImage = boxBlurPixImage.boxBlur(numIterations);
}
用这个Recursive的思想处理就非常简单。
除此之外,核心算法是考虑边界情况,这里用两个for循环,遍历该元素的周围9个邻居,有邻居在界外就忽略即可。这样比自己去强行考虑各种边界问题要简单的多。
int neighborCount = 0;
int neighborValue = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i < 0 || i >= width) {
break;
}
if (j >= 0 && j < height) {
neighborCount++;
neighborValue = neighborValue + this.pixelMatrix[i][j][color];
}
}
}
return (neighborValue / neighborCount);
sobelEdge的话,核心算法是在如何找到边界元素的reflection。
简单方法是用四个if,首先遍历某元素所有的邻居,然后将出边界的“邻居”都移回该值存在的真正位置。比考虑各种情况也简单不少。
注意coner元素的对角邻居是它本身。readme里面虽然没说,但,只能如此。
// some codes
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
int indexI = i;
int indexJ = j;
if (indexI < 0) {
indexI++;
}
if (indexJ < 0) {
indexJ++;
}
if (indexI >= width) {
indexI--;
}
if (indexJ >= height) {
indexJ--;
}
// some codes
}
}
然后计算gx与gy,可以把gx和gy之前的gradient矩阵存在一个1D的数组里,这样方便调用。
计算时别忘了cast。
关于运行:我用cmd和eclipse都试了。cmd的话,得按照win的语法习惯,把:改成;。
eclipse的话,改动一下blur和sobel的main函数即可。然后把照片放进eclipse的project中,运行。
改动如下:
Blur的main函数之前各种判断可以都不要,只留最后一句。
blurFile("highcontrast.tiff", 10);
String[] argsMe = {"highcontrast.tiff", "5"};
args = argsMe;
Iterator我直接移植了DList2,带Sentinel的circular List。
每个node里存俩data,runLength和runIntensity。
要三条lists,分别存RGB。
RLE的话,各种读各种写就行。
最后一个Part看了下,确实没心思做了,而且时间赶,以后有空再说(估计不会再说了)...