#!/usr/bin/env python # coding: utf-8 # # 201A HW3 # # Find the right rectangle in different-size rectangles. # ## In this project, I firstly found a image consisting of 100 rectangles of different sizes. And then I crop the orginal image to get images that have only one single rectangle. # ## My homework is to test if the autocorelation between the partial image and the original one can recognizing the selected rectangle's location in the original image. # In[135]: get_ipython().run_line_magic('pylab', 'inline') rcParams['figure.figsize'] = (10, 4) #wide graphs by default from __future__ import print_function from __future__ import division from scipy.signal import correlate2d # ### This is the original image. It is made up of rectangles of different size. # In[97]: i = imread('22.png') i = sum(i.astype(float), axis=2)/4.0 i = where(i > 0.9, -1, 1) imshow(i, cmap='gray') # ### 1. The first partial image. This triangle is at 2nd row and 2nd column. # In[136]: o = imread('7.png') o = o.astype(float).sum(axis=-1)/3 o = where(o > 0.9, -1, 1) imshow(o, cmap=cm.gray, interpolation='nearest') # In[137]: from scipy.signal import correlate2d from scipy.ndimage.filters import maximum_filter cc = correlate2d(i, o) imshow(cc, cmap = 'gray') colorbar() gcf().set_figheight(8) # ### Here is the first result. As we can see from the first black and white image. Those white spots indicate the 4 possible locations of the selected rectangle. The location is not unique because there are three more rectangle having similar size of the selected one, which are in the 4th row, 2nd column(4,2), (5,1) and (5,5). When we look into the original image, our conclusion are proven to be right. # In[142]: subplot(131) imshow(where(cc > 1000, 1, 0), interpolation='nearest', cmap=cm.gray) subplot(132) imshow(i, cmap=cm.gray) subplot(133) imshow(maximum_filter(cc, (10,10))) # ### 2. This is the second partial image. It's intentionally selected to be the largest rectangle. # In[145]: o = imread('6.png') o = o.astype(float).sum(axis=-1)/3 o = where(o > 0.9, -1, 1) imshow(o, cmap=cm.gray, interpolation='nearest') # In[146]: from scipy.signal import correlate2d cc = correlate2d(i, o) imshow(cc, cmap = 'gray') colorbar() gcf().set_figheight(8) # ### Similar to the conclusion above, only the locations of larger rectangles can be the possible locations. # In[147]: subplot(131) imshow(where(cc > 950, 1, 0), interpolation='nearest', cmap=cm.gray) subplot(132) imshow(i, cmap=cm.gray) subplot(133) imshow(maximum_filter(cc, (10,10))) # ### 3. The third partial image. I intentionally choose a middle-size rectangle. # In[125]: o = imread('3.png') o = o.astype(float).sum(axis=-1)/3 o = where(o > 0.9, -1, 1) imshow(o, cmap=cm.gray, interpolation='nearest') # In[126]: cc = correlate2d(i, o) imshow(cc, cmap = 'gray') colorbar() gcf().set_figheight(8) # ### Because many rectangles in the original image have exactly the same size as this selected rectangle, many white spots, i.e. the possible locations, are revealed. # In[130]: subplot(131) imshow(where(cc > 800, 1, 0), interpolation='nearest', cmap=cm.gray) subplot(132) imshow(i, cmap=cm.gray) subplot(133) imshow(maximum_filter(cc, (10,10))) # ### 4. The 4th partial image selects the (2,5) rectangle, which has a unique size. # In[131]: o = imread('4.png') o = o.astype(float).sum(axis=-1)/3 o = where(o > 0.9, -1, 1) imshow(o, cmap=cm.gray, interpolation='nearest') # In[132]: cc = correlate2d(i, o) imshow(cc, cmap = 'gray') colorbar() gcf().set_figheight(8) # ### Finally, it turns out to be only one white spot appearing. Additionally, from the right image, we can find many orange blocks and one extremely red block. Those orange blocks are highly possible because the rectangles at those locations are only slightly smaller than the selected one. However, the exactly matched result turns to be the red block, which stands out from the organge ones. # In[133]: subplot(131) imshow(where(cc > 800, 1, 0), interpolation='nearest', cmap=cm.gray) subplot(132) imshow(i, cmap=cm.gray) subplot(133) imshow(maximum_filter(cc, (10,10)))