Question

In: Computer Science

What to do This assignment is going to be pretty simple - we're just going compute...

What to do

This assignment is going to be pretty simple - we're just going compute frequency counts of certain colors in certain ranges (Histogram of the image).

Because there is no natural way of segregating RGB color ranges, we usually do this color by color. So you'll be computing frequency counts for individual color components individually.

The expectation is that you will write a function as described by a python function signature below (while you are free to do whatever you want, I think doing future parts of the assignment will be easier if the code is structured this way) -

Think about what happens when one is comparing images of different sizes -

  • Lets say A is an 4 by 4 image where half of the colors are gray (rgb(128, 128, 128)), and the other half is white (rgb(255, 255, 255))
  • When you compute frequencies for Red component, you'll find that 128 occurs 8 times and 255 occurs 8 times also.
  • Lets say B is an 8 by 8 image where half of the colors are gray (rgb(128, 128, 128)), and the other half is white (rgb(255, 255, 255))
  • When you compute frequencies for Red component, you'll find that 128 occurs 32 times and 255 occurs 32 times also.
  • This would mean that images that are basically the same in terms of colors (both half gray and white), end up with different histograms.

To make sure similar images have similar histograms regardless of image size, it is good to normalise frequencies by dividing each frequency count by the total number of pixel in the image.

  • After normalization, frequency for red 128 in A would be 8 / (4 * 4) = 0.5
  • After normalization, frequency for red 128 in B would be 32 / (8 * 8) = 0.5
  • You will be given an image as a text file in the format we've been using so far - assignment_files.zip

Solutions

Expert Solution

# PLEASE LIKE THE SOLUTION
# FEEL FREE TO DISCUSS IN COMMENT SECTION

# Python Program

# Assure that matplot and cv2 are installed in your ide used for execution of this program
# if you not require plot of histogram you may remove optional code mention below and remove import
matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import cv2

# this function calulates frequency and draw histogram and print in terminal
def draw_histogram(image):
   # initilaise the key value pair
   freqForRed = {}
   freqForGreen = {}
   freqForBlue = {}
  
   # get height of image
height = image.shape[0]
   width = image.shape[1]

   # initilaising with 0 for all 0 to 255 pixel frequency
   for i in range(256):
       freqForRed.update({i : 0})
       freqForGreen.update({i : 0})
       freqForBlue.update({i : 0})

   # for each pixel get bgr value and update frequency
  
   for i in range(0,height):
       for j in range(0,width):
           color = image[i][j]
           blueValue = int(color[0])
           greenValue = int(color[1])
           redValue = int(color[2])
          
           # update red frequency for this pixel
        value = freqForBlue[blueValue]
           freqForBlue.update({freqForBlue[blueValue] : value + 1})

           value = freqForGreen[greenValue]
           freqForGreen.update({freqForGreen[greenValue] : value + 1})

           value = freqForRed[redValue]
           freqForRed.update({freqForRed[redValue] : value + 1})

   redList = []
   blueList = []
   greenList = []

   # now normalising by dividing by its size
   size = float((height * width))
   for i in range(256):
       value = freqForRed[i];
       redList.append((value/size))
      
       value = freqForGreen[i];
       blueList.append((value/size))

       value = freqForBlue[i]
       greenList.append((value/size))

   #print key and normalised frequency
   for i in range(256):
       print("Normalized Frequency Red"+str(i)+" is "+str(redList[i])+" Green"+str(i)+" is "+str(greenList[i])+" Blue"+str(i)+" is "+str(blueList[i]))

   # OPTIONAL CODE FOR PLOTING
   # PLOT RED
x = list(freqForRed.keys())
   plt.plot(x, redList)
   plt.gcf().canvas.set_window_title("RED")
   plt.show()

# PLOT GREEN
   plt.plot(x, greenList)
   plt.gcf().canvas.set_window_title("GREEN")
   plt.show()

   # PLOT BLUE
   plt.plot(x, blueList)
   plt.gcf().canvas.set_window_title("BLUE")
   plt.show()


img = cv2.imread('./image.jpg')
draw_histogram(img)

# SAMPLE OUTPUT

# HISTOGRAM


Related Solutions

For this assignment we're going to make another calculator. This one will be a simple four...
For this assignment we're going to make another calculator. This one will be a simple four function (add, subtract, multiply, and divide) calculator, but it will have state. Specifically, the calculator will keep track of the result of the most recent operation and use that value as the first operand for the next operation. Take a look at the sample output below if this doesn't quite make sense. Your new calculator class should have the following fields and methods: fields:...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
Do it in python please This lesson's Group Activities are: We're going to take the Group...
Do it in python please This lesson's Group Activities are: We're going to take the Group Activity from last week and tweak it. Instead of storing the random numbers in a list, we're going to store them in a file. Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the...
What's your overal sentiment on the U.S. economy's growth? We're doing pretty good as an economy...
What's your overal sentiment on the U.S. economy's growth? We're doing pretty good as an economy relative to other years in your life? Things are not as good at the moment? Elaborate on your thoughts and explain how you came to that determination. There are no right or wrong answers here!
This week, we're going to be discussing Amazon and it's ascent to become one of the...
This week, we're going to be discussing Amazon and it's ascent to become one of the largest global online retailers. Get ready for another great set of conversations! https://www.youtube.com/watch?v=YlgkfOr_GLY Why do you think has Amazon succeeded when so many other companies have failed? How did their First Mover's Advantage factor into their ubiquity? How long did it take for Amazon to become profitable? What’s next for Amazon... Cloud computing? Deliveries by drone? Travel? Retail? You should research the company and...
For this assignment, implement a simple stack calculator which can compute an infix expression. It should...
For this assignment, implement a simple stack calculator which can compute an infix expression. It should take in a string containing an infix expression, compute the result, and print it out. It should handle operators +, -, *, / and parenthesis. Your program must have two main steps -- first convert the expression to postfix, and then compute the result using the algorithms discussed in class and textbook. These algorithms require that you use a stack. You must implement your...
n this problem, we're going get a rough estimate the amount of uranium fuel it would...
n this problem, we're going get a rough estimate the amount of uranium fuel it would take if the US recieved all its electrical power from nuclear power plants. The size of a power plant in normally given as the about of electrical power it can produce when running a full capacity. This electrical power produced can be very different than the mechanical or thermal power that was required to produce this electricity. For example, power plant might have a...
n the instructions for this Task C, we're going to assume that you have completed your...
n the instructions for this Task C, we're going to assume that you have completed your script for Task B above; i.e. that you have a working dictionary-based version of the functions (originally named addToRecord(), etc. in Task A) now named addToHistogram(), etc. Let's assume that your dictionary-based function (from Task B above) that creates a histogram is named makeHistogram(). You are going to use your makeHistogram() in the following sub-tasks. In fact, you're welcome to include any function you...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset...
4) We're going to test the same hypothesis four ways. Assume the people in the dataset in armspanSpring2020.csv are a random sample of all adults. For each test, report the test statistic and the p-value. With a 5% significance level, give the conclusion of each test. a) Test the hypothesis that the mean difference between armspan and height it not equal to 0, using the data in armspanSpring2020.csv. Do this by creating a new variable named diff = (armspan -...
Remember we're only using C to program this. That does not mean C++, just no! This...
Remember we're only using C to program this. That does not mean C++, just no! This one's long so pay attention. You are going to create a structure that resembles a university’s profile (Any university name is ok, just pick one). This structure must contain 5 members. They are listed here: 1. One member for number of undergraduate students 2. One member for number of graduate students 3. One member for number of classrooms 4. One member for the name...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT