I doing filter of images bmp in c. The pset required the Sobel Operator. I don't know where I being mistake.
Help me please.
I'm basically making a copy of my image (because the original will be changed.)
Then I take the 3x3 values to put in the formula
So we add and multiply
Finally I take the result and put it in the formula: square root (Gx ^ 2 + Gy ^ 2)
If it exceeds 255 it must be 255, because RGB goes up to 255 which is white
And if there is a broken number, round to the nearest
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Variáveis
RGBTRIPLE temp[height][width];
int GR[3][3];
int GG[3][3];
int GB[3][3];
int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
float resultR, resultG, resultB;
// Cópia temporária do original
for (int tempi = 0; tempi < height; tempi++)
{
for (int tempj = 0; tempj < width; tempj++)
{
temp[tempi][tempj].rgbtRed = image[tempi][tempj].rgbtRed;
temp[tempi][tempj].rgbtGreen = image[tempi][tempj].rgbtGreen;
temp[tempi][tempj].rgbtBlue = image[tempi][tempj].rgbtBlue;
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int countx = 0;
// Pegar 3X3
for (int x = i - 1; x < i + 2; x++)
{
int county = 0;
for (int y = j - 1; y < j + 2; y++)
{
if ((x < 0 || y < 0) || (x >= height || y >= width))
{
GR[countx][county] = 0;
GG[countx][county] = 0;
GB[countx][county] = 0;
}
else
{
GR[countx][county] = temp[x][y].rgbtRed;
GG[countx][county] = temp[x][y].rgbtGreen;
GB[countx][county] = temp[x][y].rgbtBlue;
}
county++;
}
countx++;
}
float sumxR = 0, sumyR = 0, sumxG = 0, sumyG = 0, sumxB = 0, sumyB = 0;
for (int ix = 0; ix <= 2; ix++)
{
for (int iy = 0; iy <= 2; iy++)
{
sumxR = sumxR + (GR[ix][iy] * Gx[ix][iy]);
sumxG = sumxG + (GG[ix][iy] * Gx[ix][iy]);
sumxB = sumxB + (GB[ix][iy] * Gx[ix][iy]);
sumyR = sumyR + (GR[ix][iy] * Gy[ix][iy]);
sumyG = sumyG + (GG[ix][iy] * Gy[ix][iy]);
sumyB = sumyB + (GB[ix][iy] * Gy[ix][iy]);
}
}
resultR = sqrt(sumxR * sumxR) + sqrt(sumyR * sumyR);
resultG = sqrt(sumxG * sumxG) + sqrt(sumyG * sumyG);
resultB = sqrt(sumxB * sumxB) + sqrt(sumyB * sumyB);
if (resultR > 255)
{
resultR = 255;
}
if (resultG > 255)
{
resultG = 255;
}
if (resultB > 255)
{
resultB = 255;
}
image[i][j].rgbtRed = round(resultR);
image[i][j].rgbtGreen = round(resultG);
image[i][j].rgbtBlue = round(resultB);
}
}
}
You need to changesqrt(sumxR * sumxR) + sqrt(sumyR * sumyR) to sqrt((sumxR * sumxR) + (sumyR * sumyR)) and they are not the same
I made a new post for the major changes in my post, but the problem now is that my image is returned as the same image entered. I believe it's a problem with the newimage variable and swapping it with the old image variable but I don't know why.
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
RGBTRIPLE newimage[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int redx = 0;
int greenx = 0;
int bluex = 0;
int redy = 0;
int greeny = 0;
int bluey = 0;
for (int k = i - 1; k <= i + 1 && k < height; k++)
{
for (int m = j - 1; m <= j + 1 && m < width; m++)
{
if (k != -1 && m != -1)
{
redx += image[k][m].rgbtRed * gx[k-(i-1)][m-(j-1)];
greenx += image[k][m].rgbtGreen * gx[k-(i-1)][m-(j-1)];
bluex += image[k][m].rgbtBlue * gx[k-(i-1)][m-(j-1)];
redy += image[k][m].rgbtRed * gy[k-(i-1)][m-(j-1)];
greeny += image[k][m].rgbtGreen * gy[k-(i-1)][m-(j-1)];
bluey += image[k][m].rgbtBlue * gy[k-(i-1)][m-(j-1)];
}
}
}
int finalred = round(sqrt((redx * redx) + (redy * redy)));
int finalgreen = round(sqrt((greenx * greenx) + (greeny * greeny)));
int finalblue = round(sqrt((bluex * bluex) + (bluey * bluey)));
if (finalred > 255)
{
finalred = 255;
}
if (finalgreen > 255)
{
finalgreen = 255;
}
if (finalblue > 255)
{
finalblue = 255;
}
newimage[i][j].rgbtRed = finalred;
newimage[i][j].rgbtGreen = finalgreen;
newimage[i][j].rgbtBlue = finalblue;
}
}
image = newimage;
return;
}
I'm writing a program calculating the shortest way from point A to point B.
I have a map (matrix) with values:
0 is block (wall, no way to pass);
1 is free way (you can pass);
2 is start point;
In the code below I declare 2 arrays: an array " map"and changed array "visited" while running program demonstrating visited points.
I check the cells in 4 directions (not diagonals) for 1 or 0. If it's 1 (possible to pass), I increase the counter for 1. For do not count the previous cell I'm trying to avoid it by the condition. I realized that in two one-dimensional arrays {1 0 -1 0} and { 0, 1, 0, -1 } to check neighbor points (what mean i check [i+1][j], [i-1][j], [i][j+1] and [i][j-1]).
As a result I wanna see "visited" matrix with a few lines which shows the way to reach to the point B (1, 2, 3, ... 15). I wanna find the way to map[7][7] point.
Right now the error here that I do count++ for the previous position. How to avoid that?
Thank you.
P.S. I wrote a few functions implementing a new array with 0 values, counting free to go cells and printing arrays.
main.c:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 8
#define HEIGHT 8
int mapZero(int map[WIDTH][HEIGHT]);
int mapPrint(int map[WIDTH][HEIGHT]);
int mapInit(int map[WIDTH][WIDTH]);
int findFreeToGoCells(int map[WIDTH][HEIGHT]);
int main(int argc, char * argv[])
{
bool stop;
unsigned int count;
unsigned int max;
int visited[WIDTH][HEIGHT];
int map[WIDTH][HEIGHT] =
{
{ 1, 1, 1, 1, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1, 0 },
{ 1, 0, 1, 1, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 1 },
{ 0, 1, 1, 1, 1, 1, 1, 1 },
};
mapZero(visited);
printf("Matrix of zeroed-visited cells:\n\n");
mapPrint(visited);
printf("Matrix of the map:\n\n");
mapPrint(map);
printf("Free to go cells: %d\n\n", findFreeToGoCells(map));
max = WIDTH * HEIGHT - 1;
visited[0][0] = map[0][0];
count = 0;
visited[0][0] = 0;
int di[4] = { 1, -1, 0, 0 };
int dj[4] = { 0, 0, 1, -1 };
//do
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (visited[i][j] == count)
{
for (int k = 0; k < 4; ++k)
{
int i_check = i + di[k];
int j_check = j + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] != 0))
{
visited[i_check][j_check] = count + 1;
}
}
count++;
}
}
}
}// while (visited[7][7] == 0);
if (count > max + 99999)
printf("The way couldn't be found\n");
else
{
printf("Matrix of visited cells:\n\n");
mapPrint(visited);
printf("Free to go cells from [0][0] to [7][7]: %d\n", findFreeToGoCells(visited));
}
/*************************************************************************************/
/*************************************************************************************/
int len;
int x = 7;
int y = 7;
int x_path[WIDTH * HEIGHT];
int y_path[WIDTH * HEIGHT];
len = visited[7][7];
count = len;
while (count > 0)
{
x_path[count] = x;
y_path[count] = y;
count--;
for (int k = 0; k < 4; ++k)
{
int i_check = x + di[k];
int j_check = y + dj[k];
if ((i_check >= 0 && i_check < WIDTH) && (j_check >= 0 && j_check < HEIGHT) && (map[i_check][j_check] == count))
{
x = x + di[k];
y = y + dj[k];
break;
}
}
}
x_path[0] = 0;
y_path[0] = 0;
printf("\nThe shortest way consist of %d cells\nThere are %d the shortest way to reach th the final point\n\n", len, findFreeToGoCells(visited)-len);
system("pause");
return 0;
}
int mapZero(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
map[i][j] = 0;
}
}
return 0;
}
int mapPrint(int map[WIDTH][HEIGHT])
{
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
printf("%2d ", map[i][j]);
}
printf("\n\n");
}
printf("\n");
return 0;
}
int findFreeToGoCells(int map[WIDTH][HEIGHT])
{
int count = 0;
for (int i = 0; i < WIDTH; ++i)
{
for (int j = 0; j < HEIGHT; ++j)
{
if (map[i][j] != 0) count++;
}
}
return count;
}
Result:
So to start this off, this is not the complete program. I am working on it and just ran into this problem. The first 3 times I call strcpy (in the first for-loop) it compiles without problem. However, the fourth through sixth times (in the second for-loop) I get the error message "too few arguments in function to call", even though the arguments are the same as in the first for-loop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
#define ESC 27
struct lag {
char namn[20];
int gjorda;
int inslappta;
int poang;
};
int main(void) {
struct lag temp, serie[] = {
{ "Bryn\204s", 0, 0, 0 },
{ "Djurg\206rden", 0, 0, 0 },
{ "Fr\224lunda", 0, 0, 0 },
{ "F\204rjestad", 0, 0, 0 },
{ "HV 71", 0, 0, 0 },
{ "Link\224ping", 0, 0, 0 },
{ "Lule\206 ", 0, 0, 0 },
{ "MODO ", 0, 0, 0 },
{ "R\224gle", 0, 0, 0 },
{ "Skellefte\206", 0, 0, 0 },
{ "S\224dert\204lje", 0, 0, 0 },
{ "Timr\206", 0, 0, 0 }
};
int i, j, k, hemma, borta;
srand((unsigned)time(NULL));
do {
for (k = 0; k <= 10; k += 2)
{
hemma = rand() % 8;
borta = rand() % 8;
serie[k].gjorda = +hemma;
serie[k].inslappta = +borta;
serie[k + 1].gjorda = +borta;
serie[k + 1].inslappta = +hemma;
printf("%s - %s \t \t %d - %d \n", serie[k].namn, serie[k + 1].namn, hemma, borta);
}
if (hemma > borta)
serie[i].poang = +3;
else if (hemma == borta)
{
serie[i].poang = +1;
serie[i + 1].poang = +1;
}
else if (hemma < borta)
serie[i + 1].poang = +3;
for (i= 0; i < 11; i++)
for (j = i+1;j< 12; j++)
if (serie[j].poang < serie[i].poang)
{
temp.poang = serie[i].poang;
serie[i].poang = serie[j].poang;
serie[j].poang = temp.poang;
temp.gjorda = serie[i].gjorda;
serie[i].gjorda = serie[j].gjorda;
serie[j].gjorda = temp.gjorda;
temp.inslappta = serie[i].inslappta;
serie[i].inslappta = serie[j].inslappta;
serie[j].inslappta = temp.inslappta;
strcpy(temp.namn, serie[i].namn); //These compile
strcpy(serie[i].namn, serie[j].namn);
strcpy(serie[j].namn, temp.namn);
}
for (i = 0; i < 11; i++)
for (j = i + 1; j< 12; j++)
if (serie[j].poang == serie[i].poang)
if ((serie[j].gjorda - serie[j].inslappta) < (serie[i].gjorda - serie[i].inslappta))
{
temp.poang = serie[i].poang;
serie[i].poang = serie[j].poang;
serie[j].poang = temp.poang;
temp.gjorda = serie[i].gjorda;
serie[i].gjorda = serie[j].gjorda;
serie[j].gjorda = temp.gjorda;
temp.inslappta = serie[i].inslappta;
serie[i].inslappta = serie[j].inslappta;
serie[j].inslappta = temp.inslappta;
strcpy_s(temp.namn, serie[i].namn); //These don't
strcpy_s(serie[i].namn, serie[j].namn);
strcpy_s(serie[j].namn, temp.namn);
}
} while (_getch() != ESC);
return 0;
}
Please, help me to find and print all square submatrices of square matrix from big to small square matrices in C programming language
I wrote code that works wrong:
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i,j;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--)
{
for(i = 0; i < sub_mtrx_size; i++)
{
for(j = 0; j < sub_mtrx_size; j++)
{
printf("%3d ", mat[i][j]);
}
printf("\n");
}
printf("\n");
}
return 0;
Here I need to find all 8x8, 7x7, 6x6, 5x5, 4x4, 3x3 and 2x2 submatrices.
Your code was just printing a single sub-matrix for each size, positioned in the upper-left corner of the matrix. You need to add i and j offsets to get the sub-matrices at all positions:
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
for(sub_mtrx_size = mtrx_size; sub_mtrx_size > 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}
Java implementation for a general nxm matrix:
private static void printSubMatrix(int[][] mat) {
int rows=mat.length;
int cols=mat[0].length;
//prints all submatrix greater than or equal to 2x2
for (int subRow = rows; subRow >= 2; subRow--) {
int rowLimit = rows - subRow + 1;
for (int subCol = cols; subCol >= 2; subCol--) {
int colLimit = cols - subCol + 1;
for (int startRow = 0; startRow < rowLimit; startRow++) {
for (int startCol = 0; startCol < colLimit; startCol++) {
for (int i = 0; i < subRow; i++) {
for (int j = 0; j < subCol; j++) {
System.out.print(mat[i + startRow][j + startCol] + " ");
}
System.out.print("\n");
}
System.out.print("\n");
}
}
}
}
}
#include <stdio.h>
int main() {
int mtrx_size = 8;
int mat[8][8] = {
{ 1, 2, 3, 4, 5, 6, 7, 8},
{ 9,10,11,12,13,14,15,16},
{17,18,19,20,21,22,23,24},
{25,26,27,28,29,30,31,32},
{33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48},
{49,50,51,52,53,54,55,56},
{57,58,59,60,61,62,63,64}
};
int i, j, ioff, joff, off_cnt;
int sub_mtrx_size;
/* if we make terminating condition sub_mtrx_size>=1 then we will have all
possible square sub matrices */
for(sub_mtrx_size = mtrx_size; sub_mtrx_size >= 1 ; sub_mtrx_size--) {
off_cnt = mtrx_size - sub_mtrx_size + 1;
for (ioff = 0; ioff < off_cnt; ioff++) {
for (joff = 0; joff < off_cnt; joff++) {
for (i = 0; i < sub_mtrx_size; i++) {
for (j = 0; j < sub_mtrx_size; j++) {
printf("%3d ", mat[i+ioff][j+joff]);
}
printf("\n");
}
printf("\n");
}
}
}
return 0;
}