Euclidean distance two pixels, each belonging to different images - matlab

I'm trying to implement Naive Bayes Nearest Neighbor (NBNN) for image classification. In the algorithm it asks for the Euclidean distance between two pixels belonging to different images.
I have 1) a set of m-images in a m-by-40,000 matrix (where 40,000 is the number of pixels in one image) and 2) another set of n-images in a n-by-40,000 matrix.
1) is the training set and 2) is the validation set.
In order for me to apply NBNN, from my understanding, I need to find the Euclidean distance between each pixels of 2) to the corresponding pixels of 1).
My question is, given two grey scale values, one from 1) and the other from 2), how would I find the Euclidean distance between them in order to apply k-NN?

Let x, y be two gray-scale 200-by-200 images. Pixels levels are x1,x2,...x40000 and y1, y2,...y40000.
The euclidean distance between x and y is d(x,y)=sqrt(sum_i((xi-yi)^2))

I will refer to the notation and definition given at wikipedia
You have 1d-data, thus p=(p1) and q=(q1). e(p,q)=sqrt((p1-q1)^2)=abs(p1-q1)
For the 1d-case, the euclidean distance is the absolute difference of the grey values.

Related

does affine transformation change neighbourhood

I have a set of 100 points.
I am considering 4 closest neighbour of each point.
Using three (non-collinear) points as basis among these four, I get affine coordinate of the fourth point.
This way I create database for 100 points.
There are 100 x 4C3 x 3 records. Each having an affine coordinate.
Now, I did affine transformation on all 100 points.
Specifically ,
If a point is (x,y)
It becomes new_x = x+2y+5
It becomes new_y = 3x+4y+6
I have observed that closest neighbours of all points got changed.
Actually some points had 1 or 2 neighbours as before, but rest changed.As a result affine coordinate matching did not work.
Note that I am calculating closest neighbour using euclidean distance i.e sqrt((x2-x1)2+(y2-y1)2).
When I think about affine transformation - I feel rotation,scaling and translation won't change neighbours but shearing maybe can.
Is my transformation not affine ?
Can you suggest a better way to get neighbours ?
Thanks.

How can I compute the diameter of the circle that circumscribes an irregular object?

I want a function to compute and get the diameter of the circle that circumscribes the object. Is there a built-in function in MATLAB to do this? Otherwise, what can I do?
Try this algorithm:
Compute the average x and average y for every point in the irregular object. This is done by taking the x and y component for every point and add them into the total x and total y and then divide by the number of points. This average x and average y point algorithm gives you a non-weighted center of the object.
Use that center point to compute the distance for every point in the irregular object again. Keeping the largest distance as the radius of the object.
Use the center point and the radius to compute the circumference.
I am submitting proof that the distance between the 2 points that are furthest apart in the object fails with a simple triangle. See image below. Also, the big-O notation for computing the two points that are the furthest apart is x^2. The big-O for this algorithm is 2x. The diameter of the circle in the image would be computed as 20; distance between -10,0 and 10,0. A circle of diameter 20 will not encompass the point # 0,-11. Any movement of the circle would automatically remove at least one of the two points used to compute the diameter of the circle because both points are on tangents.
Suppose M is a mask in BW, just do :
[b_x,b_y] = find(bwperim(M)== 1)
Check this function bwperim

Measure circularity or ellipticality

I have some binary images that want to classify them base on shape of them in MATLAB. If they have circular or elliptical shape they belong to class one,if they have elliptical shape with dent in their boundary they belong to class two. I dont know how can I use this feature. Can any body help me with this?
You can use the eccentricity property in regionprops. From MATLAB documentation of eccentricity:
The eccentricity is the ratio of the distance between the foci of the ellipse and its major axis length. The value is between 0 and 1. (0 and 1 are degenerate cases. An ellipse whose eccentricity is 0 is actually a circle, while an ellipse whose eccentricity is 1 is a line segment.)
So as the value of eccentricity increases , the ellipse starts becoming flatter. Hence, at its maximum value = 1, it is a line segment.
To check if there is a dent in the ellipse, you can use check for convexity. Whenever there is a dent in an ellipse, it will be non-convex. In other words, if you try to fit a convex polygon, it won't be able to approximate the shape well enough. You can use convexArea property to check the same. From MATLAB documentation of convexArea:
Returns a p-by-2 matrix that specifies the smallest convex polygon that can contain the region. Each row of the matrix contains the x- and y-coordinates of one vertex of the polygon. Only supported for 2-D label matrices.
So you use bwlabel to create a 2-D label matrix from your binary image and then check the difference between the area of your binary image and the area of the fitted convex polygon. Measuring area could be as simple as counting pixels. You already know that the number pixels of your fitted convex polygon = p. Just take the absolute difference between p and the number of pixels in your original binary image. You should be able to easily set a threshold to classify into one of the two classes.
I think you can write the code for this. Hope this helps.

What exactly is edge density?

I'm kinda confuse of the meaning of edge density. From the equation,
edge density = sum(x=1,w) sum(y=1,h) e(x,y)/N
where e is the edge map image (magnitude of vertical edge at (x,y)), there are two version of N.
1st version - N = w x h (width x height)
2nd version - N = number of non zero vertical edge pixel
What I don't understand is how can I calculate the edge density? Is it just summation of the white edges pixels?
Edited
Hi all, from what I understand from reading the paper given by #Gilgamesh, the N is the area of the region, width times its height but from the answer given it seems there is a conflict whereby N refers to number of non white pixels(black pixel). So, which is the correct one? Here is another reference on the N value calculating edge density.
basically the edge density is really just a (local) average density, which you can either calculate over binarized images or, more common, over grey scale images.
And yes, it is basically just summing up over both x and y coordinates in a subimage in most cases, see equation (1) here
http://ro.uow.edu.au/cgi/viewcontent.cgi?article=1517&context=infopapers
and averaging afterwards.
Regards,
G.
From what I understand about edge density, it is defined as where are the white pixels and is the total number of pixels i.e. .
cannot be the number of black pixels because the number could be arbitrary, ranging from zero to all pixels and the range of edge density will then be .
When is the area, the range will be which portrays just what we want, the places where edges are dense (or sparse depending on your requirement).
The edge map is the map of gradient magnitude (i.e. the length of the gradient vector). So the edge density is the average of the gradient magnitude over a neighborhood.
If you have a binary edge map where 0 means no-edge, 1 means edge (this can be obtained by thresholding the gradient magnitude), then the edge density is just the ratio of edge/non-edge pixels.

Principal direction in a binary image

As shown in image, there is a binary polygonal image. I want to find the principal direction in the image with respect to X-axis. I have shown the principal direction and X-axis with blue line. This can be done using PCA but my problem is such a small rectangle will have around 1000 pixels and I have to find Principal directions for around 100 polygons (polygon can be of arbitrary shape).
One approach that I have thought is:
Project that rectangle onto a line which is oriented at degrees at an interval (say) 5 degrees. The projection which has the maximum variance is the desired projection axis, and thus that is the desired angle. But this also falls under a greedy approach and thus will take time. Is there a smarter approach?
Also, if anybody could explain the exact procedure to do this using PCA, it would be helpful. I know the steps:
1. Take the covariance matrix.
2. Get the top eigenvector corresponding to largest eigenvalue -> that will be the principal direction.
But I am confused in the following statement which I often read everywhere:
A column vector: [0.5 0.5] is the first principal component and it gives the direction of the maximum variance. So can do I exactly calculate the angle by which I should rotate the data so that it will become parallel to X-axis.
Compute the eigenvector associated with the highest eigen value. Call that v. Normalize v. v = v/norm(v);
Compute angle between that and the horizontal direction: angle=acos(sum(v.*[1,0]))
Rotate by -angle, transformation matrix T = [cos(-angle) -sin(-angle); sin(-angle) cos(-angle)], multiply all points by that matrix. Do that for all polygons.