+1 (315) 557-6473 

How to check image contrast

The contrast applied on an image can be used to determine the clarity level of the image. This assignment sample describes how to check image contrast using C++ and can help you understand how to complete assignments of this nature successfully in the future.

Check Image Contrast

This assignment should be complete using C++ code. Below are the chapters that covered:
  • Vocabulary of C
  • Identifiers and constants
  • Fundamental and derived types
  • Arithmetic operators
  • Operator precedence
  • Expressions and statements
  • Functions and procedures
  • Standard input and output
  • Standard and application libraries
  • Relational and logical operators
  • Unary operators
  • Shift and assignment operators
  • Conditional and comma operators
  • Precedence and order of evaluation
  • Character input/output
  • Text analysis program
  • Control structures
  • If-else and else-if
  • Case selection (switch)
  • Loops: while, for, do-while
  • Random number generators
  • Arrays and Array Processing
  • Break and continue
Assignment should start with #include #include

a. Problem Objective

Number of elements (pixel) with value 0-80: Dark_Count Number of elements (pixel) with value 175 – 255: Bright_Count Count of all other pixels: Balanced_Count
Criteria Image Quality Decision
Dark_Count>Bright_Count and Balanced Count Dark Image
Balanced_Count>Dark_Count and Bright_Count Well Balanced Image
Bright_Count>Dark_Count and Balanced_Count Bright Image
  Display the output such as “The image quality is Dark” OR “The image quality is Well Balanced Image” OR “The image quality is Bright Image”

b. Flow Chart for Task

Consider using InputImage array. Perform the same operations of Part (a) with the elements of InputImage array. Display your decision w.r.t. InputImage array.

Program Code

image contrast 1

Consider dividing the ModifiedInputImage array into 3 (more or less same size) zones: Area_A, Area_B and Area_C row-wise. That is, say,

Area_A: Row 0 to j

Area_B: Row j+1 to j+1+k

Area_C: Row j+1+k+1 to 255

In each area, repeat the part (a) to determine the image quality. That is, for each area, declare the status like:

“Area A – The image quality is Dark”

“Area B – The image quality is Bright”

“Area C – The image quality is Well Balanced”

Solution:

ImageProcessing.c

#include

#include

#include

#include

//SIZE of image and border width

constint SIZE = 256;

constint BORDER_WIDTH = 3;

// creates a image with random pixels

voidcreateInputImage(intInputImage[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

// generate random number

InputImage[i][j] = rand() % 256;

//if border

if (i < BORDER_WIDTH || i >= SIZE – BORDER_WIDTH) {

InputImage[i][j] = 0;

}

if (j < BORDER_WIDTH || j >= SIZE – BORDER_WIDTH) {

InputImage[i][j] = 0;

}

}

}

}

//prints the image array on screen

voidprintImage(int image[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

printf(“%3d “, image[i][j]);

}

printf(“\n”);

}

}

// adjust contrast of the image PART II

voidadjustContrast(int image[SIZE][SIZE], int modified[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

modified[i][j] = image[i][j];

//if border do nothing

if (i < BORDER_WIDTH || i >= SIZE – BORDER_WIDTH) {

continue;

}

if (j < BORDER_WIDTH || j >= SIZE – BORDER_WIDTH) {

continue;

}

// adjust contrast

if (modified[i][j] < 127) {

modified[i][j] -= 20;

} else if (modified[i][j] > 127) {

modified[i][j] += 20;

}

// if invalid values

if (modified[i][j] < 0) {

modified[i][j] = 0;

} else if (modified[i][j] > 255) {

modified[i][j] = 255;

}

}

}

}

// count pixels with rows fromRows to toRows and pixels min and ax range

intcountPixels(int modified[SIZE][SIZE], intfromRow, inttoRow, int min,

int max) {

int count = 0;

for (int i = fromRow; i <= toRow; ++i) {

for (int j = 0; j < SIZE; ++j) {

if (modified[i][j] >= min && modified[i][j] <= max)

count++;

}

}

return count;

}

//prints image quality

voidprintImageQuality(int dark, int bright, int balanced) {

if (dark > bright && dark > balanced)

printf(“The image quality is Dark\n”);

else if (balanced > bright && balanced > dark)

printf(“The image quality is Well Balanced\n”);

else

printf(“The image quality is Bright\n”);

}

int main() {

srand(time(NULL));

//arrays to hold input and modified image

intInputImage[SIZE][SIZE];

intModifiedImage[SIZE][SIZE];

int quit = 0;

// while not quit

while (quit == 0) {

// create image and print

createInputImage(InputImage);

printf(“This is the starting automatically generated image….\n\n”);

printImage(InputImage);

printf(“\nPress enter to continue…”);

getchar();

// adjust contrast and print image and print

printf(“\nAfter adjusting contrast….\n\n”);

adjustContrast(InputImage, ModifiedImage);

printImage(ModifiedImage);

printf(“\nPress enter to continue…”);

getchar();

// calculate image quality of modified and print

printf(“\nCalculating Image quality of modified image…\n”);

int dark = countPixels(ModifiedImage, 0, SIZE – 1, 0, 80);

int bright = countPixels(ModifiedImage, 0, SIZE – 1, 175, 255);

int balanced = countPixels(ModifiedImage, 0, SIZE – 1, 81, 174);

printImageQuality(dark, bright, balanced);

printf(“\nPress enter to continue…”);

getchar();

// calculate image quality of input and print

printf(“\nCalculating Image quality of input image…\n”);

dark = countPixels(InputImage, 0, SIZE – 1, 0, 80);

bright = countPixels(InputImage, 0, SIZE – 1, 175, 255);

balanced = countPixels(InputImage, 0, SIZE – 1, 81, 174);

printImageQuality(dark, bright, balanced);

printf(“\nPress enter to continue…”);

getchar();

// to divide into 3 parts

int j = SIZE / 3;

int k = SIZE / 3;

// print areas divided

printf(“\nDividing modified image into 3 parts…\n”);

printf(“Area A: rows %d to %d\n”, 0, j);

printf(“Area B: rows %d to %d\n”, j + 1, j + 1 + k);

printf(“Area C: rows %d to %d\n”, j + 1 + k + 1, SIZE – 1);

//print image qualities of modified areas

printf(“\nCalculating Image quality of divided areas image…\n”);

dark = countPixels(ModifiedImage, 0, j, 0, 80);

bright = countPixels(ModifiedImage, 0, j, 175, 255);

balanced = countPixels(ModifiedImage, 0, j, 81, 174);

printf(“Area A – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(ModifiedImage, j + 1, j + 1 + k, 0, 80);

bright = countPixels(ModifiedImage, j + 1, j + 1 + k, 175, 255);

balanced = countPixels(ModifiedImage, j + 1, j + 1 + k, 81, 174);

printf(“Area B – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 0, 80);

bright = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 175, 255);

balanced = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 81, 174);

printf(“Area C – “);

printImageQuality(dark, bright, balanced);

//————-

printf(“\nPress enter to continue…”);

getchar();

// print image qualities of input image area

printf(“\nDividing input image into 3 parts…\n”);

printf(“Area A: rows %d to %d\n”, 0, j);

printf(“Area B: rows %d to %d\n”, j + 1, j + 1 + k);

printf(“Area C: rows %d to %d\n”, j + 1 + k + 1, SIZE – 1);

printf(

“\nCalculating Image quality of divided areas of the image…\n”);

dark = countPixels(InputImage, 0, j, 0, 80);

bright = countPixels(InputImage, 0, j, 175, 255);

balanced = countPixels(InputImage, 0, j, 81, 174);

printf(“Area A – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(InputImage, j + 1, j + 1 + k, 0, 80);

bright = countPixels(InputImage, j + 1, j + 1 + k, 175, 255);

balanced = countPixels(InputImage, j + 1, j + 1 + k, 81, 174);

printf(“Area B – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 0, 80);

bright = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 175, 255);

balanced = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 81, 174);

printf(“Area C – “);

printImageQuality(dark, bright, balanced);

// ask if user wants to quit

printf(“Enter 0 to run the analysis again, any other number to quit…\n”);

scanf(“%d”, &quit);

}

}

a. Flow Chart for Task

image contrast 2

Program Code

#include

#include

#include

#include

//SIZE of image and border width

constint SIZE = 256;

constint BORDER_WIDTH = 3;

// creates a image with random pixels

voidcreateInputImage(intInputImage[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

// generate random number

InputImage[i][j] = rand() % 256;

//if border

if (i < BORDER_WIDTH || i >= SIZE – BORDER_WIDTH) {

InputImage[i][j] = 0;

}

if (j < BORDER_WIDTH || j >= SIZE – BORDER_WIDTH) {

InputImage[i][j] = 0;

}

}

}

}

//prints the image array on screen

voidprintImage(int image[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

printf(“%3d “, image[i][j]);

}

printf(“\n”);

}

}

// adjust contrast of the image PART II

voidadjustContrast(int image[SIZE][SIZE], int modified[SIZE][SIZE]) {

for (int i = 0; i < SIZE; ++i) {

for (int j = 0; j < SIZE; ++j) {

modified[i][j] = image[i][j];

//if border do nothing

if (i < BORDER_WIDTH || i >= SIZE – BORDER_WIDTH) {

continue;

}

if (j < BORDER_WIDTH || j >= SIZE – BORDER_WIDTH) {

continue;

}

// adjust contrast

if (modified[i][j] < 127) {

modified[i][j] -= 20;

} else if (modified[i][j] > 127) {

modified[i][j] += 20;

}

// if invalid values

if (modified[i][j] < 0) {

modified[i][j] = 0;

} else if (modified[i][j] > 255) {

modified[i][j] = 255;

}

}

}

}

// count pixels with rows fromRows to toRows and pixels min and ax range

intcountPixels(int modified[SIZE][SIZE], intfromRow, inttoRow, int min,

int max) {

int count = 0;

for (int i = fromRow; i <= toRow; ++i) {

for (int j = 0; j < SIZE; ++j) {

if (modified[i][j] >= min && modified[i][j] <= max)

count++;

}

}

return count;

}

//prints image quality

voidprintImageQuality(int dark, int bright, int balanced) {

if (dark > bright && dark > balanced)

printf(“The image quality is Dark\n”);

else if (balanced > bright && balanced > dark)

printf(“The image quality is Well Balanced\n”);

else

printf(“The image quality is Bright\n”);

}

int main() {

srand(time(NULL));

//arrays to hold input and modified image

intInputImage[SIZE][SIZE];

intModifiedImage[SIZE][SIZE];

int quit = 0;

// while not quit

while (quit == 0) {

// create image and print

createInputImage(InputImage);

printf(“This is the starting automatically generated image….\n\n”);

printImage(InputImage);

printf(“\nPress enter to continue…”);

getchar();

// adjust contrast and print image and print

printf(“\nAfter adjusting contrast….\n\n”);

adjustContrast(InputImage, ModifiedImage);

printImage(ModifiedImage);

printf(“\nPress enter to continue…”);

getchar();

// calculate image quality of modified and print

printf(“\nCalculating Image quality of modified image…\n”);

int dark = countPixels(ModifiedImage, 0, SIZE – 1, 0, 80);

int bright = countPixels(ModifiedImage, 0, SIZE – 1, 175, 255);

int balanced = countPixels(ModifiedImage, 0, SIZE – 1, 81, 174);

printImageQuality(dark, bright, balanced);

printf(“\nPress enter to continue…”);

getchar();

// calculate image quality of input and print

printf(“\nCalculating Image quality of input image…\n”);

dark = countPixels(InputImage, 0, SIZE – 1, 0, 80);

bright = countPixels(InputImage, 0, SIZE – 1, 175, 255);

balanced = countPixels(InputImage, 0, SIZE – 1, 81, 174);

printImageQuality(dark, bright, balanced);

printf(“\nPress enter to continue…”);

getchar();

// to divide into 3 parts

int j = SIZE / 3;

int k = SIZE / 3;

// print areas divided

printf(“\nDividing modified image into 3 parts…\n”);

printf(“Area A: rows %d to %d\n”, 0, j);

printf(“Area B: rows %d to %d\n”, j + 1, j + 1 + k);

printf(“Area C: rows %d to %d\n”, j + 1 + k + 1, SIZE – 1);

//print image qualities of modified areas

printf(“\nCalculating Image quality of divided areas image…\n”);

dark = countPixels(ModifiedImage, 0, j, 0, 80);

bright = countPixels(ModifiedImage, 0, j, 175, 255);

balanced = countPixels(ModifiedImage, 0, j, 81, 174);

printf(“Area A – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(ModifiedImage, j + 1, j + 1 + k, 0, 80);

bright = countPixels(ModifiedImage, j + 1, j + 1 + k, 175, 255);

balanced = countPixels(ModifiedImage, j + 1, j + 1 + k, 81, 174);

printf(“Area B – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 0, 80);

bright = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 175, 255);

balanced = countPixels(ModifiedImage, j + 1 + k + 1, SIZE – 1, 81, 174);

printf(“Area C – “);

printImageQuality(dark, bright, balanced);

//————-

printf(“\nPress enter to continue…”);

getchar();

// print image qualities of input image area

printf(“\nDividing input image into 3 parts…\n”);

printf(“Area A: rows %d to %d\n”, 0, j);

printf(“Area B: rows %d to %d\n”, j + 1, j + 1 + k);

printf(“Area C: rows %d to %d\n”, j + 1 + k + 1, SIZE – 1);

printf(

“\nCalculating Image quality of divided areas of the image…\n”);

dark = countPixels(InputImage, 0, j, 0, 80);

bright = countPixels(InputImage, 0, j, 175, 255);

balanced = countPixels(InputImage, 0, j, 81, 174);

printf(“Area A – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(InputImage, j + 1, j + 1 + k, 0, 80);

bright = countPixels(InputImage, j + 1, j + 1 + k, 175, 255);

balanced = countPixels(InputImage, j + 1, j + 1 + k, 81, 174);

printf(“Area B – “);

printImageQuality(dark, bright, balanced);

dark = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 0, 80);

bright = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 175, 255);

balanced = countPixels(InputImage, j + 1 + k + 1, SIZE – 1, 81, 174);

printf(“Area C – “);

printImageQuality(dark, bright, balanced);

// ask if user wants to quit

printf(“Enter 0 to run the analysis again, any other number to quit…\n”);

scanf(“%d”, &quit);

}

}