Convolve3 – Sobel Operator


convolution mask

  1   2   1
  0   0   0
 -1  -2  -1

scale = 8

In bipolar mode 1, above, positive edge values are represented in red and negative values in blue.

In the code below, you can select either an x-axis edge operator or the y-axis edge operator. As shown, the y-axis is selected.

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

See Convolve.java, which does the actual work.


Convolve3.java

import java.awt.image.*;
import java.awt.color.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.JFrame;


public class Convolve3 {
    public static void main(String [] args) {

	String filename = "blob.png";
	if (args.length>0) filename = args[0];
	int style = 1;
	if (args.length>1) style = Integer.parseInt(args[1]);
	int scl = 8;
	if (args.length>2) scl = Integer.parseInt(args[2]);

	ImagePanel p1 = new ImagePanel(filename);
	int width = p1.img.getWidth();
	int height = p1.img.getHeight();
	if (width<64) p1.setMag(4);
	p1.info();
	ImageFrame f1 = new ImageFrame(p1);
	f1.setLocation(10,10);
	f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	//if (width<25) listImage.listGray(f1.img,filename);

	int w = f1.getWidth();
	int h = f1.getHeight();

	//int mask[][] = { {-1, 0, 1},{-2, 0, 2},{-1, 0, 1} };
	int mask[][] = {{1,2,1},{0, 0, 0},{-1,-2,-1}};
	Convolve c = new Convolve(mask);
	c.setScale(scl);
	c.setBipolarStyle(style);
	c.showMatrix(mask,"convolution mask");
	BufferedImage outp = c.doConvolve(p1.img);
	ImagePanel p2 = new ImagePanel(outp,"bipolar");
	if (width<64) p2.setMag(4);
	ImageFrame f2 = new ImageFrame(p2);
	f2.setLocation(20+w,10);
	f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	//p2.writeImage("out3.png");
	//if (width<25 && style==0) listImage.listGray(f2.img,"filtered image");		
    }
}


Maintained by John Loomis, updated Wed Mar 18 16:08:08 2020