Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

6 Procedure 1. Download the skeleton project: $ cd cs103 mkdir chromakey s cd ch

ID: 3674794 • Letter: 6

Question

6 Procedure 1. Download the skeleton project: $ cd cs103 mkdir chromakey s cd chromakey s wget http://bits.usc.edu/files/cs103/chromakey/cs103_chromakey.tar tar xvf cs103_chromakey.tar This will download the files: chromakey.cpp [Source code skeleton for you to complete] bmplib.h bmplib.cpp Makefile colbert gs.bmp bird gs.bmp header file for you to include in chromakey.cpp] [Source code containing the .bmp read/write functions] [Script for you to compile your program] astro.bmp faded.bmp Green-screened images for you to test with] campus1.bmp village.bmp [Background images] readme.txt [readme template for pre-lab and review, to submit] 2. Edit the chromakey.cpp file taking note of the comments embedded in it. You will implement two different methods. In addition to prototypes for the methods already mentioned, in main() we provide some starter code. When calling the read and write BMP file functions, note that we check the return value to ensure successful operation. If one of these functions ever returns a non-zero value, exit the program immediately using "return 1;". Look up the cmath library functions to take the power of a number and the square root. This is useful for computing RGB distance. 3. Review how to pass an array to a function. You will need this when you write your calls to method1(), method2(), and replace() 4. 5. For method 1 you must implement the distance method described earlier in the lab. First determine which set of pixels (location of pixels) you will "assume" are background in any green-screen image. You should write code to iterate over these and find the "chromakey" (average R, G, B) values. Then iterate over the entire image computing the distance between each pixel and the chromakey value, and setting the mask array appropriately. Use the distance threshold that the user entered at the command line. You will need to determine a good threshold value by repeatedly re-running your program and examining the resulting output1.bmp (or colbert1.bmp / bird1.bmp) file 6. At this point you can complete the replace() function and main() function, test whether your method1 worked, and see the effects of different distance thresholds.

Explanation / Answer

Here is your required chromakey.cpp file ..

I filled the code.. where you given empty slot to replace with code of replace, method1, method2 etc..

Comments added .. I made bold where I added code

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include "bmplib.h"

using namespace std;

void method1(unsigned char inImage[][SIZE][RGB],
   bool mask[][SIZE],
   double threshold);

void method2(unsigned char inImage[][SIZE][RGB],
   bool mask[][SIZE]);

void replace(bool mask[][SIZE],
   unsigned char inImage[][SIZE][RGB],
   unsigned char bgImage[][SIZE][RGB],
   unsigned char outImage[][SIZE][RGB]);

int main(int argc, char *argv[])
{
// Image data array
static unsigned char inputImage[SIZE][SIZE][RGB];
static unsigned char bgrndImage[SIZE][SIZE][RGB];
static unsigned char outputImage[SIZE][SIZE][RGB];
static bool chromaMask[SIZE][SIZE];

double threshold;

if (argc < 6) {
cerr << "usage: program_name in.bmp background.bmp dist_threshold "
<< "out1.bmp out2.bmp" << endl;
return 0;
}
  
if (readRGBBMP(argv[1], inputImage)) {
cerr << "Error reading file: " << argv[1] << endl;
return 1;
}

if (readRGBBMP(argv[2], bgrndImage)) {
cout << "Error reading file: " << argv[2] << endl;
return 1;
}
  
// Write code to convert the threshold (argv[3])
// from string format to a double and assign the 'threshold'
threshold = atof(argv[3]);

// Call Method 1 Function
method1(inputImage, chromaMask, threshold);

// Produce the output by calling replace()
replace(chromaMask, inputImage, bgrndImage, outputImage);

// Write the output image to a file using the filename argv[4]
if (writeRGBBMP(argv[4], outputImage)) {
cout << "Error writing file: " << argv[4] << endl;
exit(1);
}  

// Call Method 2 Function
method2(inputImage, chromaMask);

// Produce the output by calling replace()
replace(chromaMask, inputImage, bgrndImage, outputImage);

// Write the output image to a file using the filename argv[5]
if (writeRGBBMP(argv[5], outputImage)) {
cout << "Error writing file: " << argv[5] << endl;
exit(1);
}  

return 0;
}


void method1(unsigned char inImage[][SIZE][RGB],
   bool mask[][SIZE],
   double threshold)
{
float avg[3] = { 0, 0, 0};
float len = 25 * SIZE;
//getting averages
for (int m = 0; m < 25; m++) {
for (int n = 0; n < SIZE; n++) {
for (int p = 0; p < 3; p++)
avg[p] += inImage[m][n][p] / len;   
}
}
  
//calculating average sum
for (int m = 0; m < SIZE; m++) {
for (int n = 0; n < SIZE; n++) {
int average_sum = 0;
for (int p = 0; p < 3; p++) {
average_sum += pow(avg[p] - inImage[m][n][p], 2);
}

if (sqrt(average_sum) < threshold) {
mask[m][n] = 0;
} else {
mask[m][n] = 1;
}

}
}


}


void method2(unsigned char inImage[][SIZE][RGB],
   bool mask[][SIZE])
{
   float avg[3] = { 0, 0, 0};
float len = 25 * SIZE;

//calculating average of inner image
for (int m = 0; m < 25; m++) {
for (int n = 0; n < SIZE; n++) {
for (int p = 0; p < 3; p++)
avg[p] += inImage[m][n][p] / len;

}
}

   float avgDeviation = 0;
   //calculating standard deviation .. which is square root of variance
   for (int m = 0; m< SIZE; m++) {
for (int n = 0; n < SIZE; n++) {
  
avgDeviation += sqrt(
pow( avg[0] - inImage[m][n][0], 2 ) +
pow( avg[1] - inImage[m][n][1], 2) +
pow(avg[2] - inImage[m][n][2], 2) ) / 65536.0f;
  
}
}  

   float std_deviation = 0;

   for (int m = 0; m < SIZE; m++) {
for (int n = 0; n < SIZE; n++) {
std_deviation += pow( sqrt( pow( avg[0] - inImage[m][n][0], 2 ) +
pow( avg[1] - inImage[m][n][1], 2) +
pow(avg[2] - inImage[m][n][2], 2)
) - avgDeviation, 2
) / 65536.0f;
}
}
   std_deviation = sqrt(std_deviation);
   //calling method1
   method1(inImage, mask, std_deviation);

}


void replace(bool mask[SIZE][SIZE],
   unsigned char inImage[SIZE][SIZE][RGB],
   unsigned char bgImage[SIZE][SIZE][RGB],
   unsigned char outImage[SIZE][SIZE][RGB])
{
   //three for loops .. since three dimensional array
for (int m = 0; m< 256; m++) {
       for (int n = 0; n < 256; n++) {
           for (int p = 0; p < 3; p++) {
               if (mask[m][n]) { //checking whether mask is there or not
                   outImage[m][n][p] = inImage[m][n][p];
               } else {
                   outImage[m][n][p] = bgImage[m][n][p];
               }
           }
       }
}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote