Question

In this exercise you should write a Python function that computes the histogram of an intensity (gray scale) image. Do not use specifific Python functions for histogram computation (like hist or imhist).

Create a new function my_hist and start with the following lines:

function [h] = my_hist(im)

% H = MY_HIST(IM) computes the histogram for the intensity

% image IM (with values from 0 to 255) and returns a vector H

% with 256 dimensions

% get the image size: M = number of rows, N = number of columns

[M, N] = size(im);

% initilalize the histogram to zero

h = zeros(1,256);

% ... here goes your code ...

end % – do not forget the END!

To test your function, save the m-fifile and type at the prompt (omit the

comments):

% read image fifile

I = imread(boat.tif);

% compute the histogram

H = my_hist(I);

% display the result


1 0
Add a comment Improve this question Transcribed image text
Answer #1

def my_hist(im):

    # Get the image size: M = number of rows, N = number of columns

    M, N = im.shape


    # Initialize the histogram to zero

    h = [0] * 256


    # Iterate through the image and count pixel values

    for i in range(M):

        for j in range(N):

            pixel_value = im[i, j]

            h[pixel_value] += 1


    return h


# To test the function:

# First, make sure you have a grayscale image "boat.tif" in your working directory.


import cv2

import matplotlib.pyplot as plt


# Read the image

I = cv2.imread('boat.tif', cv2.IMREAD_GRAYSCALE)


# Compute the histogram using your custom function

H = my_hist(I)


# Display the histogram

plt.bar(range(256), H, width=1.0, align='center')

plt.title("Histogram of 'boat.tif' Image")

plt.xlabel("Pixel Value")

plt.ylabel("Frequency")

plt.show()



In this code:

  1. The my_hist function takes a grayscale image im as input.

  2. It initializes a histogram array h with 256 elements (for pixel values from 0 to 255).

  3. It then iterates through the image and counts the occurrences of each pixel value, updating the histogram accordingly.

  4. After computing the histogram, you can visualize it using the matplotlib library.

Make sure you have the "boat.tif" image in your working directory or provide the correct path to the image file


answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
In this exercise you should write a Python function that computes the histogram of an intensity (gray scale) image. Do not use specifific Python functions for histogram computation (like hist or imhist).
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Image histogram code in python?

    In this exercise you should write a Python function that computes the histogram of an intensity (gray scale) image. Do not use specifific Python functions for histogram computation (like hist or imhist). Create a new function my_hist and start with the following lines: function [h] = my_hist(im) % H = MY_HIST(IM) computes the histogram for the intensity % image IM (with values from 0 to 255) and returns a vector H % with 256 dimensions % get the image size:...

  • use MATLAB to upload the following: an image that you want to process (can be taken...

    use MATLAB to upload the following: an image that you want to process (can be taken yourself or downloaded from the internet) a script that processes the image in TWO ways. manipulates the colors averages pixels together Please make sure the script displays the images (like how I did with the 40 and 80 pixel averaging) so I can easily compare them to the original. Make sure to COMMENT your code as well. Homework 13 Please upload the following: an...

  • It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #inclu...

    It is a C++ program by using inheritance and vectors My professor said that all the files should be separate. like File.cpp, File.h, Text.cpp,Text.h,Main.cpp I already have these files File.cpp #include "File.h" // Constructor of File that takes File name and type as arguments File::File(string type, string name) {    this->type = type;    this->name = name; } //returns the type. string File::getType() {    return type; } //returns the name of file. string File::getName() {    return name; } File.h #ifndef __FILE_H__ #define...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

  • Don't attempt if you can't attempt fully, i will dislike and negative comments would be given...

    Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT