Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please all 4 questions CODE TO WORK WITH: #include #define STB_IMAGE_IMPLEMENTATION #include stb_image.h #define STB_IMAGE_WRITE_IMPLEMENTATION #include stb_image_write.h #define STB_IMAGE_RESIZE_IMPLEMENTATION #include stb_image_resize.h #define CHANNEL_NUM 3 //

image text in transcribedimage text in transcribedimage text in transcribed

Please all 4 questions

CODE TO WORK WITH:

#include

#define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #define STB_IMAGE_RESIZE_IMPLEMENTATION #include "stb_image_resize.h"

#define CHANNEL_NUM 3

// imatrix struct, wraps a byte array of pixel data for an image typedef struct imatrix{ int width; int height;

uint8_t** r; uint8_t** g; uint8_t** b; struct imatrix* (*add)(struct imatrix* this, struct imatrix* m2); struct imatrix* (*subtract)(struct imatrix* this, struct imatrix* m2); struct imatrix* (*multiply)(struct imatrix* this, struct imatrix* m2); struct imatrix* (*scale)(struct imatrix* this, int width, int height, float alpha); void (*write_image_to_rgb)(struct imatrix* this); void (*write_rgb_to_image)(struct imatrix* m); struct imatrix* (*set_rgb_image)(struct imatrix* this, uint8_t* new_rgb_image, int width, int height, int channels);

//internal image reference uint8_t* rgb_image; } imatrix;

// initialize an imatrix, allocates all memory dynamically imatrix* init_from_file(char* image_path, int* width, int* height, int* channels); imatrix* init_from_rgb_image(uint8_t* rgb_image, int width, int height, int channels); imatrix* init_blank_rgb_image(int width, int height, int channels);

// frees all allocated memory void free_imatrix(imatrix* image);

int main(int argc, char* argv[]) { int width, height, channels; int width2, height2, channels2; int i; uint8_t* rgb_image;

for (i=1; i

// Load an image imatrix* M = init_from_file("images/test_image1.png", &width, &height, &channels); if (M != NULL) stbi_write_png("images/image.png", width, height, CHANNEL_NUM, M->rgb_image, width*CHANNEL_NUM);

//Load another image imatrix* M2 = init_from_file("images/test_image2.png", &width2, &height2, &channels2); if (M2 != NULL) stbi_write_png("images/image2.png", width2, height2, CHANNEL_NUM, M2->rgb_image, width2*CHANNEL_NUM);

//resize the image to the dimensions of the first image uint8_t* output_pixels = (uint8_t*)malloc(width*height*CHANNEL_NUM*sizeof(uint8_t)); stbir_resize_uint8(M2->rgb_image , width2 , height2 , 0, output_pixels, width, height, 0, CHANNEL_NUM); stbi_write_png("images/image3.png", width, height, CHANNEL_NUM, output_pixels, width*CHANNEL_NUM);

//add two images imatrix* M2_resized = init_from_rgb_image(output_pixels, width, height, channels); imatrix* sum_image = M2_resized->add(M2_resized, M); stbi_write_png("images/sum.png", width, height, CHANNEL_NUM, sum_image->rgb_image, width*CHANNEL_NUM);

//subtract them imatrix* diff_image = sum_image->subtract(sum_image, M); stbi_write_png("images/diff.png", width, height, CHANNEL_NUM, diff_image->rgb_image, width*CHANNEL_NUM);

//scale then add float alpha = 0.4; M2_resized->scale(M2_resized, width, height, alpha); M->scale(M, width, height, (1-alpha)); imatrix* scaled_sum = M2_resized->add(M2_resized, M); stbi_write_png("images/scaled_sum.png", width, height, CHANNEL_NUM, scaled_sum->rgb_image, width*CHANNEL_NUM);

//scaled and translate (matrix multiply)

// OPTIONAL: Test code for matrix multiply // free memory free_imatrix(M); free_imatrix(M2); free_imatrix(M2_resized); free_imatrix(sum_image); free_imatrix(diff_image);

return 0; }

enum RGB {RED, GREEN, BLUE}; void init_funcptrs(imatrix* this); imatrix* init_rgb(imatrix* this, int width, int height, int channels);

imatrix* init_from_file(char* image_path, int* width, int* height, int* channels){ int i,j; uint8_t* rgb_image; rgb_image = stbi_load(image_path, width, height, channels, CHANNEL_NUM); imatrix* image_matrix = malloc(sizeof(imatrix));

init_funcptrs(image_matrix); image_matrix->rgb_image = rgb_image; image_matrix = init_rgb(image_matrix, *width, *height, CHANNEL_NUM); image_matrix->write_image_to_rgb(image_matrix);

return image_matrix; }

imatrix* init_from_rgb_image(uint8_t* rgb_image, int width, int height, int channels){ int i,j;

if (rgb_image==NULL) return NULL;

imatrix* image_matrix = malloc(sizeof(imatrix)); init_funcptrs(image_matrix); image_matrix->rgb_image = rgb_image; image_matrix = init_rgb(image_matrix, width, height, CHANNEL_NUM); image_matrix->write_image_to_rgb(image_matrix);

return image_matrix; }

imatrix* init_blank_rgb_image(int width, int height, int channels){ int i,j; uint8_t* rgb_image=NULL;

imatrix* image_matrix = malloc(sizeof(imatrix)); init_funcptrs(image_matrix); image_matrix->rgb_image = (uint8_t*) malloc(sizeof(uint8_t) * width * height * CHANNEL_NUM); image_matrix = init_rgb(image_matrix, width, height, CHANNEL_NUM); return image_matrix; }

void init_funcptrs(imatrix* this){ imatrix* add(imatrix* m1, imatrix* m2); imatrix* subtract(imatrix* m1, imatrix* m2); imatrix* multiply(imatrix* m1, imatrix* m2); void write_image_to_rgb(imatrix* this); void write_rgb_to_image(imatrix* m); imatrix* scale(imatrix* this, int width, int height, float alpha);

if (this==NULL) return; this->add = add; this->subtract = subtract; this->multiply = multiply; this->write_image_to_rgb = write_image_to_rgb; this->write_rgb_to_image = write_rgb_to_image; this->scale = scale; }

imatrix* init_rgb(imatrix* this, int width, int height, int channels){ int i,j; this->width = width; this->height = height;

//allocate the space for the three channel matrices // YOUR CODE HERE

return this; }

imatrix* set_rgb_image(imatrix* this, uint8_t* new_rgb_image, int height, int width, int channels){ free_imatrix(this); return init_from_rgb_image(new_rgb_image, height, width, channels); }

/* * Add two matrices together. Creates a new imatrix object and returns a referecne to it. Returns * NULL if the matrices have different sizes. * * @param m1 the imatrix object for the first image * @param m2 the imatrix object for the second image * @returns a reference to a newly allocated imatrix object that is the clipped sum of the two input images or * NULL if the matrices are not the same size (same number of rows and columns). * Note: This memory must be freed when you're done using it. */ imatrix* add(imatrix* m1, imatrix* m2){ // YOUR CODE HERE return NULL; }

/* * Subtract m2 from m1. Creates a new imatrix object and returns a referecne to it. Returns * NULL if the matrices have different sizes. * * @param m1 the imatrix object for the first image * @param m2 the imatrix object for the second image * @returns a reference to a newly allocated imatrix object that is the clipped sum of the two input images or * NULL if the matrices are not the same size (same number of rows and columns). * Note: This memory must be freed when you're done using it. */ imatrix* subtract(imatrix* m1, imatrix* m2){

// YOUR CODE HERE

return NULL; }

/* * Matrix multiplication. Multiplies m1*m2 using matrix-matrix multiply. Returns a new imatrix object with * the output. If m1 is a (m, n) matrix, then m2 must be a (n, k) matrix for any value of k >=1. * * @param m1 the left matrix. * @param m2 the right matrix. * @returns a new matrix with the result or NULL of the dimensions do not match properly for matrix multiplications. * Note: This memory must be freed after use. */ imatrix* multiply(imatrix* m1, imatrix* m2){ // YOUR CODE HERE return res; }

/* * Scales all the pixel values by alpha for all alpha values between 0.0 and 1.0. If the values are invalid, the references * to the input image is returned without modification. * * @param this input matrix. * @returns this with all the pixel values scaled by alpha. */ imatrix* scale(imatrix* this, int width, int height, float alpha){ // YOUR CODE HERE

return this; }

void write_rgb_to_image(imatrix* m){ int i,j, height, width; height = m->height; width = m->width;

for (i=0; irgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + RED) = m->r[i][j]; *( m->rgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + GREEN) = m->g[i][j]; *( m->rgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + BLUE) = m->b[i][j]; } } }

void write_image_to_rgb(imatrix* m){ int i,j, height, width; height = m->height; width = m->width;

for (i=0; ir[i][j] = *( m->rgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + RED); m->g[i][j] = *( m->rgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + GREEN); m->b[i][j] = *( m->rgb_image + (i * (CHANNEL_NUM * width) + (CHANNEL_NUM * j)) + BLUE); } } }

void free_imatrix(imatrix* image_matrix){ int i; if (image_matrix==NULL) return;

for (i=0; iheight; ++i){ free(*(image_matrix->r +i)); free(*(image_matrix->g + i)); free(*(image_matrix->b + i)); } free(image_matrix->r); free(image_matrix->g); free(image_matrix->b);

stbi_image_free(image_matrix->rgb_image); }

Please solve all 4 Questions. Any questions, please let me know asap. Thanks.

Please solve all 4 Questions. This should be in C language. You will be completing an image processing library that can process images using matrix operations. All images can be represented as a matrix of single-byte values, where a pixel at matrix location (i, j) is represented by a number between 0 and 255. A color image consists of three channels: a red channel (R), a green channel (G), and a blue channel (B), and the corresponding matrix information for an image include three matrixes, each with the same number of rows (height) and columns (width), one for each of the three channels. Together, these are passed to an image-writing library for outputting to a file and displaying. The code below designates parts of it with /NYOUR CODE HERE, in order to indicate where you should complete the code. There are a total of 4 Questions There are a total of 4 Questions Question: The main object that you will manipulate is shown below: // imatrix struct, wraps a byte array of pixel data for an image typedef struct imatrix{ int width; int height; uint8_t**r; uint8_t** g; uint8_t**b; struct imatrix* ("add)(struct imatrix* this, struct imatrix* m2); struct imatrix* ("subtract)(struct imatrix* this, struct imatrix m2); struct imatrix* ("multiply)(struct imatrix* this, struct imatrix m2); struct imatrix* (*scale) (struct imatrix* this, int width, int height, float alpha); void (*write_image_to_rgb)(struct imatrix* this); void (*write_rgb_to_image) (struct imatrix* m); struct imatrix* (*set_rgb_image) (struct imatrix* this, uint8_t* new_rgb_image, int width, int height, int channels); l/internal image reference uint8_t* rgb_image; } imatrix; This structure consists of a number of fields. Three pointers r, g, b will be used to represent an image as three separate matrices, one for each channel. The structure also consists of a pointer rgb_image, which is a pointer to the image data written in a contiguous memory block. When you read an image as a command-line argument, the code loads the PNG images using the library. Your job in this first task will be to implement init_rgb(). You should use malloc() for dynamic memory allocation in order to initialize the r,g,b two-dimensional arrays. Question 2: Scaling an image: In part 2, you will write the code to scale the pixel values of an image. The associated function for this part of the project is imatrix* scale(imatrix* this, int width, int height, float alpha). Please read the documentation for this function and complete the code. You should test your code by calling it on an image in the main() function. Question 3: Add, Subtract : Here complete the two add and subtraction functions, imatrix* subtract(imatrix* m1, imatrix* m2, imatrix* add(imatrix* m1, imatrix* m2. These two functions take two imatrix and add/subtract them from each other. The result is written to a file and can be observed. To blend images together, try scaling them before adding or subtracting them. Question 4: Multiply: Finally, scaling and rotating an image can be done through matrix multiplication. Complete the function for matrix multiply imatrix* multiply(imatrix* m1, imatrix* m2), following the specification in the comments section above the function Please solve all 4 Questions. This should be in C language. You will be completing an image processing library that can process images using matrix operations. All images can be represented as a matrix of single-byte values, where a pixel at matrix location (i, j) is represented by a number between 0 and 255. A color image consists of three channels: a red channel (R), a green channel (G), and a blue channel (B), and the corresponding matrix information for an image include three matrixes, each with the same number of rows (height) and columns (width), one for each of the three channels. Together, these are passed to an image-writing library for outputting to a file and displaying. The code below designates parts of it with /NYOUR CODE HERE, in order to indicate where you should complete the code. There are a total of 4 Questions There are a total of 4 Questions Question: The main object that you will manipulate is shown below: // imatrix struct, wraps a byte array of pixel data for an image typedef struct imatrix{ int width; int height; uint8_t**r; uint8_t** g; uint8_t**b; struct imatrix* ("add)(struct imatrix* this, struct imatrix* m2); struct imatrix* ("subtract)(struct imatrix* this, struct imatrix m2); struct imatrix* ("multiply)(struct imatrix* this, struct imatrix m2); struct imatrix* (*scale) (struct imatrix* this, int width, int height, float alpha); void (*write_image_to_rgb)(struct imatrix* this); void (*write_rgb_to_image) (struct imatrix* m); struct imatrix* (*set_rgb_image) (struct imatrix* this, uint8_t* new_rgb_image, int width, int height, int channels); l/internal image reference uint8_t* rgb_image; } imatrix; This structure consists of a number of fields. Three pointers r, g, b will be used to represent an image as three separate matrices, one for each channel. The structure also consists of a pointer rgb_image, which is a pointer to the image data written in a contiguous memory block. When you read an image as a command-line argument, the code loads the PNG images using the library. Your job in this first task will be to implement init_rgb(). You should use malloc() for dynamic memory allocation in order to initialize the r,g,b two-dimensional arrays. Question 2: Scaling an image: In part 2, you will write the code to scale the pixel values of an image. The associated function for this part of the project is imatrix* scale(imatrix* this, int width, int height, float alpha). Please read the documentation for this function and complete the code. You should test your code by calling it on an image in the main() function. Question 3: Add, Subtract : Here complete the two add and subtraction functions, imatrix* subtract(imatrix* m1, imatrix* m2, imatrix* add(imatrix* m1, imatrix* m2. These two functions take two imatrix and add/subtract them from each other. The result is written to a file and can be observed. To blend images together, try scaling them before adding or subtracting them. Question 4: Multiply: Finally, scaling and rotating an image can be done through matrix multiplication. Complete the function for matrix multiply imatrix* multiply(imatrix* m1, imatrix* m2), following the specification in the comments section above the function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Finance The Role Of Data Analytics In Manda Due Diligence

Authors: Ps Publishing

1st Edition

B0CR6SKTQG, 979-8873324675

More Books

Students also viewed these Databases questions

Question

Do you think that project evaluations cost-justify themselves?

Answered: 1 week ago