Find gradient magnitude and direction of 2-D image (2024)

Find gradient magnitude and direction of 2-D image

collapse all in page

Syntax

[Gmag,Gdir]= imgradient(I)

[Gmag,Gdir]= imgradient(I,method)

[Gmag,Gdir]= imgradient(Gx,Gy)

Description

[Gmag,Gdir]= imgradient(I) returns the gradient magnitude, Gmag, and the gradient direction, Gdir, of the 2-D grayscale or binary image I.

example

[Gmag,Gdir]= imgradient(I,method) returns the gradient magnitude and direction using the specified method.

example

[Gmag,Gdir]= imgradient(Gx,Gy) returns the gradient magnitude and direction from the directional gradients Gx and Gy in the x and y directions, respectively.

Examples

collapse all

Calculate Gradient Magnitude and Direction Using Prewitt Method

Open Live Script

Read an image into workspace.

I = imread("coins.png");

Calculate the gradient magnitude and direction, specifying the Prewitt gradient operator.

[Gmag,Gdir] = imgradient(I,"prewitt");

Display the gradient magnitude and direction.

imshowpair(Gmag,Gdir,"montage");title("Gradient Magnitude (Left) and Gradient Direction (Right) Using Prewitt Method")

Find gradient magnitude and direction of 2-D image (1)

Calculate Gradient Magnitude and Direction Using Directional Gradients

Open Live Script

Read an image into workspace.

I = imread('coins.png');

Calculate the x- and y-directional gradients. By default, imgradientxy uses the Sobel gradient operator.

[Gx,Gy] = imgradientxy(I);

Display the directional gradients.

imshowpair(Gx,Gy,'montage')title('Directional Gradients Gx and Gy, Using Sobel Method')

Find gradient magnitude and direction of 2-D image (2)

Calculate the gradient magnitude and direction using the directional gradients.

[Gmag,Gdir] = imgradient(Gx,Gy);

Display the gradient magnitude and direction.

imshowpair(Gmag,Gdir,'montage')title('Gradient Magnitude (Left) and Gradient Direction (Right)')

Find gradient magnitude and direction of 2-D image (3)

Input Arguments

collapse all

IInput image
2-D grayscale image | 2-D binary image

Input image, specified as a 2-D grayscale or 2-D binary image.

Data Types: single | double | int8 | int32 | uint8 | uint16 | uint32 | logical

methodGradient operator
"sobel" (default) | "prewitt" | "central" | "intermediate" | "roberts"

Gradient operator, specified as one of the following values.

MethodDescription
"sobel"

Sobel gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3 neighborhood. For gradients in the vertical (y) direction, the weights are:

[ 1 2 1 0 0 0 -1 -2 -1 ]
In the x direction, the weights are transposed.
"prewitt"

Prewitt gradient operator. The gradient of a pixel is a weighted sum of pixels in the 3-by-3 neighborhood. For gradients in the vertical (y) direction, the weights are:

[ 1 1 1 0 0 0 -1 -1 -1 ]
In the x direction, the weights are transposed.
"central"

Central difference gradient. The gradient of a pixel is a weighted difference of neighboring pixels. In the y direction, dI/dy = (I(y+1) - I(y-1))/2.

"intermediate"

Intermediate difference gradient. The gradient of a pixel is the difference between an adjacent pixel and the current pixel. In the y direction, dI/dy = I(y+1) - I(y).

"roberts"

Roberts gradient operator. The gradient of a pixel is the difference between diagonally adjacent pixels. For gradients in one direction, the weights are:

[ 1 0 0 -1 ]
In the orthogonal direction, the weights are flipped along the vertical axis.

Data Types: char | string

GxHorizontal gradient
numeric matrix

Horizontal gradient, specified as a numeric matrix. The horizontal (x) axis points in the direction of increasing column subscripts. You can use the imgradientxy function to calculate Gx.

Data Types: single | double | int8 | int32 | uint8 | uint16 | uint32

GyVertical gradient
numeric matrix

Vertical gradient, specified as a numeric matrix of the same size as Gx. The vertical (y) axis points in the direction of increasing row subscripts. You can use the imgradientxy function to calculate Gy.

Data Types: single | double | int8 | int32 | uint8 | uint16 | uint32

Output Arguments

collapse all

Gmag — Gradient magnitude
numeric matrix

Gradient magnitude, returned as a numeric matrix of the same size as image I or the directional gradients Gx and Gy. Gmag is of class double, unless the input image or directional gradients are of data type single, in which case it is of data type single.

Data Types: double | single

Gdir — Gradient direction
numeric matrix

Gradient direction, returned as a numeric matrix of the same size as gradient magnitude Gmag. Gdir contains angles in degrees within the range [-180, 180] measured counterclockwise from the positive x-axis. (The x-axis points in the direction of increasing column subscripts.) Gdir is of class double, unless the input image I or directional gradients are of data type single, in which case it is of data type single.

Data Types: double | single

Tips

  • When applying the gradient operator at the boundaries of the image, values outside the bounds of the image are assumed to equal the nearest image border value. This is similar to the "replicate" boundary option in imfilter.

Algorithms

The algorithmic approach taken in imgradient for each of the listed gradient methods is to first compute directional gradients, Gx and Gy, in the x and y directions, respectively. The horizontal (x) axis points in the direction of increasing column subscripts. The vertical (y) axis points in the direction of increasing row subscripts. The gradient magnitude and direction are then computed from their orthogonal components Gx and Gy.

imgradient does not normalize the gradient output. If the range of the gradient output image has to match the range of the input image, consider normalizing the gradient image, depending on the method argument used. For example, with a Sobel kernel, the normalization factor is 1/8, for Prewitt, it is 1/6, and for Roberts it is 1/2.

Extended Capabilities

Version History

Introduced in R2012b

expand all

imgradient now supports thread-based environments.

imgradient now supports the generation of C code (requires MATLAB Coder).

See Also

imgradientxy | imgradientxyz | imgradient3 | edge | fspecial

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Find gradient magnitude and direction of 2-D image (4)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Find gradient magnitude and direction of 2-D image (2024)
Top Articles
TamilMV Proxy: Unblock List and Its Alternatives Sites - Moviden
EXCLUSIVE: "Love me or hate me but you can’t ignore me," says Shilpa Shetty | Filmfare.com
Benchmark Physical Therapy Jobs
Citi Trends Watches
Jazmen00 Mega
What Is a Megapixel: Essential Guide [Megapixels Explained]
Indio Mall Eye Doctor
Behind the Screens: Understanding the Wisconsin Volleyball Team Leak
Umass Medhub
Terry Gebhardt Obituary
New Orleans Pelicans News, Scores, Status, Schedule - NBA
24 Hour Lock Up Knoxville Tn
Clarita Amish Auction 2023
His Words Any Sense Figgerits
Nyc Peep Show 2022
Mychart.solutionhealth.org/Mychartprd/Billing/Summary
Entegra Forum
Ellaeats Tumblr
โลโก้โภชนาการที่ดีที่สุด: สัญลักษณ์แห่งความเป็นเลิศ
Traveltalkonline
Nutrislice White Bear Lake
10 Teacher Tips to Encourage Self-Awareness in Teens | EVERFI
Atl To London Google Flights
12 Week Glute Program to Transform Your Booty with Free PDF - The Fitness Phantom
Fly Fit Bungee Rome Ga
When Is Lana Rhoades’ Baby Due Date? Baby Daddy, Bump, And More
Gestalt psychology | Definition, Founder, Principles, & Examples
Forest Haven Asylum Stabbing 2017
360 Training Food Handlers Final Exam Answers 2022
MovieHaX.Click
Bilt Rent Day Challenge June 2023 Answers
Missoula Jail Releases
My Les Paul Forum
Vegamovies Home
Cornerstone Okta T Mobile
Ucla Course Schedule
Deborah Clearbranch Psychologist Georgia
Scarabaeidae), with a key to related species – Revista Mexicana de Biodiversidad
Nahant Magic Seaweed
Lagniappemobile
Latest News & Breaking News Coverage | Flipboard
600 Aviator Court Vandalia Oh 45377
Appsanywhere Mst
Ces 2023 Badge Pickup
German American Bank Owenton Ky
Black Panther Pitbull Puppy For Sale
Kingdom Tattoo Ithaca Mi
Loredana Chivu, despre operațiile făcute la clinica anchetată: "Am fost la un pas de moarte"
When His Eyes Opened Chapter 191
Shaver Lake Webcam Gas Station
Santa Rosa Craigslist Free Stuff
Ericdoa Ethnicity
Latest Posts
Article information

Author: Trent Wehner

Last Updated:

Views: 5897

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Trent Wehner

Birthday: 1993-03-14

Address: 872 Kevin Squares, New Codyville, AK 01785-0416

Phone: +18698800304764

Job: Senior Farming Developer

Hobby: Paintball, Calligraphy, Hunting, Flying disc, Lapidary, Rafting, Inline skating

Introduction: My name is Trent Wehner, I am a talented, brainy, zealous, light, funny, gleaming, attractive person who loves writing and wants to share my knowledge and understanding with you.