I am trying to train a haar cascade classifier so that it can detect trees in an image. After detection the classifier is expected to draw a bounding rectangle around the object. I need to now segment out this region that is enclosed within the bounding box. I would like to know if this is a possible way for segmentation of tree from its background or not.
It is definitly possible to segment out the area enclosed with the boundings of the box, just write Mat <new name> = name ( <vector>[] )
Where -
Name - the name of your original frame which a detection is running on.
New name - the cropped area name.
Vector - the vector of detected object.
It worked for me very well, I hope it is useful for you too.
Related
I'm currently working on MRI images and each dataset consists of a series of images. All I need to do is to segment part of the moving image(s), based on details a from fixed image provided, strictly by using the image registration method.
I have tried some of the available code and done some tweaking but all I got was a warped transformation moving image based on features from the fixed image, which was correct but wasn't as I expected.
To help with the idea, here are some of those MRI images1:
Fixed image:
Moving image:
The plan is to segment only total area (quadriceps, inner and outer bone sections) of the moving image as per details from the fixed image, i.e. morphologically warp the boundary of moving image according to fixed image boundary.
Any idea/suggestions as to how this could be done?
1. As a new user I'm unable to post/attach more than 2 links/images but do let me know should you need further images.
'All I need to do is to segment part of the moving image/s', this is certainly not a trivial thing to do. It is called segmentation by deformable models, and there is a lot of literature on the subject. Also, your fixed image is very different from the moved image, which doesn't help.
Here are a couple of ideas to start, but you will probably need to go into more details for your application.
I1=imread('fixed.png');
I2=imread('moving.png');
model=im2bw(I1,0.54);
imshowpair(I1,Model);
This is a simple thresholding segmentation to isolate that blob in the middle of the image. The value 0.54 was obtained by fiddling, you can certainly do a better job at segmenting your fixed image.
Here is the segmented fixed image, purple is inside, green is outside.
Now, let's deform this mask to fit the moved image:
masked = activecontour(I2,model, 20, 'Chan-Vese');
imshowpair(I2,masked);
Result:
You can automatize this in a loop along all your images, deforming each subsequent mask to the next frame. Try different parameters of activecontour as well.
Edit here is another way I can think of:
In the following code, Istart is the original fixed image, Mask is the segmented region on that image (the one you called 'fixed' in your question) and Istep is the moved image.
I first turned the segmented region into a binary mask, this is not strictly necessary:
t=graythresh(Mask);
BWmask=im2bw(Mask, t);
Let's display the masked original image:
imshowpair(BWmask, Istart)
The next step was to compute intensity-based registration between the start and step images:
[optimizer, metric] = imregconfig('monomodal');
optimizer.MaximumIterations = 300;
Tform=imregtform(Istart, Istep, 'affine', optimizer, metric);
And warp the mask according to this transformation:
WarpedMask=imwarp(BWmask, Tform, 'bicubic', 'Outputview', imref2d(size(Istart)));
Now let's have a look at the result:
imshowpair(WarpedMask, Istep);
It's not perfect, but it is a start. I think your main issue is that your mask contains elements that are different from each other (that middle blob vs. the darker soft tissue in the middle) If I where you, I would try to segment these structures separately.
Good luck!
I need to programmatically determine the best place to overlay text on an image. In other words, I need to tell the foreground from the background. I have tried imagemagick: http://www.imagemagick.org/Usage/scripts/bg_removal. Unfortunately this was not good enough. The images can be photographs of pretty much anything, but usually with a blurry background.
I would now like to try liuliu's CCV. Code: https://github.com/liuliu/ccv, Demo: http://liuliu.me/ccv/js/nss/
The demo uses what looks like a json haar cascade to detect faces: https://github.com/liuliu/ccv/blob/unstable/js/face.js
How do I:
1. Convert the xml haar cascade files to be able to be used with CCV
2. Generate the best cascade for my goal (text placement on an image)
3. Find any documentation for CCV
AND, finally, is there a better way to approche this problem?
EDIT: I've asked the border question here: https://stackoverflow.com/questions/10559262/programmatically-place-text-in-an-image
Convert the xml haar cascade files to be able to be used with CCV
Generate the best cascade for my goal (text placement on an image)
Find any documentation for CCV
I have no idea about 1)
(Anyway, which XML files? I guess some from opencv?)
or 3),
but here is my take on 2)
To make a haar cascade a lá viola&jones, you need a series of small training images that contain only your desired objects, for example faces.
One object per image, with as little background as possible, all in the same orientation and size, normalized so they all have the same average brightness and variance in brightness. You will need a lot of training images.
You also need a series of negative training images, same size/brightness etc as the positive examples, that contain only background.
However, I doubt that this approach will work for you at all:
Haar filters work by recognizing common rectangular light/dark structures in all your foreground objects.
So your desired foreground images need to have a common structure.
An example haar filter cascade works like this (extremely simplified):
is the rectangular region at x1,y1 darker than the region at x2,y2? if no --> not a face, if yes --> continue
is the region at x3,y3 darker than the region at x4,y4? if no --> not a face --> if yes, continue
and so on ....
(To find the position of a face in a larger image, you execute this filter for every possible position in the image. The filter cascade is very fast in rejecting non-faces, so this is doable.)
So your foreground objects need to have a common pattern among them.
For faces, the eye region is darker than the cheek region, and the mouth is darker than the chin, and so on.
The same filter for faces will cease to work if you just rotate the faces.
You cannot build a good filter for both trees and faces, and you definitely cannot build one for general foreground objects. They have no such common structure among them. You would need a separate filter for each possible type of object, so unless your pictures only show a very limited number of types this will not work
I have applied watershed segmentation algorithm on occluded leaves and found out the region of interest (ROI) for a single leaf. The regions have been labeled with different colors, as shown in the image given below. Now the main task is to extract the region of the original with the color having the largest region i.e. the sky blue region. Please provide me the MatLAB code. Thank you for the help.
ORIGINAL IMAGE
SEGMENTED IMAGE
SEGMENTED IMAGE SUPERIMPOSED ON ORIGINAL IMAGE
The regionprops function accepts a label matrix. See doc regionprops for all the various properties which can be extracted with this useful function.
props = regionprops(labelmatrix)
[~,ind] = max([props.Area]);
imshow(labelmatrix == ind);
You should take a look at all the areas (areas = [props.Area];) and make sure the largest one is always the one you want.
I want to eliminate a specific region in my image using MATLAB. For this, i converted my image into binary. now both the wanted and unwanted (region to be removed) exhibits same spatial characteristics (when i used regionprops() function). The only difference is the position of the regions in the image. Is there any command to do this job...
Eliminating the region means setting the pixel intensity to 0 in the specific region
If you have a mask that is (e.g.) TRUE where you want to remove the region, you can do:
myImage[myMask] = 0;
If it's TRUE where you want to keep the region, you can do:
myImage[~myMask] = 0;
Is that what you mean? (Update your question with a small amount of code reproducing your problem and we'll be able to tailor our answers better).
I'm searching for an certain object in my photograph:
Object: Outline of a rectangle with an X in the middle. It looks like a rectangular checkbox. That's all. So, no fill, just lines. The rectangle will have the same ratios of length to width but it could be any size or any rotation in the photograph.
I've looked a whole bunch of image recognition approaches. But I'm trying to determine the best for this specific task. Most importantly, the object is made of lines and is not a filled shape. Also, there is no perspective distortion, so the rectangular object will always have right angles in the photograph.
Any ideas? I'm hoping for something that I can implement fairly easily.
Thanks all.
You could try using a corner detector (e.g. Harris) to find the corners of the box, the ends and the intersection of the X. That simplifies the problem to finding points in the right configuration.
Edit (response to comment):
I'm assuming you can find the corner points in your image, the 4 corners of the rectangle, the 4 line endings of the X and the center of the X, plus a few other corners in the image due to noise or objects in the background. That simplifies the problem to finding a set of 9 points in the right configuration, out of a given set of points.
My first try would be to look at each corner point A. Then I'd iterate over the points B close to A. Now if I assume that (e.g.) A is the upper left corner of the rectangle and B is the lower right corner, I can easily calculate, where I would expect the other corner points to be in the image. I'd use some nearest-neighbor search (or a library like FLANN) to see if there are corners where I'd expect them. If I can find a set of points that matches these expected positions, I know where the symbol would be, if it is present in the image.
You have to try if that is good enough for your application. If you have too many false positives (sets of corners of other objects that accidentially form a rectangle + X), you could check if there are lines (i.e. high contrast in the right direction) where you would expect them. And you could check if there is low contrast where there are no lines in the pattern. This should be relatively straightforward once you know the points in the image that correspond to the corners/line endings in the object you're looking for.
I'd suggest the Generalized Hough Transform. It seems you have a fairly simple, fixed shape. The generalized Hough transform should be able to detect that shape at any rotation or scale in the image. You many need to threshold the original image, or pre-process it in some way for this method to be useful though.
You can use local features to identify the object in image. Feature detection wiki
For example, you can calculate features on some referent image which contains only the object you're looking for and save the results, let's say, to a plain text file. After that you can search for the object just by comparing newly calculated features (on images with some complex scenes containing the object) with the referent ones.
Here's some good resource on local features:
Local Invariant Feature Detectors: A Survey