Thursday, November 3, 2011

Shape Recognition in MATLAB

This post is about something I contributed to a larger Neural Networks project.This isnt a proper tutorial on image processing,and the code discussed is also specific.And in my humble opinion,MATLAB isnt the best choice for Image Processing(OpenCV would be the way to go) anyway.Here it was the only choice since rest of the project was implemented in it.

The idea was to read an RGB Image which has the following characteristics:
-- 400x400px
-- RGB
-- has only one shape,any of a Triangle,Rectangle,Pentagon or a Hexagon,with a solid color fill.
-- has a colored background.

The project required my code to output:
-- The Identity of the shape
-- 2 Images,each of 400x400 px and each with a solid color fill,where one would be of the Shape's fill color and the other of the background color.

So heres my function .m file,all commented.It isnt very clean,and probably not very optimal too,processing wise.Its also not robust(then what use is it? :-/ ) so do tweak it for your specific application.If you spot any areas of improvement,do mention it in the comments below.

I'll briefly explain the shape characterization part,since the rest is pretty much straightforward and already commented.
I've used the Extrema property of the regionprops function and used the relative positions of the various extrem(a)e vertices to identify the shape.See the picture below for a better idea.

So,if we check for the equality(actually closeness of points.Since the left and right points detected will differ by a few pixels,even though the image is perfect.) of the extrema vertices ( whose co-ordinates are stored in the 8x2 Extrema matrix returned by regionprops) we can characterize a shape.

Heres the hexagon case:

Similarly,we can also see what extreme vertices are on the triangle,rectangle and other polygons.

Heres a link to the m-file (hosted on Google Docs)

Drop a line in the comments for any clarification or query.

PS:You can see the output of the different operations on the image using the imshow function.

No comments:

Post a Comment