简体   繁体   中英

What does the array from return of matplotlib.image.imread() mean?

I am trying to write a program in python 3 which gets the bottom half of the image height and then plot it's histogram. I saw a code like below as an example. But I don't know why and what value it returns. I couldn't figure out what values are operated also what does this line actually does ?

img[img.shape[0]//2:, :]

Sample Code

import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# Load our image
# `mpimg.imread` will load .jpg as 0-255, so normalize back to 0-1
img = mpimg.imread('warped_example.jpg')/255

def hist(img):
    # TO-DO: Grab only the bottom half of the image
    # Lane lines are likely to be mostly vertical nearest to the car
    bottom_half = img[img.shape[0]//2:, :]

    # TO-DO: Sum across image pixels vertically - make sure to set `axis`
    # i.e. the highest areas of vertical lines should be larger values
    histogram = np.sum(bottom_half, axis=0)

    return histogram

The code sample is commented well that means you got what the code is actually doing. Just in short,

It takes up the bottom half region of the image and stores in a variable "bottom_half" then, it takes the sum of all the image pixels vertically and then returns its value in an array.

Now, the thing which needs to be understood is what values the function returning and where it's used.

Since the next thing which you were going to do (as per question) was to find the histogram and a histogram basically plots the frequency of the various pixel present in an image. Now, this histogram takes input a single array or a sequence of arrays which are not required to be of the same length. (As per matplotlib documentation )

Thus to plot the histogram the function needs an array containing various pixel intensities of which you want to plot the histogram and that is returned by the function you defined in the program.

In short, The function is returning an array containing all the pixel values and this array will be later used for plotting the histogram.

Hope you got what you were looking for :)

Edit ::

I will try to break things so to make you understand well,

firstly img.shape, this returns a tuple of three values (image height, image width, no. of channels) -> img.shape[0] means it will pick the height of the image and as in code a floor division by 2 is also performed which will give half height of the image ( for Float value after division).

These were the two important things to understand in that line.

So, what actually it is doing is slicing the whole image as it can be seen from the code. Now, this slicing starts from the half of height of the image till the last row of the image. The need for the slicing was to get the bottom half of the image. The slicing can be better understood by this,

img.shape[half_height_start:,:]

Befor ',' it slices pixels from half height to bottom row of the image

and after ',' it slices all the rows in the image or full width of the image. For Reading about slicing in python you can see here

now the next question, the program is performing np.sum() over the vertical columns which will return the sum of every column in an array. The program is actually doing this so that it will print only n (no. of columns) the number of columns in the image maybe for giving a better visualization of the plot. It's all up to code.

You can remove this part as well and you can directly return the bottom_img as well but in this case, the histogram will print the frequency of all the pixels.

It's all up to code. Hope this clears your doubt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM