我正在尝试申请
[-1][-1][-1]
[-1][8][-1]
[-1][-1][-1]
3 * 3拉普拉斯滤镜用于此处著名照片的灰度版本(png扩展名)。
我主要是使用BufferedImage
类来处理图像。这是拉普拉斯滤波法。
private BufferedImage measureContrast(BufferedImage image) {
BufferedImage grayScale = createGrayscaleImage(image);
BufferedImage copy = copyImage(grayScale);
int width = image.getWidth();
int height = image.getHeight();
int sum=0;
int a;
//3*3 Laplacian filter (-1,-1,-1), (-1,8,-1), (-1,-1,-1)
for(int y=1;y<height-1;y++)
for(int x=1;x<width-1;x++) {
sum = (-1*(grayScale.getRGB(x-1, y-1)&0xff)) + (-1*(grayScale.getRGB(x, y-1)&0xff)) + (-1*(grayScale.getRGB(x+1, y-1)&0xff))
+ (-1*(grayScale.getRGB(x-1, y)&0xff)) + (8*(grayScale.getRGB(x,y)&0xff)) + (-1*(grayScale.getRGB(x+1, y)&0xff)) +
(-1*(grayScale.getRGB(x-1, y+1)&0xff)) + (-1*(grayScale.getRGB(x, y+1)&0xff)) + (-1*(grayScale.getRGB(x+1, y+1)&0xff));
a = ((grayScale.getRGB(x, y)>>24)&0xff);
copy.setRGB(x, y, ((a<<24)|(sum<<16)|(sum<<8)|(sum)));
}
return copy;
如果我运行该代码,结果就是这样
这显然是错误的。图像中突然出现粗线。
我可以确保灰度版本是正确的,因为当我在应用滤镜之前仅运行以下代码时,输出将提供完美的灰度图像。
private BufferedImage measureContrast(BufferedImage image) {
BufferedImage grayScale = createGrayscaleImage(image);
BufferedImage copy = copyImage(grayScale); /*rest of the code is commented*/
return copy;
我已经尝试寻找问题了几个小时,但我认为代码没有任何问题……任何见解将不胜感激。预先感谢!
为了复制图像,我使用了以下代码。我从堆栈溢出的选定答案中借用了它,因此我认为这不会错。
BufferedImage copyImage(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
此外,结果图像也这样打印
BufferedImage Contrast = measureContrast(image);
//write image
try {
ImageIO.write(Contrast, "png", new File(outputPath));
System.out.println("Printing complete");
}catch(IOException e) {
System.out.println("File Printing Error: "+e);
}
以防万一,这是灰度图像生成方法。
private BufferedImage createGrayscaleImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage copy = copyImage(image);
int p=0, a=0, r=0, g=0, b=0, avg=0;
for(int y=0;y<height;y++)
for(int x=0;x<width;x++) {
p=image.getRGB(x, y);
a=(p>>24)&0xff;
r=(p>>16)&0xff;
g=(p>>8)&0xff;
b=p&0xff;
avg = (r+g+b)/3;
p = (a<<24) | (avg<<16) | (avg<<8) | avg;
copy.setRGB(x, y, p);
}
return copy;
}
答案 0 :(得分:0)
with open(filename,'r') as csvfile:
csvFileReader = csv.reader(csvfile)
for row in csvFileReader:
dates.append(int(row[0]))
prices.append(float(row[1]))
此方法是错误的。我应该考虑总和为负值的时间。因此,我在嵌套的for循环中添加了private BufferedImage measureContrast(BufferedImage image) {
BufferedImage grayScale = createGrayscaleImage(image);
BufferedImage copy = copyImage(grayScale);
int width = image.getWidth();
int height = image.getHeight();
int sum=0;
int a;
//3*3 Laplacian filter (-1,-1,-1), (-1,8,-1), (-1,-1,-1)
for(int y=1;y<height-1;y++)
for(int x=1;x<width-1;x++) {
sum = (-1*(grayScale.getRGB(x-1, y-1)&0xff)) + (-1*(grayScale.getRGB(x, y-1)&0xff)) + (-1*(grayScale.getRGB(x+1, y-1)&0xff))
+ (-1*(grayScale.getRGB(x-1, y)&0xff)) + (8*(grayScale.getRGB(x,y)&0xff)) + (-1*(grayScale.getRGB(x+1, y)&0xff)) +
(-1*(grayScale.getRGB(x-1, y+1)&0xff)) + (-1*(grayScale.getRGB(x, y+1)&0xff)) + (-1*(grayScale.getRGB(x+1, y+1)&0xff));
a = ((grayScale.getRGB(x, y)>>24)&0xff);
copy.setRGB(x, y, ((a<<24)|(sum<<16)|(sum<<8)|(sum)));
}
return copy;
,以确保在这种情况下像素值为0。