Detecting edge only in particular portion of an image - matlab

I want to detect the edge only in the red marked region as shown in the image below:

A few suggestions. I assume that the red region is input by mouse and that you now have a mask of the region that you want to include in the edge search.
My proposed algorithm
1. Do Edge detection
2. Write your own Hough routine but only count edges if they should be included according to the mask.
3. Pick the edge with the best score in the Hough space.
Of course you don't need to run the edge detection on the complete image but if you don't make sure that you handle the border of your search area (so you don't get edges there). Simply mirroring the area might work.
Update
Okay, different approach:
Use the hough routines in matlab. houghlines, hough, houghpeaks are the relevant functions. If only one line intersects your region of interest, you are done. The line is the result you want.
If more than one line intersect the region of interest, you need to do a bit more. I'd suggest counting the number of pixels along the line that are within the ROI. So, if the line intersects the ROI for 10 pixels, that line's score is 10. Do this for all lines and then pick the line with the highest score.
Note that none of the approaches are optimized for speed. However, they are easy to understand.

Related

Image segmentation (OCT)

My image is something like below:
I want to be able to draw 2 layers: (1) red line on top of 1st layer, but (2) blue line in the middle of 2nd layer
I am using OpenCV. but any languages/advice are welcomed.
You can do the following:
Small closing in order to reconnect the small separated components/patterns.
Small opening in order to remove the small isolated components/patterns.
Skeletonize (or median axis)
Pruning in order to remove the small branches.
You will then get a skeleton for each pattern. It will be close to the lines you want to draw. But it will be a little bit irregular, so you can interpolate it.
[EDIT] if you need the red line on top of the edge, a solution is to:
Extract the pattern contour
Keep only the pixel on top.
Algorithmically, it can be achieved doing this: for each X coordinate on the top border, go down the image vertically until you meet the first non-null pixel. If your image is NxM, you must have N pixels in your solution.
If you want to regularize/smooth the result, you have two solutions:
Transform the contour as a parametric function and smooth it.
Do an interpolation (spline?)

how to do line fitting multiple lines MATLAB?

I'm trying find all straight lines in an image which is border. for example,stamps have four edges and I have already find those edges by edge function in MATLAB. But there is a problem that they are not real straight line. so I need to use line fitting to get all four borders. But polyfit function can only fit one line at one time. Is there any solutions that can fit all lines at one time.
for example:here I upload some pictures,the image with red lines is what I want. Please be ware I need four separate lines.
Judging from the image you won't be trying to smooth some lines, or fill in the gaps. Instead it looks more like you need to put your image in the smallest possible box.
Here is an algorithm that you can try:
Start from all 4 corners.
'walk' one of the corners inwards and determine if all points are still within four corners
If so, save this corner and go to step 2, else go to step 2
Keep repeating step 2 and 3 till you have a steady solution.
Are you trying to get rid of the perforations? In that case I would suggest using thresholding to segment out dark areas of the image, and then using regionprops to get their bounding boxes. Then you can figure out the largest rectangle that excludes them.

Method to fill in any size hole

I'm working on a fingerprint recognition project but I need to pre-process the image. I go through the following process.
1) Binarization
2) Filtering to removing the "stair-step" effect; i.e smoothing
3) Thin the lines
I'm adding in a step that I'm trying to develop that will fill in any holes that are left after thinning. I'm trying to accomplish this as follows.
4a) Use bwlabel to find regions (I might consider using bwmorp(...,'shrink') to just leave the "blobs" but doing this reduces the size of the blob a little).
4b) Find all regions that do not have the maximum area
4c) Use the location of these regions to shrink these "blobs" to points.
But how can I apply shrink at specified locations?
Binarization
Filtering
Thinning
Hole filling
I am Not sure if I understood the question correctly. But can you take complement of the thinned image and then use bwlabel. After that, count the number of pixels belonging to each label. Apply your criteria to select labels and get their locations. After that, you can use imfill(bw,locations) command.
If you have a region you want to shrink to a point then calculate the center of mass for the region. Do this by just averaging the x and y coordinates of each point in the blob. Set the region pixels to black and the center of mass to white.
The problem that I am having is trimming off the ridges to just leave the filled areas while avoiding erosion of the filled areas (if I were to use erode the ridges would disappear but also reduce the size of the area).
You don't really have to worry about erosion of the small regions as it will have little impact on the center of mass, and you point will still end up going to an appropriate location.

Fill an outline which is incomplete

Consider that I have a colored image like this in which the outline is not complete (There are gaps between lines). I want to be able to fill the area between the lines with one color or another. This actually is a binary image which I got after applying canny edge detector on a corresponding gray scale image.
I tried first dilating the image and then eroding it, but the result is not good enough. I want to be able to preserve the thickness of the root
Any help would be greatly appreciated
Original Image
Image after edge detection and some manual removal of pixels
Using the information in the edge image, I thought I would try to extract pixels from the original image of a certain color. For every white pixel in the edited image, I used a search space in the original image along the same horizontal line. I used different thresholds for R, G and B and I ended up with this
I'm not sure what your original image looks like. It would be helpful to see.
You have gaps between the lines because a line in your original image has two edges, one on each side. The canny algorithm is detecting them both. The Canny edge detection algorithm has at its heart the application of two Sobel kernels to calculate the gradient, one for detecting horizontal edges and one for detection vertical edges.
-1 0 +1
-2 0 +2
-1 0 +1
and
+1 +2 +1
0 0 0
-1 -2 -1
These kernels will present peaks for both sides of the line. One peak positive and one negative. You can exclude one side of the line by excluding the corresponding peak. After taking the gradient of each direction truncate any values below zero (set the values to zero) to remove the second peak. Then continue with the Canny edge detection as usual. This will result in the detection of only a single edge for each line instead of the two that you are seeing now.
I'll add a third approach now that I have seen the image. It looks like most of the information is in the green channel.
Green channel image
This image gives you a decent result if you simply apply a threshold.
Thresholded image with a somewhat arbitrary threshold
You can then either clean this image up by itself or use your edge image. To clean it up with the edge image you produced remove any white pixels that are more than a certain distance from one of your detected edges (create a Euclidean distance map from your edge image and use that to set any white pixels greater than a certain distance from an edge to black).
If you are still collecting images you may want to try to position the camera in a way to avoid the bottom of the jar (or whatever this is).
You could attempt to use a line scanning methodology. Start at the side and scan horizontally. When you hit an edge you assume you are in a root and you start setting the voxels to white. When you hit another edge you assume you are leaving a root and you start. There will be some fringe cases and you may want to add additional checks, such as limiting the allowed thickness of a root.
You could also do a flood fill style algorithm where you take a seed point in a root and travel up the root filling it in.
Not sure how well these would work as it depends on the image and I did not test it.

Techniques for differentiating between circle rectangle and triangle?

What coding techniques would allow me to to differentiate between circles, rectangles, and triangles in black and white image bitmaps?
You could train an Artificial Neural Network to classify the shapes :P
If noise is low enough to extract curves, approximations can be used: for each shape select parameters giving least error (he method of least squares may help here) and then compare these errors...
If the image is noisy, I would consider Hough transform - it may be used to detect shapes with small count of parameters, like circle (harder for rectangles and triangles).
just an idea off of the top of my head: scan the (pixel) image line-by-line, pixel-by-pixel. If you encounter the first white pixel (assuming it has a black background) you keep it's position as a starting point and look at the eight pixels surrounding it in every direction for the next white pixel. If you find an adjacent second pixel you can establish a directional vector between those two pixels.
Now repeat this until the direction of your vector changes (or the change is above a certain threshold). Keep the last point before the change as the endpoint of your first line and repeat the process for the next line.
Then calculate the angle between the two lines and store it. Now trace the third line. Calculate the angle between the 2nd and 3rd line as well.
If both angles are rectangular you probably found a rectangle, otherwise you probably found a triangle. If you can't find any straight line you could conclude that you found a circle.
I know the algorithm is a bit sketchy but I think (with some refinement) it could work if your image's quality is not too bad (too much noise, gaps in the lines etc.).
You are looking for the Hough Transform. For an implementation, try the AForge.NET framework. It includes circle and line hough transformations.