gpt4 book ai didi

java - 在 ImageJ 中实现 Sobel 过滤器

转载 作者:行者123 更新时间:2023-12-01 17:20:12 26 4
gpt4 key购买 nike

您好,我尝试通过创建自己的插件在 imagej 中实现 Sobel 过滤器。我有有效的代码,但是它没有正确突出显示白色边缘。我已经尝试了一段时间了,但我只是不知道我做错了什么,或者我可能不完全理解索贝尔滤波器的植入,或者不理解数学。任何帮助将不胜感激。这是我的代码:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;

public class Filter_Plugin implements PlugInFilter {
String title = null;

int sobel_x[][] = {{-1,0,1},
{-2,0,2},
{-1,0,1}};

int sobel_y[][] = {{-1,-2,-1},
{0,0,0},
{1,2,1}};

int pixel_x;
int pixel_y;

public int setup(String arg, ImagePlus im) {
title = im.getTitle();
return DOES_8G;
}

public void run(ImageProcessor ip) {

int w = ip.getWidth();
int h = ip.getHeight();
ImageProcessor copy = ip.duplicate();

for (int x=1; x < w-2; x++)
{
for (int y=1; y < h-2; y++)
{
pixel_x = 1/6 * (sobel_x[0][0] * copy.getPixel(x-1,y-1)) + (sobel_x[0][1] * copy.getPixel(x,y-1)) + (sobel_x[0][2] * copy.getPixel(x+1,y-1)) +
(sobel_x[1][0] * copy.getPixel(x-1,y)) + (sobel_x[1][1] * copy.getPixel(x,y)) + (sobel_x[1][2] * copy.getPixel(x+1,y)) +
(sobel_x[2][0] * copy.getPixel(x-1,y+1)) + (sobel_x[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

pixel_y = 1/6 * (sobel_y[0][0] * copy.getPixel(x-1,y-1)) + (sobel_y[0][1] * copy.getPixel(x,y-1)) + (sobel_y[0][2] * copy.getPixel(x+1,y-1)) +
(sobel_y[1][0] * copy.getPixel(x-1,y)) + (sobel_y[1][1] * copy.getPixel(x,y)) + (sobel_y[1][2] * copy.getPixel(x+1,y)) +
(sobel_y[2][0] * copy.getPixel(x-1,y+1)) + (sobel_y[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

int val = (int)Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y));

if(val < 0)
{
val = 0;
}

if(val > 255)
{
val = 255;
}

ip.putPixel(x,y,val);

}
}
}

}

最佳答案

您的计算中有一个错误:您仅将第一个被加数(即矩阵的左上角)乘以 1/6。通过消除这个因素,我得到了以下适合我的代码:

import ij.*;
import ij.process.*;
import ij.gui.*;
import java.util.*;
import java.awt.*;
import ij.plugin.filter.*;
import ij.process.*;
import java.lang.Math.*;

public class Filter_Plugin implements PlugInFilter {
String title = null;
int sobel_x[][] = {{-1,0,1},
{-2,0,2},
{-1,0,1}};
int sobel_y[][] = {{-1,-2,-1},
{0,0,0},
{1,2,1}};
int pixel_x;
int pixel_y;

public int setup(String arg, ImagePlus im) {
title = im.getTitle();
return DOES_8G;
}

public void run(ImageProcessor ip) {
int w = ip.getWidth();
int h = ip.getHeight();
ImageProcessor copy = ip.duplicate();
for (int x=1; x < w-2; x++) {
for (int y=1; y < h-2; y++) {
pixel_x = (sobel_x[0][0] * copy.getPixel(x-1,y-1)) + (sobel_x[0][1] * copy.getPixel(x,y-1)) + (sobel_x[0][2] * copy.getPixel(x+1,y-1)) +
(sobel_x[1][0] * copy.getPixel(x-1,y)) + (sobel_x[1][1] * copy.getPixel(x,y)) + (sobel_x[1][2] * copy.getPixel(x+1,y)) +
(sobel_x[2][0] * copy.getPixel(x-1,y+1)) + (sobel_x[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));
pixel_y = (sobel_y[0][0] * copy.getPixel(x-1,y-1)) + (sobel_y[0][1] * copy.getPixel(x,y-1)) + (sobel_y[0][2] * copy.getPixel(x+1,y-1)) +
(sobel_y[1][0] * copy.getPixel(x-1,y)) + (sobel_y[1][1] * copy.getPixel(x,y)) + (sobel_y[1][2] * copy.getPixel(x+1,y)) +
(sobel_y[2][0] * copy.getPixel(x-1,y+1)) + (sobel_y[2][1] * copy.getPixel(x,y+1)) + (sobel_x[2][2] * copy.getPixel(x+1,y+1));

int val = (int)Math.sqrt((pixel_x * pixel_x) + (pixel_y * pixel_y));

if(val < 0)
{
val = 0;
}

if(val > 255)
{
val = 255;
}

ip.putPixel(x,y,val);
}
}
}
}

你看过Stephan Preibisch's Sobel Filter Plugin吗?作为引用?

也许您应该将输出设为 32 位浮点图像,而不是截断 0 和 255 处的值。

关于java - 在 ImageJ 中实现 Sobel 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19331515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com