64194

Detecting shapes in a “bitmap”

Question:

So,preparing myself for the next ieextreme competition I was going through some past problems.I found one that really troubles me,since I can't figure out what to do.I could probably make it using some bruteforce 300 lines code,but I think that this is not what someone is supposed to do in such competitions,so I need your help!!

<strong>Detecting shapes in a bitmap</strong>

Problem statement: In image analysis, it is common to analyze a bitmap and observe the shapes present in it. For this problem, design an algorithm to detect shapes in a given bitmap. The shapes present in the map shall be from the set Square, Rectangle, Triangle and Parallelogram.

In the bitmap each pixel is represented as a bit, 1 - representing black and 0 - representing white. Participants are expected to detect the shapes outlined in black. Input The first line will contain the size of the bit map in pixels represented as (Row,Column).

E.g. 6,8 this means a bit map of 6 rows and 8 columns. The next line will contain a series of decimal digits from 0 to 255 separated by spaces. Each digit will represent a collection of 8 binary bits in the bitmap. IE. 55 represents a binary pattern 00110111.

Note: There can be multiple shapes in a bitmap and NO shapes shall intersect. However there can be shapes nested with each other without any intersection.

Output The shapes present in the bitmap in ascending order of their names, separated by a comma and a space. Eg. Rectangle, Square, Triangle

Note: There is NO linefeed or space at the end of the output If any shape repeats, the output should contain as many repetitions as in the bitmap. ie. If there are 2 squares and one triangle, the output shall be Square, Square, Triangle

Example Set 1

Input:

6 8

0 126 66 66 126 0

Output: Rectangle

Example Set 2

Input:

6 16

0 0 120 120 72 144 73 32 123 192 0 0

Output: Parallelogram, Square

I have written this code so that I can visualize the bitmap and have 2 lists(bitmap in rows and cols)...

rows,cols=(int(i) for i in raw_input().split()) nums=[int(n) for n in raw_input().split()] mr=[] for i in range(0,len(nums),cols/8): row='' for j in range(i,i+cols/8): b=bin(nums[j])[2:] b='0'*(8-len(b))+b row+=b mr.append(row) mc=[''.join([mr[i][j] for i in range(rows)]) for j in range(cols)]

Thank you for your time...Please answer if you can in Python,C++ or Ruby,since these are the languages I can understand or even algorithmically...

Answer1:

My approach would be something like:

<ol><li>Find the first black pixel (either leftmost-topmost, or topmost-leftmost).</li> <li>Trace the black path right and down (that is, loop until you hit a white pixel).</li> <li>

3 cases:

<ul><li>The paths have same length: square or triangle. Check the pixel right of the bottomleft pixel to decide (black: square, white: triangle).</li> <li>The paths have different length: rectangle or triangle (? or should they always be 45 degrees?). Check the pixel right of the bottomleft pixel to decide (black: rectangle, white: triangle).</li> <li>One of the paths does not exist: triangle or parallelogram. Assuming the path down <em>did</em> exist: check the pixel right of the bottomleft pixel to decide (black: triangle, white: parallelogram).</li> </ul></li> <li>

Remove the shape and repeat.

</li> </ol>

You examine every pixel only a constant number of times (not quite sure about that constant), so this should run in time linear in the number of pixels.

Recommend

  • Restrict mouse movement over a specified window handle
  • onpreviewframe byte[] to int[]
  • AlertDialog style when using setView()
  • cell spacing in div table
  • HTML5 video only works in IE. The other browsers shows the black screen
  • How to split circle in to the sectors in google maps?
  • Primefaces :radioButton inside a ui:repeat
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Django model inheritance, filtering models
  • Java color detection
  • AndEngine Applying Transparancy to AndEngine View
  • Breaking out column by groups in Pandas
  • Unable to get column index with table.getColumn method using custom table Model
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • Android fill_parent issue
  • Change multiple background-images with jQuery
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Using $this when not in object context
  • Android screen density dpi vs ppi
  • JFileChooser in front of fullscreen Swing application
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Perl system calls when running as another user using sudo
  • vba code to select only visible cells in specific column except heading
  • Change an a tag attribute in JavaScript based on screen width
  • When should I choose bucket sort over other sorting algorithms?
  • How do you troubleshoot character encoding problems?
  • Do I've to free mysql result after storing it?
  • Unanticipated behavior
  • Transpose CSV data with awk (pivot transformation)
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Understanding cpu registers
  • Can't mass-assign protected attributes when import data from csv file
  • Sorting a 2D array using the second column C++
  • Unable to use reactive element in my shiny app