Question

Implement optimized C/C++ code using code optimization techniques

Consider an image to be represented as a two-dimensional matrix M, where Mi,j denotes the value of M[i][j] th pixel of M. Pixel values are triples of red, green, and blue (RGB) values. We will only consider square images. Let N denote the number of rows (or columns) of an image (NxN square image). Rows and columns are numbered, in C-style, from 0 to N-1. Given this representation, the rotate operation can be implemented as the combination of the following two matrix operations:

 Transpose: For each (i,j) pair, Mi,j and Mj,i are interchanged.

 Exchange rows: Row i is exchanged with row N-1-i 1)

First task is to rotate a given image counterclockwise in 90 degree. Use the model image given in the assignment folder for rotation. To do that, first transpose the image then exchange the rows. These operations are illustrated in the following figure. Notice how red pixel moved during matrix transpose and exchange rows matrix operations.

Rotate by 90 (counter-clockwise) (0,0) (0,0) Transpose Exchange Rows Figure 1. Rotation of an image by 90 degree clockwise

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

Please refer to code screenshots

code screenshot

1 #include <iostream> #define N 4 4 using namespace std; 8 6 // function to transpose of A[][] 7- void transpose(int A[][N])34 35int main() { 36 // matrix for testing functions int A[N][N] = { {10, 10, 10, 10}, {20, 20, 20, 20}, 30, 30, 30, 30}, {40

Output

Matrix after rotation 10 20 30 40 10 20 30 40 10 20 30 40 10 20 30 40 ... Program finished with exit code o Press ENTER to ex

Code to copy

#include <iostream>
#define N 4

using namespace std;

// function to transpose of A[][]
void transpose(int A[][N]) {
   int B[N][N]; // grid to store matrix
   for (int i = 0; i < N; i++){
       for (int j = 0; j < N; j++){
           B[i][j] = A[j][i]; // getting transpose
       }
   }
          
   for(int i = 0; i < N; i++){
   for(int j = 0; j < N; j++){
   A[i][j] = B[i][j]; // updating grid A
   }
   }
}

// function to exchange rows
void exchangeRows(int A[][N]){
for(int i = 0; i < N / 2; i++){
for(int j = 0; j < N; j++){
int p = N - 1 - i; // row to exchange with
// exchanging rows using swapping of elements
int temp = A[i][j];
A[i][j] = A[p][j];
A[p][j] = temp;
}
}
}

int main() {
// matrix for testing functions
   int A[N][N] = {
   {10, 10, 10, 10},
                   {20, 20, 20, 20},
                   {30, 30, 30, 30},
                   {40, 40, 40, 40}
   };


   transpose(A); // calling function to get transpose of matrix
   exchangeRows(A); // calling function to get exhange of rows of matrix

cout<<"Matrix after rotation\n";
  
// displaying matrix after rotation
   for (int i = 0; i < N; i++) {
       for (int j = 0; j < N; j++){
       cout<<A[i][j]<<' ';
       }
       cout<<'\n';
   }

   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Implement optimized C/C++ code using code optimization techniques Consider an image to be represented as a...
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
  • convert the for loop into x86 assembly..             int[][] rotateMatrixBy90Degree(int[][] matrix, int n) { //We need...

    convert the for loop into x86 assembly..             int[][] rotateMatrixBy90Degree(int[][] matrix, int n) { //We need to run the loop for only half the number of rows as it represents one quarter or quadrant for (int layer = 0; layer < n / 2; layer++) { //In every iteration, we reduce number of columns to be swapped by computing first and last column index. We are rotating counter clockwise.                                     int first = layer; //start column pixel of current row...

  • How do I compile c++ code in terminal. Below i have some code to apply Gaussian...

    How do I compile c++ code in terminal. Below i have some code to apply Gaussian Filter on an image using OpenCV. I have tried compling using the code g++ Project2.cpp -o Project2 `pkg-config --cflags --libs opencv` && ./Project2 and it gives me a whole list of errors saying directory not found. --------------------------------------------------------------------------------------------------------- #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/core/core.hpp> #include <iostream> using namespace cv; using namespace std; int main() { Mat image = imread("//u//oowens//First.jpg"); int rows=image.rows; int cols=image.cols; if (image.empty())...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Please code in C++. link to continue the code is this below or you can make...

    Please code in C++. link to continue the code is this below or you can make your own code if you wish(fix any mistakes if you think there are any in it): cpp.sh/3qcekv 3. Submit a header file (project3.h), a definition file (project3.cpp), a main file (main.cpp), and a makefile to compile them together. Make sure to run the command make and produce an application file and include it with your submission. For this project, you are required to create...

  • from PIL import Image import random # NOTE: Feel free to add in any constant values...

    from PIL import Image import random # NOTE: Feel free to add in any constant values you find useful to use BLACK = (0, 0, 0) WHITE = (255, 255, 255) # NOTE: Feel free to add in any helper functions to organize your code but # do NOT rename any existing functions (or else, autograder # won't be able to find them!!) # NOTE: The following function is already completed for you as an example # You can use...

  • use MATLAB to upload the following: an image that you want to process (can be taken...

    use MATLAB to upload the following: an image that you want to process (can be taken yourself or downloaded from the internet) a script that processes the image in TWO ways. manipulates the colors averages pixels together Please make sure the script displays the images (like how I did with the 40 and 80 pixel averaging) so I can easily compare them to the original. Make sure to COMMENT your code as well. Homework 13 Please upload the following: an...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • Psuedocode works! DP is dynamic programming for this algorithm Problem 4.2. (Difficulty 3) Seam carving is...

    Psuedocode works! DP is dynamic programming for this algorithm Problem 4.2. (Difficulty 3) Seam carving is a real-world application of DP for content- aware image resizing. The simplest way to reduce the size of an image is cropping and scaling, i.e. cutting out parts of the image and scaling down the size. However, cropping leaves visible crop lines of incontinuity while scaling reduces the details of the image. We would like to intelligently reducing the size while accounting for the...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...

    Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...

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