Answered step by step
Verified Expert Solution
Question
1 Approved Answer
have to make the function quadrants in python and solid zero Quadtrees! First the gratuitous backstory! You've been hired as a summer intern at SASA
have to make the function quadrants in python
and solid zero
Quadtrees! First the gratuitous backstory! You've been hired as a summer intern at SASA (the Shmorbodian Air and Space Administration) and assigned to the image- processing group. SASA's deep space probes capture black-and-white images and must send those images back to Earth. The amount of power required to send an image is proportional to the length of the encoding of the image, so SASA wants to compress those images as much as possible. In addition, those images sometimes need to be processed (e.g., rotated, flipped, etc.). Your job is to develop software based on quadtrees to compress and manipulate binary (black-and- white) images. Your colleagues at SASA have decided that quadtrees are the way to go-they tend to compress images very well and they allow for fast manipulation of those images. In this problem, you'll add code to the hw4pr3.py starter file that we've provided. (It's best to right-click-which is CTRL-click on the Mac-and download this file rather than cutting and pasting into an editor.) In the hw4pr 3.py file, you'll see a global string variable test 1. This string has length 64 and represents an 8x8 binary image. This is one of two ways that we'll use to create a binary image. The second way is to load an existing.png file; more on that later.) Once you've loaded hw4pr3.py into Python, try this: >>> testi 0000001100001100000000110000001111111111111111111111111111111111' >>> array1 = stringToArray(testi) >>> array1 [[0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1], [1, 1, 1, >>> renderASCII (array1) >>> render Image(array1) Notice that the function stringToArray (string) accepts a string of O's and 1's like the test string above and returns a 2-dimensional array representation of that image. Try it! The renderASCII(array) function accepts a 2D array input and displays the image as blanks and asterisks on the screen. Finally, render Image (array) accepts a 2D representation of the image and opens a matplotlib window rendering of the image. You'll need to close the image window before Python is willing to continue. (NOTE: if render Image doesn't generate a window on your computer, it's probably trying to use the wrong "back end". No worries! Just use render ASCII instead, or ask a professor for help in convincing it to use a different back end.) quadrants (array) It will bs useful later to have a function that takes a 2D array and returns a list of four arrays, one for each of the northwest, northeast, southwest, and southeast quadrants, in that order. Here's an example of this function in action. About seven lines of code suffice. Using higher-order functions or list comprehensions will keep this short and sweet! >>> NW, NE, SW, SE - quadrants (array1) Nice syntax! >>> NW 110, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0] >>> NE 110, 0, 1, 1), 11, 1, 0, 0], [0, 0, 1, 11, 10, 0, 1, 11] >>> renderASCII (NE) >>> SW 111, 1, 1, 1), 11, 1, 1, 1], [1, 1, 1, 1), (1, 1, 1, 1]] >>> SE [[1, 1, 1, 1), 11, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Recall the convenient syntax used here: quadrants(array) returns a list of four arrays, and the first line above gives names to each of the four elements in that list. # A sample 8x8 image represented as a sequence of 64 bits test1 = "0000001100001100000000110000001111111111111111111111111111111111" def powerOfTwo (num): # CODE SUPPLIED BY INSTRUCTOR. "Accepts a positive integer and returns True if and only if it is a power of two. if num == 1: return True elif num % 2 == 1: return false else: return powerOfTwo(num//2) def pngToArray(filename, threshold = 2): # CODE SUPPLIED BY INSTRUCTOR Accepts the name of a file that contains a .png image, and returns a binary 2D array as output. The image must be a square whose dimensions are a power of two. Pixels whose total brightness are less than the threshold will be rendered as black; all others will become white." img = mpimg.imread(filename) # Read the image dimensions = img.shape # Get the dimensions rows = dimensions (0) # Extract the number of rows... columns = dimensions (1) # ... and columns # Insist on appropriate dimensions if rows != columns or not powerof Two rows): return None if rows != columns or not poweroftwo rows): return None image = 0 # This will become the final image for r in range (rows): row = ] for c in range(columns): if sum( img[r][c]) >= threshold: row.append() else: row.append(1) image.append( row) return image def renderASCII(array): # CODE SUPPLIED BY INSTRUCTOR "Accepts a 2D array of O's and its and renders it on the screen in ASCII, using spaces and asterisks to represent O's and i's, respectively." for row in array: row = list(map(lambda x: '' if x == @ else '*', row)) stringify = reduce(lambda x, y: x + y, row) print(stringify) def renderImage(array): # CODE SUPPLIED BY INSTRUCTOR "Accepts a 2D array of 's and l's and renders it on the screen using matplotub." dim = len(array) imagenn.zeros dim dim ditunen .float dim = len(array) image = np.zeros((dim, dim), dtype = np. float) for r in range(dim): for c in range( dim): image(r) (c) = float(array(r][c]) plt.imshowl image, cmap = "Greys", interpolation = 'nearest') plt.show() def stringToArray(bstring): # CODE SUPPLIED BY INSTRUCTOR Accepts a binary string input and returns its 2D array representation as an image." dim = int(math.sqrt(len(string))) charArray = [list(bstring1:1+dim)) for i in range(0, len(string), dim) array = [ (int(x) for x in row) for row in charArray 1 return array def quadrants(array): Accepts an array of bits and returns a list of quadrants of the form (NW, NE, SW, SE] where each entry is the array for that quadrant." return quads def solidZero(array): Accepts a 2D binary array and returns True if every bit is a and False otherwise." # You'll write this code. One line suffices Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started