Contour and perimeter recognition in binary image - java

I need to detect contour (object) and find the perimeter of a detected object.
For example, I have the following image:
All images are binary, so they consist of only 0 and 1.
I need to firstly detect objects, and then find the perimeter of the object contour, the area will be known I guess because this is just the sum of all object pixels.
I am using 4-pixel coherence while finding objects.
I have found some algorithms, but cannot figure out how they work and how to implement them if I have an array of 1 and 0.
Please, can someone provide explanation or code example of the easiest to understand the algorithm.
I need to do this without using OpenCV or any other library.

Here is what you can do:
Small opening in order to erase all the small patterns.
Connected component labeling in order to detect, label and separate your objects. See here for java codes.
Perimeter extraction (each non null pixel with at least one black neighbor).

Related

To find the coordinates of the skeleton of contour of an image using java

My requirement is to find the coordinates of skeleton of an image using JAVA. This is the actual image contour from which i ahve to find the skeleton
Below are my questions.
I could do that by using OpenCV Java but it has gaps and also it is not 1 pixel width. Can we get the skeleton(of 1 pixel width) with out gaps using Java's openCV?This is the skeleton got from openCV JAVA
I can also find the skeleton by using imageJ library of Java but the returned skeleton is in ByteProcessor with which I can not process further to get the coordinates of skeleton. Is there a way to convert ByteProcessor back to image(matrix) so that I can find the coordinates by using openCV findcontours()?
Apart from these two, are there any other way of finding skeleton of an image using Java.
Attached the images for reference. Please advice.
maybe you should use other words to describe your problem, if it describes better, you should better say you need an array with coordinates of the skeleton. I can propose you to make two pixel sliding window for each line and extract contour when pixels are different (blakc/white) then, just link extracting points in an array to get all coordinates
Here is an ImageJ-macro that gives you the coordinates:
orig=getTitle();
nme=split(orig, ".");
path=getDir("image")+nme[0]+".csv";
run("Duplicate...", "title=cpy");
run("Skeletonize");
run("Create Selection");
run("Save XY Coordinates...", "save="+path);
selectWindow(orig);
run("Restore Selection");
close("cpy");
open(path);
It should be easy to convert it to Java (just use the Recorder).

Rigid transformation of two shapes with OpenCV in Java

As the title says, I'm trying to find a way to generate a transformation matrix to best best align two images (the solution with the smallest error value computed with an arbitrary metric - for example the SAD of all distances between corresponding points). Example provided below:
This is just an example in the sense that the outer contour can be any shape, the "holes" can be any shape, any size and any number.
The "from" image was drawn by hand in order to show that the shape is not perfect, but rather a contour extracted from a camera acquired image.
The API function that seems to be what I need is Video.estimateRigidTransform but I ran into a couple of issues and I'm stuck:
The transformation must be rigid in the strongest sense, meaning it must not do any kind of scaling, only translation and rotation.
Since the shapes in the "from" image are not perfect, the number of points in the contour are not the same as the ones in the "To" image, and the function above need two sets of corresponding points. In order to bypass this I have tried another approach: I have calculated the centroids of the holes and outer contour and tried aligning those. There are two issues here:
I need alignment even if one of the holes is missing in the "from" image.
The points must be in the same order in both lists passed to Video.estimateRigidTransform and there is no guarantee that function findContours will provide them in the same order in both shapes.
I have yet to try to run a feature extractor and matcher to obtain some corresponding points but I'm not very confident in this method, especially since the "From" image is a natural image with irregularities.
Any ideas would be greatly appreciated.

Shape Detection in Java using OpenCV

I need to detect shapes and their colours on a taken image. These shapes are: a heart, a rectangle, a star and a circle. Each shape has one of 5 predefined colours. Colour recognition works fine, but shape recognition is a real problem.
After hours and hours of googling, trying and tweaking code, the best I have come up with is the following procedure:
First, read the image and convert it to grayscale.
Then, apply blur to reduce the noise from the background.
Medianblur seems to work best here. Normal blur has little effect, and Gaussian blur rounds the edges of the shapes which gives trouble.
Next, apply threshhold.
AdaptiveThreshold doesn't give the results I expected; the result vary widely with each image. I now apply two thresholds: One uses Otsu's algorithm to filter the light shapes, and the other uses the Binary Inverted value for the darker shapes.
Finally, find contours on the two threshholds and merge them in one list.
By the amount of points found in each contour, I decide which shape is found.
I have tried adding Canny, sharpening the image, different threshholds, Convex Hull, Houghes, etc. I probably tried every possible value for each method as well. For now, I can get things working with the above procedure on a few images, but then it fails again on a new image. Either it detects too much points in a contour, or it doesn't detect the shape at all.
I am really stuck and dont know what else to try. One thing I still have to work out is using masks, but I can't find much information on that and don't know if it would make any difference at all.
Any advice is more than welcome. If you would like to see my code, please ask. You can find sample images here: http://tinypic.com/a/34tbr/1

Rasterizing a triangle in Java

Is there a Java graphics library that will rasterize a triangle given the coordinates of the vertices?
I'm trying to analyse the pixel values of an image for the triangular region defined by three points. I have the pixel values in memory, so I just want to figure out which pixels are in the triangle and iterate through them. The order of iteration is irrelevant, so long as I visit each pixel once.
I've done some searching for algorithms, and I think I could implement my own code based on Triangle Rasterization for Dummies, or an Introduction to Software-based Rendering, but I'd feel stupid if I overlooked some library that already implements this.
I briefly looked at getting Java to talk to the GPU, but that seems to be too much hassle.
You can use Polygon Shape to represent the tringle. Then use one of the contains() method passing Point2D or just two doubles params.

Getting boundary information from a 3d array

Hey, I'm currently trying to extract information from a 3d array, where each entry represents a coordinate in order to draw something out of it. The problem is that the array is ridiculously large (and there are several of them) meaning I can't actually draw all of it.
What I'm trying to accomplish then, is just to draw a representation of the outside coordinates, a shell of the array if you'd like. This array is not full, can have large empty spaces with only a few pixels set, or have large clusters of pixel data grouped together. I do not know what kind of shape to expect (could be a simple cube, or a complex concave mesh), and am struggling to come up with an algorithm to effectively extract the border. This array effectively stores a set of points in a 3d space.
I thought of creating 6 2d meshes (one for each side of the 3d array), and getting the shallowest point they can find for each position, and then drawing them separetly. As I said however, this 3d shape could be concave, which creates problems with this approach. Imagine a cone with a circle on top (said circle bigger than the cone's base). While the top and side meshes would get the correct depth info out of the shape, the bottom mesh would connect the base to the circle through vertical lines, making me effectivelly loose the conical shape.
Then I thought of annalysing the array slice by slice, and creating 2 meshes from the slice data. I believe this should work for any type of shape, however I'm struggling to find an algorithm which accuratly gives me the border info for each slice. Once again, if you just try to create height maps from the slices, you will run into problems if they have any concavities. I also throught of some sort of edge tracking algorithm, but the array does not provide continuous data, and there is almost certainly not a continuous edge along each slice.
I tried looking into volume rendering, as used in medical imaging and such, as it deals with similar problems to the one I have, but couldn't really find anything that I could use.
If anyone has any experience with this sort of problem, or any valuable input, could you please point me in the right direction.
P.S. I would prefer to get a closed representation of the shell, thus my earlier 2d mesh approach. However, an approach that simply gives me the shell points, without any connection between them, that would still be extremely helpful.
Thank you,
Ze
I would start by reviewing your data structure. As you observed, the array does not maintain any obvious spatial relationships between points. An octree is a pretty good representation for data like you described. Depending upon the complexity of you point set, you may be able to find the crust using just the octree - assuming you have some connectivity between near points.
Alternatively, you may then turn to more rigorous algorithms like raycasting or marching cubes.
Guess, it's a bit late by now to be truly useful to you, but for reference I'd say this is a perfect scenario for volumetric modeling (as you guessed yourself). As long as you know the bounding box of your point cloud, you can map these coordinates to a voxel space and increase the density (value) of each voxel for each data point. Once you have your volume fully defined, you can then use the Marching cubes algorithm to produce a 3D surface mesh for a given threshold value (iso value). That resulting surface doesn't need to be continuous, but will wrap all voxels with values > isovalue inside. The 2D equivalent are heatmaps... You can refine the surface quality by adjusting the iso threshold (higher means tighter) and voxel resolution.
Since you're using Java, you might like to take a look at my toxiclibs volumeutils library, which also comes with sevaral examples (for Processing) showing the general approach...
Imagine a cone with a circle on top
(said circle bigger than the cone's
base). While the top and side meshes
would get the correct depth info out
of the shape, the bottom mesh would
connect the base to the circle through
vertical lines, making me effectivelly
loose the conical shape.
Even an example as simple as this would be impossible to reconstruct manually, let alone algorithmically. The possibility of your data representing a cylinder with a cone shaped hole is as likely as the vertices representing a cone with a disk attached to the top.
I do not know what kind of shape to
expect (could be a simple cube...
Again, without further information on how the data was generated, 8 vertices arranged in the form of a cube might as well represent 2 crossed squares. If you knew that the data was generated by, for example, a rotating 3d scanner of some sort then that would at least be a start.

Categories