The above figure shows a digital picture represented as a nested list. Each element in the list stores the value of a single pixel. Each pixel is either black or white. A white pixel is represented by 1 and a black pixel by 0.Write a Python function grey_value(image) which takes an image image (represented as a list of lists like above) and returns its grey value. The grey value of a black and white picture is defined as the number of white pixels divided by the total number of pixels in the picture. Your function should return the grey value as a single floating-point number in the range 0.0 to 1.0.
Question
The above figure shows a digital picture represented as a nested list. Each element in the list stores the value of a single pixel. Each pixel is either black or white. A white pixel is represented by 1 and a black pixel by 0.Write a Python function grey_value(image) which takes an image image (represented as a list of lists like above) and returns its grey value. The grey value of a black and white picture is defined as the number of white pixels divided by the total number of pixels in the picture. Your function should return the grey value as a single floating-point number in the range 0.0 to 1.0.
Solution
Here is a Python function that calculates the grey value of an image:
def grey_value(image):
total_pixels = 0
white_pixels = 0
for row in image:
for pixel in row:
total_pixels += 1
if pixel == 1:
white_pixels += 1
return white_pixels / total_pixels
This function works by iterating over each row in the image, and then over each pixel in the row. It counts the total number of pixels and the number of white pixels. Finally, it returns the ratio of white pixels to total pixels, which is the grey value of the image.
Similar Questions
Youwrite a command to read the input image , to convert rgb to gray, to display output, to FFT of the input image , Display the output, display the normalized intensity value of pixel coordinates,
Convert the gray scale image to a binary image by setting all the array elementsabove 55 to 1 and below 55 to 0 (zero).
1. Sample an Image in Python 2. Read an Image and obtain it's complement in Python 3. Convert an Image from in python - RGB to GRAY - RGB to HSV - HSV to GRAY - RGB to CB - RGB to Binary code in python
1. Obtain histogram equalization for both original and negative of an image in Python. give direct one sniipit code
We saw in lectures that vectors can be used to represent images: In this challenge, you will need to reshape your given vector so that you have 8, 8-vectors. Then use .imshow() to display the resultant image and plt.colorbar() to view the colorscale. Additionally use cmap='gray'.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.