MATLAB - Filling in the empty region of an ellipse/skull shape? - matlab

I have been attempting to fill in a binary image in Matlab so that I am left with the entirety of this oval-like image like this.
However, I have been running into an issue in actually being able to define the red region. I have tried the following:
Using the bwconvhull function to fill the shape accurately, but then I do not know how to get rid of the inner shape to isolate just the red region.
I have also attempted to trace the boundary of the binary region but to no avail. I am not entirely sure what to do after tracing the boundary. I have attempted to trace just the inner boundary, but the bwtraceboundary function simply follows the entirety of the borders (on the inside and outside of the skull).
Are there any similar functions to bwconvhull where I am able to expand a region from the center outward? My major difficulties have been in isolating either (a) the inner boundary of the skull or (b) the inner "black" region where the brain should be. My coding attempts can be found below:
Issue (a) - Tracing boundaries
hole=imread('Copy CT.jpg');
BW=im2bw(hole,.9);
dim=size(BW);
col=round(dim(2)/2);
row=min(find(BW(:,col)));
boundary = bwtraceboundary(BW,[row,col],'S');
x=boundary(:,2);
y=boundary(:,1);
Issue (b) - Isolating only the center
hole=imread('Copy CT.jpg');
BW=im2bw(hole,.9);
CH=bwconvhull(BW);
KH=CH-BW;
KH2=bwareaopen(KH,200);
Are there any particular functions that would be worth trying, or would there be another way to isolate the center of the circle so I can only highlight the red region? Any insight would be greatly appreciated!

I would approach this with these steps:
apply an edge detection filter so you end up with two ellipse-ish shaped parts: an inner and outer ellipse.
apply an algorithmic ellipse-fit to the inner ellipse. There are some good examples out there, but I don't have one on me.
subtract the bwconvhull boundary with the inner ellipse.
subtract all parts of your new oval that overlap with the white portions of the original image.
I am sorry I don't have actual code to back up this approach, but this will get you pretty close. You may need more steps to clean up the final result.

Related

Blender - Intersection between 2 objects deletes part of my first object

I'm making a chess piece (a bishop) and I am trying to make the top notch.
For this purpose, I made a new cube, resized it and put on the place to make the cut.
I want to make a cut using a modifier: Boolean, intersecting the two objects.
The problem I am facing is that while intersecting, the top UV Sphere that simulates the 'hat' of the bishop disappears.
What I did so far:
- Remove Doubles
- CTRL+J to join Bishop+Hat(UV Sphere) to make 1 component
Nothing helped and when trying to intersect, the UVSphere-hat disappears.
Why? How to solve?
Here is the bishop before modifier, with hat:
Here is the .blend file to catch the problem faster:
Thank you for your help :-)
The boolean modifier offers two different solvers that produce different results. You want the intersect operation with the carve solver. You also want to hide the cube that you are using for the intersection otherwise you won't see the hole that it has cut out.
Just to go straight to the point I'll add the reply here and select sambler's excellent answer as right.
In my case the Cube which was going to intersect with the bishop HAD NEGATIVE SCALE.
If you have similar problems, check if your objects scale/parameters have negative parameters

Matlab color detection

I'm trying to consistently detect a certain color between images of the same scene. The idea is to recognize a set of object based on a color profile. So, for instance, if I'm given a scene with a green ball in it and I select that green as part of my color palette, I would like a function which has a matrix reflecting that it detects the ball.
Can anyone recommend some matlab functions/plugins/starting points for this project? ideally the function for color recognition will take an array of color values and will match them within a certain threshold.
Kinda like this:
http://www.mathworks.com/matlabcentral/fileexchange/18440-color-detection-using-hsv-color-space-training-and-testing
except it works (this one didn't)
Update:
Here's why I chose not to use the above toolkit..
I start by selecting some colors of interest in the picture
and then ask the function to recognize the road in later images...
And absolutely nothing useful is triggered. So yeah, apart from the few bugs that I came across in the code on download and fixed, this was kind of the kicker. I didn't try to fix the body of the code that recognizes the colors because.. well, I don't know how, which is why I came here.
So, let me just start off by saying road detection with color profiles is a pathological problem. But if the color of the roads are consistent, and the lighting doesn't change the color of the object you are trying to recognize then you might have a shot. (this will be extremely difficult if this is taken outside, or with different cameras, or if shadows happen, or it taking place in any sort of real-world environment)
Here are a few things that might help.
Try smoothing the image beforehand, the reason you get the bad results in the first images is probably because of small pixel variations in the road. If you can blur them, or use some sort of watershed or local averaging, you might get regions with more consistent color.
You might also consider using the LAB color space instead of HSV or RGB.
Using edge detection (see matlab's canny edge detector) might be able to get you some boundary information. If you are looking for a smooth object, there will not be very many edges in it.
Edit: I tried to adhere to this advice in the most simplistic way. Here are the resulting code and a few samples.
im=rgb2gray(im) %for most basic color capturing.. using another color space is better practice
%imshow(im)
RoadMask=roipoly(im)%create mask
RoadMask=uint8(RoadMask);%cast to so you can elementwise multiply
im=im.*RoadMask;%apply mask
[x y]=size(im);
for i=1:x
for j=1:y
%disp('here')
if (im(i,j)<160 || im(i,j)>180) %select your values based on your targets range
im(i,j)=0; %replace everything outside of range with 0
%disp(im(x,y)) %if you'd like to count pixels, turn all values
end %within range to 1 and do a sum at end
end
end
First converted from RGB to grayscale
selected a region that generally matched the roads grayness
Notice parts of the road are not captured and the blocky edges. such as this -------------^
This implementation was quicky and dirty, but I wanted to put it up before I forgot. I'll try to update with code that implements smoothing, sampling, and the LAB color space.

Identify different shapes drawn using UIBezierPath?

I am able to draw shapes using the UIBezierPath object. Now I want to identify different shapes drawn using this eg. Rectangle , Square , Triangle , Circle etc. Then next thing I want to do is that user should be able to select a particular shape and should be able to move the whole shape to different location on the screen. The actual requirement is even more complex , but If I could make this much then I can work out on the rest.
Any suggestion or links or points on how do I start with this is welcome . I am thinking of writing a separate view to handle every shape but not getting how do I do that..
Thank You all in advance !!
I recommend David Gelphman’s Programming with Quartz.
In his chapter “Drawing with Paths” he has a section on “Path Construction Primitives” which provides a crossroads:
If you use CGContextAddLineToPoint your user could make straight lines defined by known Cartesian points. You would use basic math to deduce the geometric shapes defined by those points.
If you use CGContextAddCurveToPoint your user could make curved lines defined by known points, and I’m pretty sure that those lines would run through the points, so you could still use basic math to determine at least an approximation of the types of shapes formed.
But if you use CGContextAddQuadCurveToPoint, the points define a framework outside of the drawn curve. You’d need more advanced math to determine the shapes formed by curves along tangents.
Gelphman also discusses “Path Utility Functions,” like getting a bounding box and checking whether a given point is inside the path.
As for moving the completed paths, I think you would use CGContextTranslateCTM.

Detect Eyes Using Matlab

I have tryed hough circle algo but it give not acceptable out put of detecting
eyes . Can we find way to get the eyes detect in image I having face front ...
Try this implementation from Oxford's Visual Geometry Group. Their pipeline consists of a Viola-Jones face detector at the front and then 9 face landmarks including inner and outer corners of both eyes, center/edge of nostrils, and mouth corners.
I've used this with some success in the past. If you want an exact eye corner, then some additional refinement may be necessary as this includes a model that tries to make a best fit for the entire face.

Problem drawing a polygon on data clusters in MATLAB

I have some data points which I have devided into them into some clusters with some clustering algorithms as the picture below:(it might takes some time for the image to appear)
alt text http://www.freeimagehosting.net/uploads/05a807bc42.png
Each color represents different cluster. I have to draw polygons around each cluster. I use convhull for this reason. But as you can see the polygon for the red cluster is very big and covers a lot of areas, which is not the one I am looking for. I need to draw lines(ploygons) exactly around my data sets. For example in the picture above I want a polygon that is drawn exactly the same(and around) as the red cluster with the 3 branches. In other words, in this case I need a polygon with 3 branches to cover my red clusters not that big polygon that covers the whole area. Can anyone help me with this?
Please Note that the solution should be general, because the clusters will change in each run of the algorithm, so it needs to be in a way that is general.
I am not sure this is a fully specified question. I see this variants on this question come up quite often.
Why this can not really be answered here: Imagine six points, three in an equilateral triangle with another three in an equilateral triangle inside it in the same orientation.
What is the correct hull around this? Is it just the convex hull? Is it the inner triangle with three line spurs coming out from it? Does it matter what the relative sizes of the triangles are? Should you have to specify that parameter then?
If your clusters are very compact, you could try the following:
Create a grid, say with a spacing of 0.1.
Set every pixel in the grid to 1 if there's at least one data point covering it, set the pixel to 0 if there is no data point covering the pixel.
You may need to run imclose on your mask in order to fill little holes inside that have not been colored due to sheer bad luck.
Extract the border pixels using, e.g. bwperim. This is the outline of the polygon you're looking for.