2d arrays and image smoothing in c [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have to write a program that " will take in an image as input, represented by 2d array pixel values(for simplicity, each pixel can be represented by an integer). Output the resulting smoothed image by applying the mean filter to every pixel in the array".
I'm just learning about arrays, but I am lost as to how to even start this program. Whenever I search this topic it gets very confusing because every example or concept that I find is using or talking about actual images. Since my program is using integers, I'm having a hard time distinguishing what is needed and what is not. Basically I understand the premise(at least i think I do), each number must take the mean value from the 4 numbers around it, but outside the basic concept, I'm at a loss as to what needs to be done. Any help, suggestions, or examples would be great.
thanks

A key concept:
int values[20][20];
float intermediates[20][20];
for (int y = 1; y < 19; y++)
for (int x = 1; x < 19; x++)
intermediates = (float)values[y][x];
int means[20][20];
for (int y = 1; y < 19; y++)
for (int x = 1; x < 19; x++)
means[y][x] = (int) ( (float) (intermediates[y-1][x-1] + intermediates[y-1][x] + intermediates[y-1][x+1] + intermediates[y][x-1] + intermediates[y][x] + intermediates[y][x+1] + intermediates[y+1][x-1] + intermediates[y+1][x] + intermediates[y+1][x+1]) / 9.0F); // Divisor is 9 because we added nine values and we're getting the mean
Now there are 4 corner cases: intermediates[0][0], intermediates[0][19], etc. and then all the sides.
The values were copied into "intermediates" first because I didn't want to put (float)in front of everything.

0) Load the image? or are you given the integer array?
1) We start with a 2D array, generate a copy: int I[N][M]; int New[N][M];
2) Loop through the values:
for (i = 0; i < N; i++){
for (j = 0; j < M; j++){
3) Do your stuff
New[i][j] = sum(I[i-1][j] + I[i][j-1]...)/4;
note, beware (i or j-1) going < 0 and (i or j+1) > N or M. deal with these boundary conditions as you see fit.
Obviously this is the simplest way, but it looks like you're in a class so it should be fine

Related

Tetris function move right in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Currently i have the move left function
int8_t move_piece_left(piece_type* piecePtr) {
int8_t row;
/*
* Check if piece is all the way to the left If so, return.
* (Remember, column number is based on bit position - higher
* numbers to the left.
*/
if(piecePtr->right_column + piecePtr->x_dimension >= BOARD_WIDTH) {
return 0;
}
/*
* Make the move.
*/
for(row=0; row < piecePtr->y_dimension; row++) {
piecePtr->rowdata[row] <<= 1;
}
piecePtr->right_column++;
return 1;
}
I assumed for the move right it would be a simple change, which I am sure it is but I don't think its going so well. So obviously I need to check if the piece is all the way to the right not the left, and perhaps its
piecePtr->right_column--;
To move right since left is ++ right would be -- I think?
I've tried a few changes here and there but not much has worked, so I am beginning to think I'm not fully understanding the code.
Can someone give a more of a deeper explain what the code is doing (I can read the comments I wanted a more of an in depth explanation).
EDIT there are appropriate checks in order to move left or right. e.g. checking if a piece is there
rowData seems to be key here: It appears to contain all pixels of the piece, in binary form (one integer per row). So, if you move the piece to the right, you have to shift its values right instead of left, in addition to changing the test and incrementation.
Also note that this code does not check for existing pieces in the way: It only checks for bounds.
The moving code shold be something like this:
int8_t move_piece_right(piece_type* piecePtr) {
int8_t row;
/*
* Check if piece is all the way to the right If so, return.
* (Remember, column number is based on bit position - higher
* numbers to the left.
*/
if(piecePtr->right_column == 0) {
return 0;
}
/*
* Make the move.
*/
for(row=0; row < piecePtr->y_dimension; row++) {
piecePtr->rowdata[row] >>= 1;
}
piecePtr->right_column--;
return 1;
}

While loop increments in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am trying to make my "Steps" or increments in a while loop such that my values of are like N=2, 4, 8, 16... basically powers of 2 till 2^20. I have tried to do
for(j=1;j<=20;j++){
m=pow(2,2*i);
MAX=pow(2,20);
INC=pow(2,i);
while(m<=MAX){
then have my code running inside this. But in the output it gives me 2,4,6,8,10,12.. does anyone know what the right way is to do this?
Thanks!
You can start with a value of 2 for m and multiply m by two every iteration. As an optimization you can replace the multiplication by two with an left shift:
int MAX = pow(2, 20);
int m = 2;
while(m <= MAX) {
//do your work
m <<= 1;
}
In each iteration of the while loop; multiply your variable by two.
int m = 2;
while(m < maxpow)
{
m = m * 2;
printf("%d\n", m);
}

Why for loop does not run by other than int variables? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Why for loop does not run by other than int variables? I tried to run for loop by taking float variable but it does not run by any other variables than int type?
You can use a for loop with integer variables, floating-point variables, even no variables at all.
int i;
for(i = 0; i < 10; i++) continue;
float f;
for(f = 0.0; f < 5; f += 0.5) continue;
for(;;) break;
But see What Every Computer Scientist Should Know About Float-Point Arithmetic for why you should think twice before using example 2.
You should be able to do it by using the STEP command
float X = 0;
//
//increase in steps of 1 x 1 thousandth
for (X = 1; X <= 100; X += 0.001) {
// DISPLAY YOUR RESULT maybe using: Math.Round(X, 3)
}

Range queries for strings [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My order is defined as 'A < a < B < b ...< Z < z'.
I have to find if a given string is in range or not.
Ex. If my range is AaA - BaB, AA or AaaB is inthe range, but not CbAA.
I am looking for any pointers, ideas, suggestions to help me start. I will implement this in C.
So all you need to implement is a single function that compares two strings according to your rules. It is kind of modified lexicograogical sorting:
int compare_letters(char x, char y) {
char lx = tolower(x);
char ly = tolower(y);
if (lx != ly) {
return lx < ly;
} else {
return x < y;
}
}
int smaller(const char* a, const char* b) {
.. use the above function ...
}
Now make use of the above function and to check if a given string x is in the range (a,b), check if smaller(a, x) and smaller(x, b). That's it.
Some tips on the function smaller - compare the strings char by char and if the two chars differ, return their compare_letter. If one of the strings runs out of letters, consider it smaller.

convert from 1d to 2d matrix using arithmetic pointer in c language [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
how can i convert 1 dimension to 2 dimensional's matrix addressing and 1 d block addressing with pointer arithmetic in c programming language.
can anybody help please and thanks in advance:)
Suppose you want a 10x15 matrix (10 rows, 15 columns), for allocating memory:
int numRows = 10;
int numColumns = 15;
int *matrix = malloc(sizeof(int) * numRows * numColumns);
To access any element, multiply the desired row by the number of columns and add the column number:
//access row 3, column 5:
int value = matrix(3 * numColumns + 5);
Thats it.
int** matrix = malloc(sizeof(int*) * COLUMN_SIZE);
matrix[0] = malloc(sizeof(int) * COLUMN_SIZE * ROW_SIZE);
int* temp = matrix[0];
for (int i=1; i < COLUMN_SIZE; i++)
{
matrix[i] = temp;
temp += ROW_SIZE;
}
Now you'll have a linear array in matrix[0] that can also be addressed using either a linear index like matrix[0][INDEX] where index is between 0 and (COLUMN_SIZE * ROW_SIZE), or you can address it using matrix[COLUMN_INDEX][ROW_INDEX].
When the linear 1d index is called index and the 2d indexes row and col, the formula to transfer between them is
index = row * (MAX_ELEMENTS_PER_ROW) + col;
and the other direction
row = index / MAX_ELEMENTS_PER_ROW;
col = index % MAX_ELEMENTS_PER_ROW;
The pointer arithmetic is done by the + on a pointer and [] operator.
The actual coding I give to you as homework ;-).

Resources