Image processing: Rotational alignment of an object - matlab

I have a stack of images with a bar close to the center. As the stack progresses the bar pivots around one end and the entire stack contains images with the bar rotated at many different angles up to 45 degrees above or below horizontal.
As shown here:
I'm looking for a way to rotate the bar and/or entire image and align everything horizontally before I do my other processing. Ideally this would be done in Matlab / imageJ / ImageMagick. I'm currently trying to work out a method using first Canny edge detection, followed by a Hough transform, followed by an image rotation, but I'm hoping this is a specific case of a more general problem which has already been solved.

If you have the image processing toolbox you can use regionprops with the 'Orientation' property to find the angle.
http://www.mathworks.com/help/images/ref/regionprops.html#bqkf8ji

The problem you are solving is known as image registration or image alignment.
-The first thing you need to due is to treshold the image, so you end up with a black and white image. This will simplify the process.
-Then you need to calculate the mass center of the imgaes and then translate them to match each others centers.
Then you need to rotate the images to matcheach other. This could be done using the principal axis measure. The principal axis will give you the two axis that explain most of the variance in the population. Which will basically give you a vector showing which way your bar is pointing. Then all you need to due is rotate the bars in the same direction.
-After the principal axis transformation you can try rotating the pictues a little bit more in each direction to try and optimise the rotation.
All the way through your translation and rotation you need a measure for showing you how good a fit your tranformation is. This measure can be many thing. If the picture is black and white a simple subtraction of the pictures is enough. Otherwise you can use measures like mutual information.
...you can also look at procrustes analysis see this link for a matlab function http://www.google.dk/search?q=gpa+image+analysis&oq=gpa+image+analysis&sugexp=chrome,mod=9&sourceid=chrome&ie=UTF-8#hl=da&tbo=d&sclient=psy-ab&q=matlab+procrustes+analysis&oq=matlab+proanalysis&gs_l=serp.3.1.0i7i30l4.5399.5883.2.9481.3.3.0.0.0.0.105.253.2j1.3.0...0.0...1c.1.5UpjL3-8aC0&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.&bvm=bv.1355534169,d.Yms&fp=afcd637d8ae07bde&bpcl=40096503&biw=1600&bih=767

You might want to look into the SIFT transform.
You should take as your image the rectangle that represents a worst case guess for your bar and determine the rotation matrix for that.
See http://www.vlfeat.org/overview/sift.html

Use the StackReg plugin of ImageJ. I'm not 100% sure but I think it already comes installed with FIJI (FIJI Is Just ImageJ).
EDIT: I think I have misread your question. That is not a stack of images you are trying to fix, right? In that case, a simple approach (probably not the most efficient but definetly works), is the following algorithm:
threshold the image (seems easy, your background is always white)
get a long horizontal line as a structuring element and dilate the image with it
rotate the structuring element and keep dilating image, measuring the size of the dilation.
the angle that maximizes it, is the rotation angle you'll need to fix your image.

There are several approaches to this problem as suggested by other answers. One approach possibly similar to what you are already trying, is to use Hough transform. Hough transform is good at detecting line orientations. Combining this with morphological processing and image rotation after detecting the angle you can create a system that corrects for angular variations. The basic steps would be
Use Morphological operations to make the bar a single line blob.
Use Hough transform on this image.
Find the maximum in the transform output and use that to find orientation angle.
Use the angle to fix original image.
A full example which comes with Computer Vision System Toolbox for this method. See
http://www.mathworks.com/help/vision/examples/rotation-correction-1.html

you can try givens or householder transform, I prefer givens.
it require an angle, using cos(angle) and sin(angle) to make the givens matrix.

Related

How to get the area of the bubble in the image using MATLAB?

Here are some images taken from experiments which show a bubble caused by spheres moving in liquid.
Now I want to get the area of the bubble from every image using Matlab. The first thing come to my mind is edge detection. So I tried using the following code:
A = imread('D:\1.jpg');
BW1 = edge(A,'sobel');
figure, imshow(BW1)
to get the cavity edge of the picture which was then cropped manually, as the picture show, the result (below) doesn't satisfy requirements. Also, I still don't know how to get the area of the bubble.
So, can someone tell me what should I do?
I think you should use background subtraction and try a simple segmentation.
You could use regionprops to get the area of the bubble:
https://www.mathworks.com/help/images/ref/regionprops.html
I feel like it should work pretty well. If you have a hard time obtaining a clean segmentation you could probably improve the experimental setup to increase the contrast of the bubble with respect to the background by choosing a background as dark as possible and using some lateral illumination to leverage the diffusion of the light by the bubble.
Finally the segmentation should be performed in a region of interest (ROI) since you know the bubble is confined within the tank
As for the issue of getting an accurate cavity edges, the computer vision system toolbox has the vision.ForegroundDetector object, which implements a variant of Stauffer and Grimson's GMM background subtraction. The implementation is very fast, leveraging multiple cores. Check out this example of how to use background subtraction.
As for the issue of finding the area of the bubble, use the bwarea command. https://www.mathworks.com/help/images/ref/bwarea.html, it will sum up all the white pixels in the image.
I believe background subtraction is the most efficient method to calculate this bubble area. Note that you may need to use opening and closing techniques afterwards to filter other regions see (imopen imclose) at: https://uk.mathworks.com/help/images/ref/imopen.html , and afterwards, you can apply bwarea to calculate area. You could also use impixelinfo command to compare intensity level of bubbles and other areas, and therefore, threshold image to extract bubbles. It works only when you have same threshold level for all images. Further, it is possible to combine all these techniques which is completely depended on your images to achieve better results.
Other shape-based techniques also can be used to extract bubble region area.

exact working of hough transform in matlab

Can anyone guide me how the function 'hough transform' works in matlab?? The problem is that i have an image containing two straight rectangles and one rectangle is tilted at some angle. According to me after applying hough transform; i should get a line structure of 1X6 but i am getting a structure of 1x14. Can anyone help me? I have also uploaded the images:
You can't restrict Hough Transform to give a structure of 1x6.It doesn't produce stabile results.It also doesn't work well when looking further ahead on curved roads. I should not acquire 1x6 structure from each frame.Instead, I should take all returned line segments and use some logic to determine the lane markings.
First of all, your image actually looks slightly blurred. I don't know if it actually is , but if so, you need to run an edge detection algorithm, so your hough transform does not detect the blurred part of the line.
Second of all, you need to reduce the number of detected lines, simply by taking out any lines which does not have enough points going through it. This can be done by thresholding the H variable in [H,t,r]=hough(image).
Additional Sources:
http://en.wikipedia.org/wiki/Hough_transform
http://www.mathworks.com/help/toolbox/images/ref/hough.html

after rotating image on matlab..then results go on bad:(

I am intersted with determining branchpoints of retinal images and then registering these images. First of all I am using a algorithm for extracting of vessels from image then skeletization and finding branchpoints. At first operation all things were ok but if I rotate same image by randomize angle, number of branchpoints are very increase. what are your opinions about this problem...
related links of images :
http://e1204.hizliresim.com/w/6/449m6.jpg
http://e1204.hizliresim.com/w/6/449by.jpg
imrotate's standard interpolation algorithm is nearest neighbor, that tends to transform your image in a beautiful saw.
If you rotate your image with imrotate, try passing as the third parameter a better method, like bicubic or bilinear, both will be much better.
The syntax is simple:
imrotate(img, degrees, 'bicubic');

Is there a way to figure out 3D distance/view angle from a 2D environment using the iPhone/iPad camera?

Maybe I'm asking this too soon in my research, but I'd better know if this is possible sooner than later.
Imagine I have the following square printed on a paper on top of a table:
The table is brown, so it does not match with any of the colors in the square. Is there a way for me, from a common iPhone camera (non-stereo view), to figure out the distance and angle from which Im looking at the square in the table?
In the end what I'm looking for is being able to draw a 3D square on top of this one using the camera image, but I'm not sure if I am going to be able to figure out the distance and position of the object in space using only a 2D image. Any hints are well appreciated.
Short answer: http://weblog.bocoup.com/javascript-augmented-reality
Big answer:
First posterize, Then vectorize, With the vectors in your power you may need to do some math tricks to define, based on the vectors position, the perspective and then the camera position.
Maybe this help:
www.pixastic.com/lib/docs/actions/posterize/
github.com/selead/cl-vectorizer
vectormagic.com/home
autotrace.sourceforge.net
www.scipy.org/PyLab
raphaeljs.com/
technabob.com/blog/2007/12/29/video-games-get-vectorized/
superuser.com/questions/88415/is-there-an-open-source-alternative-to-vector-magic
Oughta be possible. Scan the image for the red/blue/yellow pattern, then do edge detection to figure out how warped the squares are (they'll be parallelograms in anything but straight-on view). Distance would depend on the camera's zoom setting and scan resolution. But basically you'd count how many pixels are visible in each of the squares, run that past the camera's specs and you should be able to determine a rough distance.

I need help compensating for the shifting of images when trying to create a grid with one image and apply it on another

I have two images of yeast plates:
Permissive:
Xgal:
The to images should be in the same spot and roughly the same size. I am trying to use one of the images to generate a grid and then apply that grid to the other image. The grid is made by looking at the colonies on permissive plate, the plate should have 1536 colonies on it. The problem is that the camera that was used to take the images moves a bit up and down and the images can also be shifted slightly due to the other plate not being in exactly the same place.
This then means that when I use the permissive plate to generate the grid on the xgal plate the grid shifts. Does anyone know a way in which I can compensate for this? I am using perl with the gd module. Any advice would be greatly appreciated. Thank you
I've done this in other languages in relation to motion analysis. You can mathematically determine the shift in position between two images using cross correlation.
Fortunately, you may not need to actually do the maths :) You could use something like ImageMagick, which provides a lot of image processing functions for you, and is perl scriptable. Independently scripts already exists for tasks very much like yours -- see.
If you have only a few pairs of images and, as in the examples, they are very different in appearance then an alternative method to Tim Barrass' would be
Open the first image in gimp, find the co-ordinates of a landmark feature
Open the second image in gimp, find the co-ordinates of the same landmark
Calculate the offset
Shift the second image using ImageMagick's convert command with the affine option. Set the parameters sx=sy=1.0, rx=ry=0.0, tx= negative horizontal offset, ty= negative vertical offset