Question

Task: Write the imagej macro code to perform Histogram Equalization Your code should perform the following steps: . Find thewrite in C

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

// C program to perform histogram equalisation to adjust contrast levels

// All the needed library functions for this program
#include <fcntl.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Function to perform histogram equalisation on an image
// Function takes total rows, columns, input file name and output
// file name as parameters
void histogramEqualisation(int cols, int rows,
                       char* input_file_name, char* output_file_name)
{
   // creating image pointer
   unsigned char* image;

   // Declaring 2 arrays for storing histogram values (frequencies) and
   // new gray level values (newly mapped pixel values as per algorithm)
   int hist[256] = { 0 };
   int new_gray_level[256] = { 0 };

   // Declaring other important variables
   int input_file, output_file, col, row, total, curr, i;

   // allocating image array the size equivalent to number of columns
   // of the image to read one row of an image at a time
   image = (unsigned char*)calloc(cols, sizeof(unsigned char));

   // opening input file in Read Only Mode
   input_file = open(input_file_name, O_RDONLY);
   if (input_file < 0) {
       printf("Error opening input file\n");
       exit(1);
   }

   // creating output file that has write and read access
   output_file = creat(output_file_name, 0666);
   if (output_file < 0) {
       printf("Error creating output file\n");
       exit(1);
   }

   // Calculating frequency of occurrence for all pixel values
   for (row = 0; row < rows; row++) {
       // reading a row of image
       read(input_file, &image[0], cols * sizeof(unsigned char));

       // logic for calculating histogram
       for (col = 0; col < cols; col++)
           hist[(int)image[col]]++;
   }

   // calulating total number of pixels
   total = cols * rows;

   curr = 0;

   // calculating cumulative frequency and new gray levels
   for (i = 0; i < 256; i++) {
       // cumulative frequency
       curr += hist[i];

       // calculating new gray level after multiplying by
       // maximum gray count which is 255 and dividing by
       // total number of pixels
       new_gray_level[i] = round((((float)curr) * 255) / total);
   }

   // closing file
   close(input_file);

   // reopening file in Read Only Mode
   input_file = open(input_file_name, O_RDONLY);

   // performing histogram equalisation by mapping new gray levels
   for (row = 0; row < rows; row++) {
       // reading a row of image
       read(input_file, &image[0], cols * sizeof(unsigned char));

       // mapping to new gray level values
       for (col = 0; col < cols; col++)
           image[col] = (unsigned char)new_gray_level[image[col]];

       // reading new gray level mapped row of image
       write(output_file, &image[0], cols * sizeof(unsigned char));
   }

   // freeing dynamically allocated memory
   free(image);

   // closing input and output files
   close(input_file);
   close(output_file);
}

// driver code
int main()
{
   // declaring variables
   char* input_file_name;
   char* output_file_name;
   int cols, rows;

   // defining number of rows and columns in an image
   // here, image size is 512*512
   cols = 512;
   rows = 512;

   // defining input file name (input image name)
   // this boat_512_512 is a raw grayscale image
   input_file_name = "boat_512_512";

   // defining output file name (output image name)
   output_file_name = "boat_512_512_histogram_equalised";

   // calling function to do histogram equalisation
   histogramEqualisation(cols, rows, input_file_name, output_file_name);

   return 0;
}

Results: Boat image before and after Histogram Equalisation (From Left to Right) 90 boat 512 512 boat 512 512 histogram_equalTransformation of Histogram before and after Equalisation (From Left to Right) hlstogram_after_equallsation.png 9e histogram_

Add a comment
Know the answer?
Add Answer to:
write in C Task: Write the imagej macro code to perform Histogram Equalization Your code should perform the following steps: . Find the minimum intensity in the image. Construct a histogram in an arra...
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:...

  • 1.find the following i. Use histogram equalization to improve the contrast of an image of your...

    1.find the following i. Use histogram equalization to improve the contrast of an image of your choice. Explain why the contrast changed (or didn’t change). Show both the original and modified image and a histogram for both images. ii. Convert a color image of your choice to black and white. Then, convert the black and white image to a binary image. Repeat the previous step, but use a different threshold value. Share both the original, the black and white image,...

  • 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 zeroh = zeros(1,256);% ... here goes your code ...end % – do not forget the END!To test your function, save...

  • write a small C program including the following C functions/subroutines. Your main routine should use these...

    write a small C program including the following C functions/subroutines. Your main routine should use these functions to generate a small-ish array of random values and show that the standard deviation lowers as the array is smoothed. • void random_array(double* array, int size, double scale); – load an array with random double values scaled by scale (random number generators generate double values between 0 and 1). Note: array should point to memory already allocated. • double sum(double* array, int size);...

  • write a code on .C file Problem Write a C program to implement a banking application...

    write a code on .C file Problem Write a C program to implement a banking application system. The program design must use a main and the below functions only. The program should use the below three text files that contain a set of lines. Sample data of these files are provided with the assessment. Note that you cannot use the library string.h to manipulate string variables. For the file operations and manipulations, you can use only the following functions: fopen(),...

  • Your program should be capable of creating a ppm image of the flag of Benin • The new flag gener...

    Your program should be capable of creating a ppm image of the flag of Benin • The new flag generated should use the proper width-to-height ratio as defined on Wikipedia for the flag. o For the colors, use pure red, and yellow, and a value of 170 for green. ▪ Pure color values are talked about in Lab 6. o The left green field should be 6/15ths of the width of the flag. Variables country_code should also include a new...

  • Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate...

    Write a C++ program named, gradeProcessor.cpp, that will do the following tasks: -Print welcome message -Generate the number of test scores the user enters; have scores fall into a normal distribution for grades -Display all of the generated scores - no more than 10 per line -Calculate and display the average of all scores -Find and display the number of scores above the overall average (previous output) -Find and display the letter grade that corresponds to the average above (overall...

  • The following C++  code contains an incomplete program that should be able to calculate the distance between...

    The following C++  code contains an incomplete program that should be able to calculate the distance between a series of geocoded locations. Two parts of the program need to be completed: 1. The portion of the main() function that calculates the distances between each of the hard-coded 5 locations, and stores it in a two-dimensional array declared as distances. 2. A portion of the calculateDistanceBetweenLocations(l1, l2) function; omitted code spaces are designed with a // TODO: comment. The code is properly...

  • Object Oriented Programming Please write the code in C++ Please answer the question in text form...

    Object Oriented Programming Please write the code in C++ Please answer the question in text form 9.5 (complex Class) Create a class called complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form where i is V-I Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in...

  • Exercise #1: Write a C program that contains the following steps (make sure all variables are...

    Exercise #1: Write a C program that contains the following steps (make sure all variables are int). Read carefully each step as they are not only programming steps but also learning topics that explain how functions in C really work. Ask the user for a number between 10 and 99. Write an input validation loop to make sure it is within the prescribed range and ask again if not. Commenting out the existing coding, write the code to divide a...

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