In: Computer Science
Please Complete this C Code using the gcc compiler. Please include comments to explain each added line.
/*This program computes the Intersection over
Union of two rectangles
as a percent:
IoU = [Area(Intersection of R1 and R2) * 100 ] / [Area(R1)
+ Area(R2) - Area(Intersection of R1 and R2)]
The answer will be specified as a percent: a number between 0
and 100.
For example, if the rectangles do not overlap, IoU = 0%. If they
are
at the same location and are the same height and width, IoU =
100%.
If they are the same area 30 and their area of overlap is 10, IoU
=
20%.
Input: two bounding boxes, each specified as {Tx, Ty, Bx, By),
where
(Tx, Ty) is the upper left corner point and
(Bx, By) is the lower right corner point.
These are given in two global arrays R1 and R2.
Output: IoU (an integer, 0 <= IoU < 100).
In images, the origin (0,0) is located at the left uppermost
pixel,
x increases to the right and y increases downward.
So in our bounding box representation, it will always be true
that:
Tx < Bx and Ty < By.
Assume images are 640x480 and bounding boxes fit within these
bounds and
are always of size at least 1x1.
IoU should be specified as an integer (only the whole part of
the division),
i.e., round down to the nearest whole number between 0 and 100
inclusive.
FOR FULL CREDIT (on all assignments in this class), BE SURE TO
TRY
MULTIPLE TEST CASES and DOCUMENT YOUR CODE.
*/
#include
#include
//DO NOT change the following declaration (you may change the
initial value).
// Bounding box: {Tx, Ty, Bx, By}
int R1[] = {64, 51, 205, 410};
int R2[] = {64, 51, 205, 410};
int IoU;
/*
For the grading scripts to run correctly, the above
declarations
must be the first lines of code in this file (for this
homework
assignment only). Under penalty of grade point loss, do not
change
these lines, except to replace the initial values while you are
testing
your code.
Also, do not include any additional libraries.
*/
int main() {
// insert your code here
IoU = -999; // Remove this line. (It's only provided so that shell code compiles w/out warnings.)
printf("Intersection over Union: %d%%\n", IoU);
return 0;
}
// C program that computes the Intersection over Union of two rectangles as a percent
#include <stdio.h>
#include <stdlib.h>
//DO NOT change the following declaration (you may change the initial value).
// Bounding box: {Tx, Ty, Bx, By}
int R1[] = {64, 51, 205, 410};
int R2[] = {64, 51, 205, 410};
int IoU;
/*
For the grading scripts to run correctly, the above declarations
must be the first lines of code in this file (for this homework
assignment only). Under penalty of grade point loss, do not change
these lines, except to replace the initial values while you are testing
your code.
Also, do not include any additional libraries.
*/
int main(void) {
int Inter[4]; // coordinates for the intersection rectangle
// determine the top coordinate
// x value – will be the maximum of the Tx
if(R1[0] > R2[0])
Inter[0] = R1[0];
else
Inter[0] = R2[0];
// y value – will be maximum of the Ty
if(R1[1] > R2[1])
Inter[1] = R1[1];
else
Inter[1] = R2[1];
// determine the bottom coordinate
// x value – minimum of the Bx
if(R1[2] < R2[2])
Inter[2] = R1[2];
else
Inter[2] = R2[2];
// y value – minimum of the By
if(R1[3] < R2[3])
Inter[3] = R1[3];
else
Inter[3] = R2[3];
// calculate area of R1
int Area_R1 = (R1[2]-R1[0])*(R1[3]-R1[1]);
// calculate area of R2
int Area_R2 = (R2[2]-R2[0])*(R2[3]-R2[1]);
// calculate area of Intersection rectangle
int Area_Inter = 0;
// if the rectangles do intersect
if(Inter[2] > Inter[0])
Area_Inter = (Inter[2]-Inter[0])*(Inter[3]-Inter[1]);
// calculate IOU
IoU = (Area_Inter*100)/(Area_R1 + Area_R2 - Area_Inter);
printf("Intersection over Union: %d%%\n", IoU);
return EXIT_SUCCESS;
}
//end of program
/*
* Test Cases:
*
* Test Case 1 :
* int R1[] = {0, 0, 10, 10};
* int R2[] = {5, 0, 10, 10};
* IoU = 50%
*
* Test Case 2:
* int R1[] = {0, 0, 10, 10};
* int R2[] = {5, 5, 20, 20};
* IoU = 8%;
*
* Test Case 3:
* int R1[] = {0, 0, 10, 10};
* int R2[] = {15, 15, 20, 20};
* IoU = 0%
*
* Test Case 4:
* int R1[] = {64, 51, 205, 410};
* int R2[] = {64, 51, 205, 410};
* IoU = 100%
*
* Test Case 5:
* int R1[] = {0, 0, 10, 10};
* int R2[] = {5, 5, 7, 8};
* IoU = 6%
*/
Output: