First-order differential operators of images

Source: Internet
Author: User
Overview

The first order of the binary function f (x, y) is FX = sort f/sort X or FX = sort f/sort Y. The coordinates of the image are as follows:

Assume that each pixel of an image is:

3 3 3 3 3

3 5 5 5 3

3 5 9 9 5 3

3 5 9 9
5 3

3 5 5 5 3

3 3 3 3 3

Before proceeding, we will first provide two functions for reading and writing images.

Algorithm source code 1 (read and write images)
/*** Read image ** @ Param srcpath image storage location * @ return returns the bufferedimage object of the image */public static bufferedimage readimg (string srcpath) {bufferedimage IMG = NULL; try {IMG = ImageIO. read (new file (srcpath);} catch (ioexception e) {e. printstacktrace ();} return IMG ;} /*** write the image to disk * @ Param IMG the bufferedimage object of the image * @ Param formatname the file format stored * @ Param distpath the storage location of the image to be saved */public static void writeimg (bufferedimage IMG, s Tring formatname, string distpath) {outputstream out = NULL; try {// int imgtype = IMG. getType (); // system. out. println ("W:" + IMG. getwidth () + "H:" + IMG. getheight (); out = new fileoutputstream (distpath); ImageIO. write (IMG, formatname, out);} catch (ioexception e) {e. printstacktrace ();} finally {try {If (OUT! = NULL) {out. Close () ;}catch (ioexception e) {e. printstacktrace ();}}}
Horizontal differential operator

The differential operator in the horizontal direction is to obtain the change rate of the image in the horizontal direction.

Definition

G = f (x-1, Y-1) + 2f (x, Y-1) + f (x + 1, Y-1)-f (x-1, Y + 1)-2f (X, Y + 1)-f (x + 1, Y + 1)

Weight Matrix Template

Template d1

Processing result of the differential operator in the horizontal direction:

0 0 0 0 0 0

0-10-20-20-10 0

0-4-12-12-4 0

0 4 12 12 4 0

0 10 20 10 0

0 0 0 0 0 0

To properly display the processing result, add the absolute value of the minimum value to each pixel (as shown above |-20 |). The result is as follows:

20 20 20 20 20

20 10 0 0 10 20

20 16 8 8 16 20

20 24 32 24 20

20 30 40 40 30 20

20 20 20 20 20

Algorithm source code 2
/*** Horizontal differential operator ** @ Param srcpath: storage location of the image * @ Param distpath: storage location of the image to be saved * @ Param formatname: storage location of the image to be saved */public static void firstmomentx (string srcpath, string distpath, string formatname) {bufferedimage IMG = readimg (srcpath); int W = IMG. getwidth (); int H = IMG. getheight (); int pix [] = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); pix = firstmomentx (pix, W, H); IMG. setrgb (0, 0, W, H, pix, 0, W); writ Eimg (IMG, formatname, distpath );} /*** horizontal differential operator ** @ Param pix Pixel matrix array * @ Param W matrix width * @ Param H matrix height * @ return processed array */public static int [] firstmomentx (INT [] pix, int W, int h) {int [] newpix = new int [w * H]; colormodel CM = colormodel. getrgbdefault (); int R, G, B; For (INT y = 0; y Vertical differential operator

The differential operator in the horizontal direction is to obtain the change rate of the image in the horizontal direction.

Definition

G = f (x-1, Y-1) + 2f (x-1, Y) + f (x-1, Y + 1)-f (x + 1, Y-1)-2f (x + 1, y)-f (x + 1, Y + 1)

Weight Matrix Template

Template D2

Processing result of vertical differential operators:

20 20 20 20 20

20 10 16 24 30 20

20 0 8 32 40 20

20 0 8 32 40 20

20 10 16 24 30 20

20 20 20 20 20

Algorithm source code 3
/*** Vertical differential operator ** @ Param srcpath image storage location * @ Param distpath image storage location * @ Param formatname image storage location */public static void firstmomenty (string srcpath, string distpath, string formatname) {bufferedimage IMG = readimg (srcpath); int W = IMG. getwidth (); int H = IMG. getheight (); int pix [] = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); pix = firstmomenty (pix, W, H); IMG. setrgb (0, 0, W, H, pix, 0, W); writ Eimg (IMG, formatname, distpath );} /*** vertical differential operator ** @ Param pix Pixel matrix array * @ Param W matrix width * @ Param H matrix height * @ return processed array */public static int [] firstmomenty (INT [] pix, int W, int h) {int [] newpix = new int [w * H]; colormodel CM = colormodel. getrgbdefault (); int R; For (INT y = 0; y Lab results:

Source image:


Effect of differential operators in the horizontal direction


Effect of vertical Differential Operators

Robert ts cross-Differential Operator

The horizontal and vertical operators only obtain detailed information in a specific direction. However, for most images, it is very important to obtain detailed outlines, most of the details of a scene are irregular, so it is necessary to consider sharpening differentiation in two-dimensional image directions. The role templates of the Robert ts cross-differential operator are as follows:


Definition:

G = | f (x + 1, Y + 1)-f (x, y) | + | f (x, y + 1)-f (x + 1, Y) |

Weight Matrix Template


F1 '= D1 (f (x, y), f2' = d2 (f (x, y ))

G = | F1 '| + | f2' |

Algorithm source code 4
/*** Robert ts cross-differential operator * @ Param srcpath image storage location * @ Param distpath storage location of the image to be saved * @ Param formatname storage location of the image to be saved */public static void Robert TS (string srcpath, string distpath, string formatname) {bufferedimage IMG = readimg (srcpath); int W = IMG. getwidth (); int H = IMG. getheight (); int pix [] = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); pix = Roberts (pix, W, H); IMG. setrgb (0, 0, W, H, pix, 0, W); writei Mg (IMG, formatname, distpath );} /*** Robert ts cross-differential operator * @ Param pix Pixel matrix array * @ Param W matrix width * @ Param H matrix height * @ return processed Pixel matrix */Public static int [] Robert TS (INT pix [], int W, int h) {colormodel CM = colormodel. getrgbdefault (); int R; int [] newpix = new int [w * H]; for (INT y = 0; y Effect:

Sobel differential operator

As described above, the Robert ts differential operator can obtain the detailed contour of a scene with a small template and a small amount of computation. However, because the size of the template is an even number, the pixels to be processed cannot be placed in the template center. Sobel differential operator is a full-direction differential operator under an odd number (3*3) template.

Definition:

D1 = f (x-1, Y-1) + 2f (x, Y-1) + f (x + 1, Y-1)-f (x-1, Y + 1)-2f (X, Y + 1)-f (x + 1, Y + 1)

D2 = f (x-1, Y-1) + 2f (x-1, Y) + f (x-1, Y + 1)-f (x + 1, Y-1)-2f (x + 1, y)-f (x + 1, Y + 1)

G = SQRT (d1 ^ 2 + D2 ^ 2)

Algorithm source code 5
/*** Sobel differential operator ** @ Param srcpath image storage location * @ Param distpath storage location * @ Param formatname storage location of the image */public static void sobel (string srcpath, string distpath, string formatname) {bufferedimage IMG = readimg (srcpath); int W = IMG. getwidth (); int H = IMG. getheight (); int pix [] = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); pix = Sobel (pix, W, H); IMG. setrgb (0, 0, W, H, pix, 0, W); writeimg (IMG, Formatname, distpath );} /*** Sobel differential operator ** @ Param pix Pixel matrix array * @ Param W matrix width * @ Param H matrix height * @ return processed Pixel matrix */public static int [] Sobel (INT pix [], int W, int h) {colormodel CM = colormodel. getrgbdefault (); int G1, G2, R; int [] newpix = new int [w * H]; for (INT y = 0; y Effect:

Priwitt differential operator

The priwitt differential operator is similar to the Sobel differential operator. It is defined in an odd number (3*3) template. Only the template coefficients are different.

D1 = f (x-1, Y-1) + f (x, Y-1) + f (x + 1, Y-1)-f (x-1, Y + 1)-f (x, Y + 1)-f (x + 1, Y + 1)

D2 = f (x-1, Y-1) + f (x-1, Y) + f (x-1, Y + 1)-f (x + 1, Y-1)-f (x + 1, y)-f (x + 1, Y + 1)

G = SQRT (d1 ^ 2 + D2 ^ 2)

Algorithm source code 6
/*** Priwitt differential operator * @ Param srcpath image storage location * @ Param distpath storage location of the image to be saved * @ Param formatname storage location of the image to be saved */public static void priwitt (string srcpath, string distpath, string formatname) {bufferedimage IMG = readimg (srcpath); int W = IMG. getwidth (); int H = IMG. getheight (); int pix [] = new int [w * H]; pix = IMG. getrgb (0, 0, W, H, pix, 0, W); pix = priwitt (pix, W, H); IMG. setrgb (0, 0, W, H, pix, 0, W); writeimg (IMG, formatname, distpath );} /*** priwitt differential operator * @ Param pix Pixel matrix array * @ Param W matrix width * @ Param H matrix height * @ return processed Pixel matrix */public static int [] priwitt (INT pix [], int W, int h) {colormodel CM = colormodel. getrgbdefault (); int G1, G2, R; int [] newpix = new int [w * H]; for (INT y = 0; y 

Effect:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.