Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: How to find Horizontal & Vertical Gradients?

Subject: How to find Horizontal & Vertical Gradients?

From: selvi

Date: 05 Jan, 2008 16:40:48

Message: 1 of 5

Hi,
   I want to implement the following Equations in MATLAB to find hori
& Vert Gradient.

DH = | x(i , j-2) + x(i ,,j+2) - 2x(i, j) | + | x(i,,j-1) - x(i,,j
+1) |

DV = | x(i-2, j) + x(i+2,,j) - 2x(i, j) | + | x(i-1, j) - x(i+1,,j)
|

If(DH+35< DV)

   G={ [ x(i,,j-1) + x(i,,j+1) ] /2 } + { [ 2x(i, j) - x(i , j-2)
- x(i ,,j+2) ]/4}

Subject: Re: How to find Horizontal & Vertical Gradients?

From: dvt

Date: 05 Jan, 2008 22:18:12

Message: 2 of 5

selvi wrote:
> Hi,
> I want to implement the following Equations in MATLAB to find hori
> & Vert Gradient.
>
> DH = | x(i , j-2) + x(i ,,j+2) - 2x(i, j) | + | x(i,,j-1) - x(i,,j
> +1) |
>
> DV = | x(i-2, j) + x(i+2,,j) - 2x(i, j) | + | x(i-1, j) - x(i+1,,j)
> |
>
> If(DH+35< DV)
>
> G={ [ x(i,,j-1) + x(i,,j+1) ] /2 } + { [ 2x(i, j) - x(i , j-2)
> - x(i ,,j+2) ]/4}

If I understand your question, try something like the following:

x = rand(10);
Ga = zeros(10); %preallocate
DHa = Ga;
DVa = Ga;

i = 3:8;
j = 3:8;
DH(i,j) = abs( x(i,j-2) + x(i,j+2) - 2*x(i,j) ) +...
     abs( x(i,j-1) - x(i,j+1) );
DV(i,j) =abs( x(i-2,j) + x(i+2,j) - 2*x(i,j) ) +...
     abs( x(i-1,j) - x(i+1,j) );
mask = ((DH+35)< DV);
G(i,j) = ( x(i,j-1) + x(i,j+1) ) /2 +...
     ( 2*x(i,j) - x(i,j-2) - x(i,j+2) )/4;
G = mask .* G;

As Roger pointed out, you should look into the meanings of {}, [], and
() in Matlab.
--
Dave
dvt at psu dot edu

Subject: Re: How to find Horizontal & Vertical Gradients?

From: Selwyna Bas

Date: 06 Jan, 2008 08:49:53

Message: 3 of 5

dvt <dvt+usenet@psu.edu> wrote in message <flovn7
$102o$1@f04n12.cac.psu.edu>...
> selvi wrote:
> > Hi,
> > I want to implement the following Equations in
MATLAB to find hori
> > & Vert Gradient.
> >
> > DH = | x(i , j-2) + x(i ,,j+2) - 2x(i, j) | + | x
(i,,j-1) - x(i,,j
> > +1) |
> >
> > DV = | x(i-2, j) + x(i+2,,j) - 2x(i, j) | + | x(i-1,
j) - x(i+1,,j)
> > |
> >
> > If(DH+35< DV)
> >
> > G={ [ x(i,,j-1) + x(i,,j+1) ] /2 } + { [ 2x(i,
j) - x(i , j-2)
> > - x(i ,,j+2) ]/4}
>
> If I understand your question, try something like the
following:
>
> x = rand(10);
> Ga = zeros(10); %preallocate
> DHa = Ga;
> DVa = Ga;
>
> i = 3:8;
> j = 3:8;
> DH(i,j) = abs( x(i,j-2) + x(i,j+2) - 2*x(i,j) ) +...
> abs( x(i,j-1) - x(i,j+1) );
> DV(i,j) =abs( x(i-2,j) + x(i+2,j) - 2*x(i,j) ) +...
> abs( x(i-1,j) - x(i+1,j) );
> mask = ((DH+35)< DV);
> G(i,j) = ( x(i,j-1) + x(i,j+1) ) /2 +...
> ( 2*x(i,j) - x(i,j-2) - x(i,j+2) )/4;
> G = mask .* G;
>
> As Roger pointed out, you should look into the meanings
of {}, [], and
> () in Matlab.
> --
> Dave
> dvt at psu dot edu

What is the meaning of G = mask .* G;
Also explain whats the neccesity of this?
 x = rand(10);
> Ga = zeros(10); %preallocate
> DHa = Ga;
> DVa = Ga;
 pl explain..
Thanku for your help

Subject: Re: How to find Horizontal & Vertical Gradients?

From: Selwyna Bas

Date: 06 Jan, 2008 17:16:18

Message: 4 of 5

What is the meaning of G = mask .* G;
Also explain whats the neccesity of this?
 x = rand(10);
> Ga = zeros(10); %preallocate
> DHa = Ga;
> DVa = Ga;
 pl explain..
Thanku for your help

Subject: Re: How to find Horizontal & Vertical Gradients?

From: dvt

Date: 07 Jan, 2008 18:23:52

Message: 5 of 5

Selwyna Bas wrote:
> dvt <dvt+usenet@psu.edu> wrote in message <flovn7
> $102o$1@f04n12.cac.psu.edu>...

>> x = rand(10);
>> i = 3:8;
>> j = 3:8;
>> DH(i,j) = abs( x(i,j-2) + x(i,j+2) - 2*x(i,j) ) +...
>> abs( x(i,j-1) - x(i,j+1) );
>> DV(i,j) =abs( x(i-2,j) + x(i+2,j) - 2*x(i,j) ) +...
>> abs( x(i-1,j) - x(i+1,j) );
>> mask = ((DH+35)< DV);
>> G(i,j) = ( x(i,j-1) + x(i,j+1) ) /2 +...
>> ( 2*x(i,j) - x(i,j-2) - x(i,j+2) )/4;
>> G = mask .* G;

> What is the meaning of G = mask .* G;

mask is a variable containing ones where DH+35<DV, zeros elsewhere.
Multiplying mask by G leaves nonzero values in the matrix G wherever
DH+35<DV, and zeros elsewhere. If you don't want zeros elsewhere, you
can modify the definition of mask... maybe you'd rather have NaN?

> Also explain whats the neccesity of this?
> x = rand(10);

x is a 10x10 random matrix; the code should calculate the gradient of
that matrix. You shouldn't need that line, since you already have a
matrix to operate upon. Note that the rest of the code assumes a 10x10
matrix, since i and j are 3:8. You'll have to modify to suit your needs.

BTW, I don't like using i and j as variables, since I often use them in
complex numbers. I usually use ii and jj instead.

>> Ga = zeros(10); %preallocate
>> DHa = Ga;
>> DVa = Ga;

oops... those three lines are unnecessary.

--
Dave
dvt at psu dot edu

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics