我想自己实现Sobel Filter(实际上并没有很好的实现)。但在进行卷积后,我不知道如何计算rgb值。
假设:灰度图像
double [][] sobel_x =
{
{ -1, 0, 1},
{ -2, 0, 2},
{ -1, 0, 1}
};
double [][] sobel_y =
{
{ 1, 2, 1},
{ 0, 0, 0},
{-1, -2, 1}
};
for(int y=1; y<image.getHeight()-1; y++)
{
for(int x=1; x<image.getWidth()-1; x++)
{
Color a = new Color(image.getRGB(x-1, y-1));
Color b = new Color(image.getRGB(x, y-1));
Color c = new Color(image.getRGB(x+1, y-1));
Color d = new Color(image.getRGB(x-1, y));
Color e = new Color(image.getRGB(x, y));
Color f = new Color(image.getRGB(x+1, y));
Color g = new Color(image.getRGB(x-1, y+1));
Color h = new Color(image.getRGB(x, y+1));
Color i = new Color(image.getRGB(x+1, y+1));
double pixel_x = (sobel_x[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
(sobel_x[1][0] * d.getRed()) + (sobel_x[1][1] * e.getRed()) + (sobel_x[1][2] * f.getRed()) +
(sobel_x[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());
double pixel_y =
(sobel_y[0][0] * a.getRed()) + (sobel_x[0][1] * b.getRed()) + (sobel_x[0][2] * c.getRed()) +
(sobel_y[1][0] * d.getRed()) + (sobel_x[1][1] * e.getRed()) + (sobel_x[1][2] * f.getRed()) +
(sobel_y[2][0] * g.getRed()) + (sobel_x[2][1] * h.getRed()) + (sobel_x[2][2] * i.getRed());
//Here it is possible to get values between [-1020, 1020]
//How to going on
//int rgb = (int) Math.sqrt(pixel_x*pixel_x+pixel_y*pixel_y);
//int rgbAsInt = (int)(65536 * rgb + 256 * rgb + rgb);
}
}
答案 0 :(得分:0)
我的一个想法是进行线性转换。例如,你得到的图像中的最小像素值是-998,最大值是1000.所以你可以对应-998到0和1000到255,然后得到(-998,1000)的比例与比例为(0,255)并将[-998,1000]到[0,255]之间的所有值标准化。
答案 1 :(得分:0)
以下图像区域的x轴梯度为1:
1 2 3
1 2 3
1 2 3
将此过滤器应用于它 -
-1 0 1
-2 0 2
-1 0 1
- 得到8的结果。因此,X和Y渐变按该因子缩放。
您需要确定要在输出图像中表示的最大渐变量;称之为&#34; gr_max&#34;。应将X和Y渐变钳位到该值:
float gr_x, gr_y, gr_max = 16;
gr_x /= (gr_max * 8);
gr_y /= (gr_max * 8);
if (gr_x > 1)
gr_x = 1;
if (gr_x < -1)
gr_x = -1;
if (gr_y > 1)
gr_y = 1;
if (gr_y < -1)
gr_y = -1;
然后,假设您希望输出RGB值在[0,255] -
范围内int pixel_x = lround((gr_x + 1) * 255/2),
pixel_y = lround((gr_y + 1) * 255/2);