implement 3d sobel operator - image

I am currently working on inhomogeniety removal from MRI data volume which contains voxels.
I want to apply sobel operator on those volumes to find the gradient. I am familiar with 2d sobel mask and the neighbourhood of 2d images.
sobel mask:
1 2 1
0 0 0
-1 -2 -1
1 0 -1
2 0 -2
1 0 -1
neighbourhood of(x,y):
(x+1,y-1) (x+1,y) (x+1,y+1)
(x,y-1) (x,y) (x,y+1)
(x-1,y-1) (x-1,y) (x-1,y+1)
Now I want to apply it on 3d.
Please suggest me how should I proceed??
Thank you.

Wikipedia has a nice introduction about that : http://en.wikipedia.org/wiki/Sobel_operator
Basically, since the sobel filter is separable, you can apply 1D filters in each of x, y and z directions consecutively. Theses filters are h(x) and h'(x) as given on wikipedia. Doing so will allow you to get the edges in the direction where you applied h'(x).
For example, if you do h(x)*h(y)*h'(z), you'll get the edges in the direction z.
Alternatively (and more expensively), you can compute the whole 3D 3x3x3 kernel and apply the convolution in 3D. The kernel for the z direction is given on wikipedia as well.

Good question! For 3D images you have to use 3 different 3x3x3 sobel operators: 1 for each direction, that is x, y and z. Be aware that in digital image processing the x-coordinate axis points right, y-coordinate downwards, and z-coordinate into the screen!
I visualized all three 3D sobel operators to make it more intuitive. Here the sobel filters in X direction, Y direction and Z direction.
Furthermore, if you want to see the equations behind it (basically what your code has to compute) here you go: SobelFilterEquations

Related

Plotting a location into an irregular rectangle

I have 4 Point values: TopLeft, TopRight, BottomLeft, BottomRight. These define a 4 sided shape (like a distorted rectangle) on my monitor. These are the point a Tobii gaze device thinks I am looking at when in fact I am looking at the four corners of my monitor.
This picture shows a bitmap on the left representing my monitor, and the points the Tobii device tells me I am looking at when I am in fact looking at the corners of the screen. (It's a representation, not real).
I want to use those four calibration points to take a screen X,Y position that is from an inaccurate gaze position and correct it so that it is positioned as per the image on the right.
Edit: New solution for the edited question is at the end.
This problem is call bilinear interpolation.
Once you grasp the idea, it will be very easy and you would remember it for the rest of your life.
It would be quite long to post all detail here, but I will try.
First, I will name the point on the left to be (x,y) and the right to be (X,Y).
Let (x1,y1), (x1,y2), (x2,y1), (x2,y2) be the corner points on the left rectangle.
Secondly, let's split the problem into 2 bilinear interpolation problems:
want to find X
want to find Y
Let's find them one by one (X or Y).
Define : Qxx are the value of X or Y of the four corner in the right rectangle.
Suppose that we want to find the value of the unknown function f at
the point (x, y). It is assumed that we know the value of f at the
four points Q11 = (x1, y1), Q12 = (x1, y2), Q21 = (x2, y1), and Q22 =
(x2, y2).
The f(x,y) of your problem is X or Y in your question.
Then you interpolate f(x,y1) and f(x,y2) to be f(x,y) in the same way.
Finally, you will got X or Y=f(x,y)
Reference : All pictures/formulas/text here are copied from the wiki link (some with modification).
Edit: After the question has been edited, it become very different.
The new one is opposite, and it is called "inverse bilinear interpolation" which is far harder.
For more information, please read http://www.iquilezles.org/www/articles/ibilinear/ibilinear.htm
You can define a unique Linear Transform using 6 equations. The 3 points which have to align provide those 6 equations, as each pair of matching points provides two equations in x and y.
If you want to pursue this, I can provide the matrix equation which defines the Linear Transform based on how it maps three points. You invert this matrix and it will provide the linear transform.
But having done that, the transform is completely specified. You have no control over where the corner points of the original quadrilateral will go. In general, you can't even define a linear transform to map one quadrilateral onto another; this gives 8 equations (2 for each corner) with only 6 unknowns. Its over-specified. In fact a Linear Transform must always map a rectangle to a parallelogram, so in general you can't define a Linear Transform which maps one quadrilateral to another.
So if it can't be a Linear Transform, can it be a non-Linear Transform? Well, yes, but non-Linear Transforms don't necessarily map straight lines to straight lines, so the mapped edges of the quadrilateral won't be straight. Or any other lines. And you still have 14 equations (2 for each point and corner) for which you have to invent some non-Linear transform with 14 unknowns.
So the problem as stated cannot be solved with a Linear Transform; its over specified. Using a non-Linear transform will require you to devise a non-Linear transform which has 14 free variables (vs the 6 in a Linear Transform), this will map the 7 points correctly but straight lines will no longer be straight. Adding this requirement in adds an infinite number of constraints (one for every point in the line) and you won't even be able to use continuous functions.
There may be some solution to what you are doing in terms of what you are really trying to do (ie the underlying application need), but as a mathematical problem it is unsolvable.
Let me know if you want the matrix equation to produce a Linear Transform based on how it transforms 3 points.

Sobel Edge detection – matlab

hello as part of my Homework. i need to calculate and display the edge magnitude image and the
edge direction image of image balls1.tif, using Sobel Edge detection.
Do not use matlab's edge function. You may use conv2.
Display a binary edge image (1 edge pixel, 0 no edge) of strong edge pixels (above a threshold).
Determine a threshold that eliminates the ball shadows.
here is my main.m
addpath(fullfile(pwd,'TOOLBOX'));
addpath(fullfile(pwd,'images'));
%Sobel Edge Detection
Image = readImage('balls1.tif');
showImage(Image);
message = sprintf('Sobel Edge Detection');
sobelEdgeDetection(Image);
uiwait(msgbox(message,'Done', 'help'));
close all
here is my SobeEdgeDetection.m
function [ output_args ] = SobelEdgeDetection( Image )
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
resX = conv2(Image, maskX);
resY = conv2(Image, maskY);
magnitude = sqrt(resX.^2 + resY.^2);
direction = atan(resY/resX);
thresh = magnitude < 101;
magnitude(thresh) = 0;
showImage(magnitude);
end
my questions are:
1. i what is the direction used for ? and how can i display it?
2. is there a better way to get a threshold to eliminate the ball shadows. i used trial and error....
these are my result as far as showing the magnitude:
According to the second part of your homework you have solved it, i.e., you eliminated the shadows.
For the first question: the direction can be used in many different ways. Here is the simplest way: make pretty pictures with it. A more useful reason to consider it is when you are doing non-maximum suppression, but since you are not manually doing it then there isn't much immediate use for it. To visualize the results of the gradient direction it is simply matter of establishing colors for each direction you consider. To further simplify the visualization also suppose you reduce the directions to increments of 30 degrees up to 180, starting from 0. That way if you have a direction of 35 degrees, for example, you consider it as 30 degrees (since it is the nearest one in your reduced list). Next we see an image and a visualization of its gradient direction considering Sobel and the discretization to steps of 30 degrees (black is indicating 0 degree direction).
Automatically determining good thresholds is usually not an easy task. For example, you could start with the one provided by the Otsu method, and decrease or increase its value based on some other histogram analysis according to the problem you are trying to solve.
Here's the answer to your first question :
In Sobel Edge Detection Algo. the direction obtained is basically the gradient.
Gradient in image processing is defined as the direction in which change for intensity is maximum. Change can be increase in intensity or decrease in intensity. Also, this change is calculated for each pixel,this means that for each pixel maximum change in intensity is measured. resX (in your question's example,SobelEdgeDetection.m) signifies changes in X-direction and resY defines change in Y direction.
See see it practically just fire this command in command window of Matlab:
imshow(resX);
Also try, imshow(resY)

Understanding Matrices - Reading Rotation

I am trying to learn more about matrices. If I have a 4x4 matrice such as :
0.005 0.978 -0.20 60.62
-0.98 -0.027 0.15 -18.942
-0.15 0.20 0.96 -287.13
0 0 0 1
Which part of the matrix tells me the rotation that is applied to an object ? I know that column 4 is the position of the object and suspect row 1,2 and 3 are the x,y and z rotation ?
Thanks in advance.
The first three columns are directional vectors in the x, y, z directions, possibly including scaling of the object. If you imagine a cube, the first column's vector points in the direction of the positive-x-face of the cube, the second in the direction of the positive-y-face and the third in the direction of the positive-z-face.
Note that when object-scaling was applied to the matrix (which doesn't appear to be the case in your example), those direction vectors are not normalized.
But this isn't "rotation" in the euler-angle or quaternion-rotation sense. In fact calculating any angles from this matrix is pretty tricky.
Here are some links that explain how to do it, but this comes with a lot of problems and you should avoid it if it's not absolutely necessary:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

Is there any algorithm for determining 3d position in such case? (images below)

So first of all I have such image (and ofcourse I have all points coordinates in 2d so I can regenerate lines and check where they cross each other)
(source: narod.ru)
But hey, I have another Image of same lines (I know thay are same) and new coords of my points like on this image
(source: narod.ru)
So... now Having points (coords) on first image, How can I determin plane rotation and Z depth on second image (asuming first one's center was in point (0,0,0) with no rotation)?
What you're trying to find is called a projection matrix. Determining precise inverse projection usually requires that you have firmly established coordinates in both source and destination vectors, which the images above aren't going to give you. You can approximate using pixel positions, however.
This thread will give you a basic walkthrough of the techniques you need to use.
Let me say this up front: this problem is hard. There is a reason Dan Story's linked question has not been answered. Let provide an explanation for people who want to take a stab at it. I hope I'm wrong about how hard it is, though.
I will assume that the 2D screen coordinates and projection/perspective matrix is known to you. You need to know at least this much (if you don't know the projection matrix, essentially you are using a different camera to look at the world). Let's call each pair of 2D screen coordinates (a_i, b_i), and I will assume the projection matrix is of the form
P = [ px 0 0 0 ]
[ 0 py 0 0 ]
[ 0 0 pz pw]
[ 0 0 s 0 ], s = +/-1
Almost any reasonable projection has this form. Working through the rendering pipeline, you find that
a_i = px x_i / (s z_i)
b_i = py y_i / (s z_i)
where (x_i, y_i, z_i) are the original 3D coordinates of the point.
Now, let's assume you know your shape in a set of canonical coordinates (whatever you want), so that the vertices is (x0_i, y0_i, z0_i). We can arrange these as columns of a matrix C. The actual coordinates of the shape are a rigid transformation of these coordinates. Let's similarly organize the actual coordinates as columns of a matrix V. Then these are related by
V = R C + v 1^T (*)
where 1^T is a row vector of ones with the right length, R is an orthogonal rotation matrix of the rigid transformation, and v is the offset vector of the transformation.
Now, you have an expression for each column of V from above: the first column is { s a_1 z_1 / px, s b_1 z_1 / py, z_1 } and so on.
You must solve the set of equations (*) for the set of scalars z_i, and the rigid transformation defined R and v.
Difficulties
The equation is nonlinear in the unknowns, involving quotients of R and z_i
We have assumed up to now that you know which 2D coordinates correspond to which vertices of the original shape (if your shape is a square, this is slightly less of a problem).
We assume there is even a solution at all; if there are errors in the 2D data, then it's hard to say how well equation (*) will be satisfied; the transformation will be nonrigid or nonlinear.
It's called (digital) photogrammetry. Start Googling.
If you are really interested in this kind of problems (which are common in computer vision, tracking objects with cameras etc.), the following book contains a detailed treatment:
Ma, Soatto, Kosecka, Sastry, An Invitation to 3-D Vision, Springer 2004.
Beware: this is an advanced engineering text, and uses many techniques which are mathematical in nature. Skim through the sample chapters featured on the book's web page to get an idea.

skew matrix algorithm

I'm looking for skew algorithm, just like on photoshop, edit->transform->skew
is there any simple matrix which could do that?
what I've seen so far was basic skew matrix (shear) but its lack of control point, doesn't like on photoshop which have at least 4 points on each corner of rectangle and we can move each control point freely.
I need to implement it to transform a plane.
Looking at http://www.w3.org/TR/SVG11/coords.html, which talks about SVG, it says:
A skew transformation along the x-axis is equivalent to the matrix
or [1 0 tan(a) 1 0 0], which has the effect of skewing X coordinates by angle a.
A skew transformation along the y-axis is equivalent to the matrix
or [1 tan(a) 0 1 0 0], which has the effect of skewing Y coordinates by angle a.
Hope that helps! :)

Resources