How to implement Gray scale morphology to detect round object on gray scale image in matlab? - matlab

There are many ways to implement math morph on binary image like imerode and imdilate. Its also used to detect different object/shape using this simple operations on binary image but the problem that i am facing right now is to apply this simple operation i.e erode, dilate and many on grey scale image with out convert them into binary image.
Selement = strel('disk',5);//disk type element used in morphology
erodeimage = imerode(image,selement);//this is only implement on binary image
Above code is for binary math morph how do i implement same concept on grey scale image.
Note: If your have any resources regarding gray scale math morph kindly provide it or provide useful link

There should be a mathematical morphology (MM) library in MatLab. MM operations on binary images are shown as example/illustration but are performed most of the time as gray level.
I think that the fastest C++ library is SMIL, and you can call it from MatLab. An other fast one in C is that one (optimized opening/closing in a single pass).
But if you want to understand a dilation in gray level, here is how it works: for a given pixel p you analyze the value of all the pixels in its neighborhood (defined by the structuring element), and you affect to p the highest value in the neighborhood. You do it for each pixel in your image. See the formula.
That's in fact a rank filter like the median, but instead of taking the median value, you take the max (or min for the erosion). Obviously that the basic definition and it exits faster algorithms, like the one developed in the library I pointed.

Related

How to remove pepper noise from a non-handwritten scanned document(.tif) in matlab

I am unable to find a method to detect the text area in the document and apply a filter to the rest of the image to clear it from any noise. Please refer to this image. If I do apply a filter to the image anyway, the text doesn't remain visible anymore.
Is there an algorithm in MATLAB that can help me find the textarea and treat it separately?
You can greatly reduce the amount of noise by applying erosion, followed by a gaussian blur (σ=2; the image will be converted to grayscale) followed by conversion back to binary.
Erosion alone will reduce the amount of noise significantly:
After application of gaussian blur and re-conversion into binary, the noise will be further reduced. Note that very small text (like the sub-headline) will be also degraded:
All three needed operations seem to be already implemented in MatLab: See Erosion; Gaussian Blur and Thresholding. However, note that for the example automatic (histogram based) thresholding was used to determine the optimal threshold.

use scale space representation to filter one image

Currently I hope to use scale space representation to filter one image. Features in one image can be filtered using an Gaussian smooth filter with one optimal sigma. It means different features in one image can be expressed best in different scale under scale space representation.
For example, I have one image with one tree in it. In the scale space representation, three sigma values are used and they are represented as sigma0, sigma1 and sigma2. The ground is best expressed in the smoothed image with sigma0 because it contains textures mainly. The branches are best expressed in the smoother image with sigma1 and the trunk is with the smoother image with sigma2. If I hope to filter the image, I hope that the filtered pixels for the group is from the smoothed image with sigma0.
The filtered pixels for the branches are from the smoothed image with sigma1. The filtered pixels for the trunk are from the smoothed image with sigma2.
It requires that I need to determine in which smoothed image one pixel is expressed best. Is this idea plausible?
I am trying to use differece-of-Gaussian of two successive smoothed images to perform the above task. Is there any other way to combine the three smoothed image?
I use Matlab to implement the idea. The values of the three sigmas is 1.0, 2.0 and 3.0. The corresponding size of Gaussian kernel is 3, 5 and 7. I use the function fspecial to generate the kernel. Are the parameter reasonable? Please share your experience with the scale space representation to help me. You can provide some links to useful papers.
your idea is very much plausible! You are just one step away from it. I did something very similar once and it looked like this:
After smoothing your images and extracting the edges for each smoothing step (I used a weighted [to compensate for maxima supression after Gauss filtering] Sobel filter for this since DOG was not quite stable for my aplication), you can proyect (and normalize) your whole stack of edge images into a single image ("cummulative edges") which will contain the characteristic edges. You can then compare the cummulative edges image (using cross-correlation or whatever you wish) with every single image in your edge stack, the biggest value of this comparation is then the smooth-scale in which the pixel is expressed the best.
Hope that makes sense for you after reading it a couple of times.
Also don't be afraid of using much bigger kernel sizes, while it all depends on your application, I ended up using things of 51 and bigger!!! (was working with 40MP images though...)
T. Lindeberg has literally dozens of papers related to this problem. I found this one the most useful, but since you are already in the right track, I don't think reading the 50 pages will make you that much smarter. The most important part of it is maybe this one:
Principle for scale selection:
In the absence of other evidence, assume that a scale level, at which some
(possibly non-linear) combination of normalized derivatives assumes a
local maximum over scales, can be treated as reflecting a characteristic
length of a corresponding structure in the data.

MATLAB: Using hough transform to detect circle

I am writing a matlab code that takes in a photo and detects the circular object. For example, the function takes a picture of a peach (circular object) as an input and will return the same image with the peach circled.
Currently, I am using hough transform, utilizing imfindcircles function. However, this function requires me to specify radius range and some sort of sensitivity/threshold value. These values differ for different sizes of image and round objects. So, to get the desired output, I will have to manually change these values for each input image, which is not what I want. I'm going to use this function on 100+ images, so it's impossible for me to do this manually.
My question is is there any way I can make my circular object detection function less manual and possibly completely automatic (does not require me to input any values, just the image)?
Complexity of circle detection
The Hough transform is a voting procedure that requires assumptions be made about the minimum and maximum radii of your circles. Generally speaking using the Randomized Hough Transform for Circles you would pick three-points and then try to form a circle and check if the radius is within the desired range. Running this for a good number of iterations you should find peaks (multiple hits) in your accumulator matrix that represent circles. If you didn't make any assumptions about object size I think it is obvious this method wouldn't work.
Do some routine pre-processing to adjust for contrast and brightness e.g. contrast stretching, histogram equalization. If you might have some noise in the images, then apply bit of gaussian smoothing as well.
Normalizing images this way will reduce inter-image variance and help you with setting thresholds.
the Hough Transform can be used to detect circles, lines, etc.You can refer the demos in Matlab. There are several cases for the application of Hough Transform.

Shape Recognition - counting mangoes

I would like to be able to process a close-up image of a mango tree so that I can identify and count the mangoes. A mango is roughly an oval or ellipse shape that is uniquely different from the leaves and branches in the image. I would like to be able to count mangos that might be 20% covered by other objects (but still obvious to the human eye.) I believe there is an algorithm in MatLab that could do this and I would appreciate any help or suggestions.
I think that the more robust solution for that problem is to segment by color the mangoes from the background (i.e. tree leaves) and count the number of connected components in the resulting binary image. As btown pointed out, you can get the connected components of a binary image by using the bwconncomp and labelmatrix functions.
To segment the mangoes by color, first convert the image to HSV color space and then perform a binarization using the hue component. I believe that the hue component from the mangoes will be different from other parts of the image. This blog post gives some insight on how to do that in Matlab.
Perhaps you could:
Pre process the image (greyscale/threshold etc.).
Extract all the countours/connected components from the binary image.
Calculate the area and perimeter of each contour/connected component.
Calculate the shape factor/roundness using:
Shape Factor – (4 * PI * Area) / (Perimeter^2). This gives an
indication as to the objects shape. Circles have the greatest area to
perimeter ratio and this formula will approach a value of 1 for a
perfect circle. Squares are around 0.78. A thin thread-like object
would have the lowest shape factor approaching 0.
Roundness – (Perimeter^2) / 4 * PI * Area). This gives the
reciprocal value of Shape Factor for those that are used to using it.
A circle will have a value slightly greater than or equal to 1. Other
shapes will increase in value.
So you could approximate a shape factor for an "ideal" mango and see if any of the components lie inside the approximation?
See this for more details.

Remove paper texture pattern from a photograph

I've scanned an old photo with paper texture pattern and I would like to remove the texture as much as possible without lowering the image quality. Is there a way, probably using Image Processing toolbox in MATLAB?
I've tried to apply FFT transformation (using Photoshop plugin), but I couldn't find any clear white spots to be paint over. Probably the pattern is not so regular for this method?
You can see the sample below. If you need the full image I can upload it somewhere.
Unfortunately, you're pretty much stuck in the spatial domain, as the pattern isn't really repetitive enough for Fourier analysis to be of use.
As #Jonas and #michid have pointed out, filtering will help you with a problem like this. With filtering, you face a trade-off between the amount of detail you want to keep and the amount of noise (or unwanted image components) you want to remove. For example, the median filter used by #Jonas removes the paper texture completely (even the round scratch near the bottom edge of the image) but it also removes all texture within the eyes, hair, face and background (although we don't really care about the background so much, it's the foreground that matters). You'll also see a slight decrease in image contrast, which is usually undesirable. This gives the image an artificial look.
Here's how I would handle this problem:
Detect the paper texture pattern:
Apply Gaussian blur to the image (use a large kernel to make sure that all the paper texture information is destroyed
Calculate the image difference between the blurred and original images
EDIT 2 Apply Gaussian blur to the difference image (use a small 3x3 kernel)
Threshold the above pattern using an empirically-determined threshold. This yields a binary image that can be used as a mask.
Use median filtering (as mentioned by #Jonas) to replace only the parts of the image that correspond to the paper pattern.
Paper texture pattern (before thresholding):
You want as little actual image information to be present in the above image. You'll see that you can very faintly make out the edge of the face (this isn't good, but it's the best I have time for). You also want this paper texture image to be as even as possible (so that thresholding gives equal results across the image). Again, the right hand side of the image above is slightly darker, meaning that thresholding it well will be difficult.
Final image:
The result isn't perfect, but it has completely removed the highly-visible paper texture pattern while preserving more high-frequency content than the simpler filtering approaches.
EDIT
The filled-in areas are typically plain-colored and thus stand out a bit if you look at the image very closely. You could also try adding some low-strength zero-mean Gaussian noise to the filled-in areas to make them look more realistic. You'd have to pick the noise variance to match the background. Determining it empirically may be good enough.
Here's the processed image with the noise added:
Note that the parts where the paper pattern was removed are more difficult to see because the added Gaussian noise is masking them. I used the same Gaussian distribution for the entire image but if you want to be more sophisticated you can use different distributions for the face, background, etc.
A median filter can help you a bit:
img = imread('http://i.stack.imgur.com/JzJMS.jpg');
%# convert rgb to grayscale
img = rgb2gray(img);
%# apply median filter
fimg = medfilt2(img,[15 15]);
%# show
imshow(fimg,[])
Note that you may want to pad the image first to avoid edge effects.
EDIT: A smaller filter kernel than [15 15] will preserve image texture better, but will leave more visible traces of the filtering.
Well i have tried out a different approach using Anisotropc diffusion using the 2nd coefficient that operates on wider areas
Here is the output i got:
From what i can See from the Picture, the Noise has a relatively high Frequency Compared to the image itself. So applying a low Pass filter should work. Have a look at the Power spectrum abs(fft(...)) to determine the cutoff Frequency.