Object extraction after Canny - java

I have found out edges by using canny edge algorithm in OpenCV Java. I need to extract the spinal cord from this image.
Can you please suggest the next steps to extract the spinal cord only from this image?
This is the original image:

to extract a specific object from an image you have to find the contours in the canny image first. You can use findContours() to get all contours in the image. This link shows a tutorial on how to do this: http://bytefish.de/blog/extracting_contours_with_opencv/ This tutorial shows a C++ code but java opencv is just an api mapping C++ functions to java.
Next step is to do some calculations about the width and hight ratios of the contours and then you can extract them from your image.
This is not an easy task to do. Research hard to figure out some stuff related to ratios of width and height of the spine.

This is tricky, there are so many variables here. However, if all images of spine are of the same size, you can do matching, Opencv has function to do template matching:
matching
I would also consider using Hough transform to detect rectangles/squares since the Spine is clearly composed of several rectangles Hough

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).

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

What is algorithm to detect a shape on some image? [duplicate]

I want to detect a circle, rectangle shaped object in an image and read the information from that object. Is there any api in java which will be helpful to me?
Ex: Detect a round shaped coin in a white background and obtain information about that that coin like ( value of a coin, etc.)
Thanks.
Here's an answer to a similar question for C++.
For Java, you can use the OpenCV wrappers. However, once you understand the essence of the approach you should be able to solve your problem using whichever framework is available.
Circles are perfect targets for the Hough transform. Check this out Detect circles with HT and OpenCV
Rectangles are a bit harder since the Hough Transform is not rotation invariant. You can go into edge detection and fast fitting (Fast line and rectangle detection by clustering and grouping)

Crop image after selecting area using edge detection in android

I want to automatically select area of the page to crop later.
I thought edge detection might be useful and used canny edge detection to find edges of the image. Now I have this image! but I've no clear idea to select area of the page as a rectangle. Can anyone suggest a method or implementation for this problem?
What I really want to do is this selecting the area of the page as follows.
Is there any other method to do this? I have also seen boundary detection in the book of introduction to digital image processing using matlab. But I'm not familiar with that. Can I use that for this purpose?
I assume that you have "Canny-ed" image. Now you can use
Imgproc.findCountours, to find and store edges (you need List<MatOfPoint>for that). Before using findContours i would play with Imgproc.dilate, which might help finding contours(it "fattens" lines so we are sure that findContours wont miss your target). Then you need only to use Imgproc.boudningRect to get your ROI. Then just crop image using this ROI.

Ellipse detection with OpenCV

I would like to detect ellipses with OpenCV for Android, using the Tutorial 2-Basic included with OpenCV 2.4.1 package as a starting point. Note that my ellipse would be a perfect-photoshop one.
From what I understand, using the "HoughCircles" will only find perfect (or so) circles, thus leaving ellipses out.
Any help would be much appreciated as I am a total beginner at OpenCV
This is what I've tried so far
case Sample2NativeCamera.VIEW_MODE_CANNY: (ignore the Canny mode...)
capture.retrieve(mGray, Highgui.CV_CAP_ANDROID_GREY_FRAME);
Imgproc.HoughCircles(mGray, mCircles, Imgproc.CV_HOUGH_GRADIENT, 1, 20);
Log.d("Ellipse Points", " X " + mCircles.get(1,1)[0] + mCircles.get(1, 1)[1]);
break;
If you think any more info could be useful, please let me know.
One possible solution to your problem is similar to this thread Detection of coins (and fit ellipses) on an image .
You should take a look a opencv's function fitEllipse.
The parameters used in HoughCircles play a fundamental role. HoughCircles will detect not just perfect, but also near-perfect circles (ellipses). I suggest you check this examples:
How to detect circles (coins) in a photo
Simple object detection using OpenCV
OpenCV dot target detection
Reshaping noisy coin into a circle form
And this answer has a decent collection of references.
If you already have an idea of the sizes of the ellipses that you're looking for, then try the following steps:
Find Canny edges in the image
Use a sliding window, the size of which is the maximum length of the major axis of ellipses you're looking for.
Within the window, collect all edge pixels, pick 6 pixels randomly and use linear least squares to fit an ellipse in the general form.
Repeat the above step in a RANSAC like procedure.
If there are enough inliers, you have an ellipse.

Categories