I Want to get 2 curve as in Desire result. I tried to use Edge detection technique to get these 2 curves, but the output was not as expected. First step, I convert the original image to grayscale image. In second step, I convert the grayscale image to binary image with threshold calculated by the formula below:
threshold = floor((sum(sum("grayscale image here")))/(2 *high *width));
And then use Sobel edge detection algorithm to find the edges:
im_edge = edge("binary image here", 'sobel');
I remove unwanted edges in left side and right side by just fill it with black.
I got the result in Result but it was not as my expected. The result is also embedded edges found by:
im_edge = edge("grayscale image here", 'sobel');
Can anyone help me to get a better result
Since I don't have 50 reputations to write a comment, I will write my comments here as answer.
The problem you have is that there is no visible edge in your input image. The image is pretty smooth as far as I can see. If you did not put two lines on the image, I won't be able to tell it.
To get better results, you need to get more features such as by applying some transformation on the input image. For example, you can try to find the edge on the gradient of the input image or absolute value of gradient, and see if you can find that two lines better(imgradient).
Related
I'm trying to select overlapping/ touching images from 2 binary images. The first image (blue circles) has the main body and I want to find any green triangles attached to it.
1st Binary Image here (colored for identification)
2nd Binary Image here
FinalImage = BinaryImage1 | BinaryImage2;
**Apply Filter**
Expected result:
Note how the unattached Green triangles are removed, & all blue circles are retained.
Is there a way to do this trick?
This calls for a morphological reconstruction! In morphological reconstruction, you specify a marker and start reconstructing the original image from that marker point using a morphological dilation.
Luckily for us, MATLAB already has a function for that, called imreconstruct in the Image Processing Toolbox, which is called by imreconstruct(marker,image)
To recognize touching, but non-overlapping figures, we can just do a dilation on one of the input image to make touching figures overlap. As structuring element we can e.g. use a 3x3 square, so we also recognize 8-connected figures. After that we use the overlapping points as markers and do the morphological reconstruction using the combined image.
dilatedImage1 = imdilate(binaryImage1, strel('square',3));
finalImage = imreconstruct(dilatedImage1&binaryImage2, dilatedImage1|binaryImage2);
As you write that all circles, i.e. all parts from binaryImage1 should be retained, we can just add binaryImage1 to the result using
finalImage = finalImage | binaryImage1;
For your two example images, this results in:
The circle on the upper right is not connected to any triangle. I don't know how that triangle appeared in your expected result image, but I suppose this is only for demonstration purposes.
Note: I imported the .jpg example images in MATLAB, which lead to ugly borders, so I did a morphological opening on the images first. The borders are still not optimal, but it doesn't look that bad.
I'm trying to make an object recognition program using a k-NN classifier. I've got a bunch of images for the training part of the classifier and a bunch of images to recognize. Those images are in grayscale and there's an object (only its edge) per image. I need to calculate their center of mass so I use
img=im2bw(img)
and then regionprops(img,'centroid').
The problem is that some of those edges aren't closed so regionprops doesn't work then. I tried eroding the image (the edge is black, white background) but the endlines of those edges are too apart from eachother. I tried using bwmorph function to do so but still can't make it work.
Any ideas?
EDIT
I'm adding some images in case anyone wants to try:
Use morphological operation
Use a closing operation to make your structures filled.
1. As first step prepare your image data
im = imread('your image.jpg');
% Get first channel as gray scale information
im = im(:,:,1);
% Threshold it for simplicyty, you may work on grayscale too.
im1 = logical(im > 128);
2. Use a simple block shaped structuring element
The structuring element is defined by:
strel=ones(3,3);
You may use disk shaped elements or whatever gives the best result to you.
3. Apply structuring element a couple of times
Apply the strel a couple of times with an erosion operator to your original image to close your figure:
for i=1:20
im1 = imerode(im1,strel);
end
4. Dilate the image to get back to original shape
Next step is to dilate the image to get back to your original outer shape:
for i=1:20
im1 = imdilate(im1,strel);
end
Final result
The final result should be suitable to get a sufficiently precise center or gravity.
I was working on my image processing problem with detecting coins.
I have some images like this one here:
and wanted to separate the falsely connected coins.
We already tried the watershed method as stated on the MATLAB-Homepage:
the-watershed-transform-strategies-for-image-segmentation.html
especially since the first example is exactly our problem.
But instead we get a somehow very messed up separation as you can see here:
We already extracted the area of the coin using the regionprops Extrema parameter and casting the watershed only on the needed area.
I'd appreciate any help with the problem or even another method of getting it separated.
If you have the Image Processing Toolbox, I can also suggest the Circular Hough Transform through imfindcircles. However, this requires at least version R2012a, so if you don't have it, this won't work.
For the sake of completeness, I'll assume you have it. This is a good method if you want to leave the image untouched. If you don't know what the Hough Transform is, it is a method for finding straight lines in an image. The circular Hough Transform is a special case that aims to find circles in the image.
The added advantage of the circular Hough Transform is that it is able to detect partial circles in an image. This means that those regions in your image that are connected, we can detect them as separate circles. How you'd call imfindcircles is in the following fashion:
[centers,radii] = imfindcircles(A, radiusRange);
A would be your binary image of objects, and radiusRange is a two-element array that specifies the minimum and maximum radii of the circles you want to detect in your image. The outputs are:
centers: A N x 2 array that tells you the (x,y) co-ordinates of each centre of a circle that is detected in the image - x being the column and y being the row.
radii: For each corresponding centre detected, this also gives the radius of each circle detected. This is a N x 1 array.
There are additional parameters to imfindcircles that you may find useful, such as the Sensitivity. A higher sensitivity means that it is able to detect circular shapes that are more non-uniform, such as what you are showing in your image. They aren't perfect circles, but they are round shapes. The default sensitivity is 0.85. I set it to 0.9 to get good results. Also, playing around with your image, I found that the radii ranged from 50 pixels to 150 pixels. Therefore, I did this:
im = im2bw(imread('http://dennlinger.bplaced.net/t06-4.jpg'));
[centers,radii] = imfindcircles(im, [50 150], 'Sensitivity', 0.9);
The first line of code reads in your image directly from StackOverflow. I also convert this to logical or true black and white as the image you uploaded is of type uint8. This image is stored in im. Next, we call imfindcircles in the method that we described.
Now, if we want to visualize the detected circles, simply use imshow to show your image, then use the viscircles to draw the circles in the image.
imshow(im);
viscircles(centers, radii, 'DrawBackgroundCircle', false);
viscircles by default draws the circles with a white background over the contour. I want to disable this because your image has white circles and I don't want to show false contouring. This is what I get with the above code:
Therefore, what you can take away from this is the centers and radii variables. centers will give you the centre of each detected circle while radii will tell you what the radii is for each circle.
Now, if you want to simulate what regionprops is doing, we can iterate through all of the detected circles and physically draw them onto a 2D map where each circle would be labeled by an ID number. As such, we can do something like this:
[X,Y] = meshgrid(1:size(im,2), 1:size(im,1));
IDs = zeros(size(im));
for idx = 1 : numel(radii)
r = radii(idx);
cen = centers(idx,:);
loc = (X - cen(1)).^2 + (Y - cen(2)).^2 <= r^2;
IDs(loc) = idx;
end
We first define a rectangular grid of points using meshgrid and initialize an IDs array of all zeroes that is the same size as the image. Next, for each pair of radii and centres for each circle, we define a circle that is centered at this point that extends out for the given radius. We then use these as locations into the IDs array and set it to a unique ID for that particular circle. The result of IDs will be that which resembles the output of bwlabel. As such, if you want to extract the locations of where the idx circle is, you would do:
cir = IDs == idx;
For demonstration purposes, this is what the IDs array looks like once we scale the IDs such that it fits within a [0-255] range for visibility:
imshow(IDs, []);
Therefore, each shaded circle of a different shade of gray denotes a unique circle that was detected with imfindcircles.
However, the shades of gray are probably a bit ambiguous for certain coins as this blends into the background. Another way that we could visualize this is to apply a different colour map to the IDs array. We can try using the cool colour map, with the total number of colours to be the number of unique circles + 1 for the background. Therefore, we can do something like this:
cmap = cool(numel(radii) + 1);
RGB = ind2rgb(IDs, cmap);
imshow(RGB);
The above code will create a colour map such that each circle gets mapped to a unique colour in the cool colour map. The next line applies a mapping where each ID gets associated with a colour with ind2rgb and we finally show the image.
This is what we get:
Edit: the following solution is more adequate to scenarios where one does not require fitting the exact circumferences, although simple heuristics could be used to approximate the radii of the coins in the original image based on the centers found in the eroded one.
Assuming you have access to the Image Processing toolbox, try imerode on your original black and white image. It will apply an erosion morphological operator to your image. In fact, the Matlab webpage with the documentation of that function has an example strikingly similar to your problem/image and they use a disk structure.
Run the following code (based on the example linked above) assuming the image you submitted is called ima.jpg and is local to the code:
ima=imread('ima.jpg');
se = strel('disk',50);
eroded = imerode(ima,se);
imshow(eroded)
and you will see the image that follows as output. After you do this, you can use bwlabel to label the connected components and compute whatever properties you may want, for example, count the number of coins or detect their centers.
I'm doing some low level image processing project and I need to locate the position of an object. In this case it's a comet. I played a bit with the thresholds and got a pretty much binary image, but what I need next is to locate a fixed point in the middle of the large group of white pixels so it can be traced or something. Any idea on how to do that?
Here's a screen shot of what I got.
Link to the photo
And is there a way to filter out all the white dots all over the picture? Some kind of function to give all small groups value of "0"?
Thanks in advance!
Maybe you are looking for this -
Code
%%// Read in image and convert to binary
img = imread(IMAGE_FILEPATH);
BW = im2bw(img);
%%// Get only the comet blob, which is the biggest blob
[L, num] = bwlabel(BW);
counts = sum(bsxfun(#eq,L(:),1:num));
[~,ind] = max(counts);
BW = (L==ind);
%%// Find the centroid of the comet blob
stats = regionprops(BW, 'Centroid');
center_point = stats.Centroid
Output
center_point =
56.7471 131.9373
Note: There's another question - Select largest object in an image that relates to this case.
MATLAB regionprops is your friend, and your image is good enough for what you want to do.
regionprops does just what you need (you get the X,Y of the center of mass). Since it also gives the area, you can filter results and only retain the largest object in your image.
Your problem seems to be solved with regionprops, as others have proposed already, but if you're interested, in general, in eliminating small white artefacts in an image:
You could apply one or multiple steps of erosion to your binary image, eliminating small white spots (but also decreasing the size of your comet!).
you can use a median filter or a morphological opening filter to remove small white regions. you'll have to experiment and pick the right size for the filter kernel. assuming you have a cleaner image after filtering, you can perform a labeling and then do a regionprops to get the centroid of the blob.
I'm using the MNIST digit images for a machine learning experiment, and I'm trying to center each image based on position, rather than the center of mass that they are centered on by default.
I'm using the regionprops class, BoundingBox method to extract the images. I create a B&W copy of the greyscale, use this to determine the BoundingBox properties (regionprops works only B&W images) and then apply that on the greyscale original to extract the precise image rectangle. This works fine on ~98% of the images.
The problem I have is that the other ~2% of images has some kind of noise or errant pixel in the upper left corner, and I end up extracting only that pixel, with the rest of the image discarded.
How can I incorporate all elements of the image into a single rectangle?
EDIT: Further research has made me realise that I can summarise and rephrase this question as "How do I find the bounding box for all regions?". I've tried adjusting a label matrix so that all regions are the same label, to no avail.
You can use an erosion mask with the same size of that noise to make it totally disappear " using imerode followed by imdilate to inverse erosion ", or you can use median filter