Question

In: Computer Science

compress(image) Description: It compresses a grayscale image and returns a data structure (list of lists) that...

compress(image)

Description:

It compresses a grayscale image and returns a data structure (list of lists) that holds a summary of the content, based on the following algorithm: First, we construct from the original image a black and white bitmap – values less than 128 are mapped to black and the rest are mapped to white. Then, we encode each row of this bitmap as a list containing the lengths of the alternating black and white sequences. Last, we package all these lists in an outer list. You can think of the encoding of each row as the process of counting the length of the consecutive black and white pixels (starting always with the black pixels) and adding these values in a list in the same order as we encounter the alternating black and white sequences. Obviously, each row, depending on its content, will have a different number of alternating sequences, so the full data structure will most probably be a list of variable-size sublists.

Parameters: image (2D list of int) is the grayscale image

Return value: A list of lists that summarizes the image

Example:

compress([[100,170,200,111,250,128],

     [223,244,255,115,127,105],

                   [100,100,100,120,120,120]]) → [ [1, 2, 1, 2], [0, 3, 3], [6] ]

Solutions

Expert Solution

 

Hi. I have answered this question before. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

''' required method '''
def compress(image):
    #creating a list to store the result
    summary=[]
    #looping through each row
    for row in image:
        #initializing count to 0
        count=0
        #currently processing black pixels
        black=True
        #list to store the consecutive black, white pixels in current row
        current_summary=[]
        # looping through each column in current row
        for col in row:
            #if col<128, it is counted as black, else white
            if col<128:
                #checking if we are already processing black
                if black:
                    #simply incrementing count
                    count+=1
                else:
                    #otherwise append current count to current_summary
                    current_summary.append(count)
                    #resetting count to 1
                    count=1
                    #setting black to True
                    black=True
            else: #white
                #if we are already processing white, incrementing count
                if not black:
                    count+=1
                else:
                    #otherwise appending count to current_summary
                    current_summary.append(count)
                    #updating count
                    count=1
                    #setting black to False
                    black=False
        #at the end, if count is above 0, appending to current_summary
        if count>0:
            current_summary.append(count)
        #appending current_summary to summary list
        summary.append(current_summary)
    #returning the list
    return summary


Related Solutions

Hot & Cold compress 1. What are compresses? 2. What conditions would benefit from hot compresses?...
Hot & Cold compress 1. What are compresses? 2. What conditions would benefit from hot compresses? 3. What conditions would benefit from cold compresses? 4. How do you create a compress?
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array is based around having a fixed size per array creation, there is no logical limit on the ability of a linked list to grow. Physical limitations aside, linked lists are capable of growing largely without any limitations. To achieve this, they trade easier access to their individual elements. This is because linked lists have to be traversed from their root node to any node...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
Write a python function image compress() that takes one argument called filename, which is the name...
Write a python function image compress() that takes one argument called filename, which is the name of a file that contains a N × N (N-pixel by N-pixel) “grayscale bitmap image”. A “grayscale bitmap image” is an image of the following form where every pixel contains a grayscale color value between 0 − 255 (inclusive). Colour value 0 means that pixel should appear completely black and color value 255means completely white. Any other value in between stands for different shades...
Consider the data contained in the table below, which lists 30 monthly excess returns to two...
Consider the data contained in the table below, which lists 30 monthly excess returns to two different actively managed stock portfolios (A and B) and three different common risk factors (1, 2, and 3). (Note: You may find it useful to use a computer spreadsheet program such as Microsoft Excel to calculate your answers.) Period Portfolio A Portfolio B Factor 1 Factor 2 Factor 3 1 1.00 % 0.00 % 0.02 % -0.99 % -1.73 % 2 7.52 6.61 6.85...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name:...
1. Dictionaries and Lists. a) Implement a list of student dictionaries containing the following data: name: Andy, classes: ET580 MA471 ET574 name: Tim, classes: ET574 MA441 ET575 name: Diane, classes: MA441 MA471 ET574 name: Lucy, classes: ET574 ET575 MA471 name: Steven, classes: ET574 MA441 ET580 b) Implement a dictionary of courses and set each courses enrollment to 0: ET580: 0 ET574: 0 ET575: 0 MA441: 0 MA471: 0 c) Use a loop and if statements to read class data from...
C++ only please Description A hash table is a data structure that is used to store...
C++ only please Description A hash table is a data structure that is used to store keys/value pairs. It is perfect to use when you have a large amount of directory-type information and the operations you need to perform are to insert, delete, print, and search. I am giving you all a lot more freedom in this program in that the value held in your hash table can be a pointer to any object created from your own custom class....
A deque is a data structure consisting of a list of items, on which the following...
A deque is a data structure consisting of a list of items, on which the following operations are possible: • push(x): Insert item x on the front end of the deque. • pop(): Remove the front item from the deque and return it. • inject(x): Insert item x on the rear end of the deque. • eject(): Remove the rear item from the deque and return it. Write routines to support the deque that take O(1) time per operation. In...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT