Related
My cs50 filter edges function is not working, it compiles ok but when i run check50 the first test (edges correctly filters middle pixel) us correct while the others are incorrect just by the last value, like this:
:( edges correctly filters pixel on edge
expected "213 228 255\n", not "213 228 140\n"
However, when I print the gx and gy for the red, green and blue alone, and the value of the squareroot, none of the values for the colors match.
now, this is my code for edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
int sr = 0;
int sb = 0;
int sg = 0;
int yr = 0;
int yb = 0;
int yg = 0;
struct RGBTRIPle
{
int rgbtRed;
int rgbtGreen;
int rgbtBlue;
};
struct RGBTRIPle copia[height][width];
struct RGBTRIPLe
{
int rgbtRed;
int rgbtGreen;
int rgbtBlue;
};
struct RGBTRIPLe copia2[height][width];
//Implementing Gx
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
sr = 0;
sb = 0;
sg = 0;
for (int m = i - 1; m <= i + 1; m++)
{
for (int c = j - 1; c <= j + 1; c++)
{
if (m >= 0 && m < height && c >= 0 && c < width)
{
if (c == j - 1)
{
if (m == i - 1 || m == i + 1)
{
sr += -1 * image[m][c].rgbtRed;
sb += -1 * image[m][c].rgbtBlue;
sg += -1 * image[m][c].rgbtGreen;
}
else
{
sr += -2 * image[m][c].rgbtRed;
sb += -2 * image[m][c].rgbtBlue;
sg += -2 * image[m][c].rgbtGreen;
}
}
if (c == j + 1)
{
if (m == i - 1 || m == i + 1)
{
sr += image[m][c].rgbtRed;
sb += image[m][c].rgbtBlue;
sg += image[m][c].rgbtGreen;
}
else
{
sr += 2 * image[m][c].rgbtRed;
sb += 2 * image[m][c].rgbtBlue;
sg += 2 * image[m][c].rgbtGreen;
}
}
else //c = j
{
sr += 0 * image[m][c].rgbtRed;
sb += 0 * image[m][c].rgbtBlue;
sg += 0 * image[m][c].rgbtGreen;
}
}
}
}
copia[i][j].rgbtRed = sr;
copia[i][j].rgbtGreen = sg;
copia[i][j].rgbtBlue = sb;
}
}
//Implementing Gy
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
yr = 0;
yb = 0;
yg = 0;
for (int m = i - 1; m <= i + 1; m++)
{
for (int c = j - 1; c <= j + 1; c++)
{
if (m >= 0 && m < height && c >= 0 && c < width)
{
if (m == i - 1)
{
if (c == j - 1 || c == j + 1)
{
yr += -1 * image[m][c].rgbtRed;
yb += -1 * image[m][c].rgbtBlue;
yg += -1 * image[m][c].rgbtGreen;
}
else
{
yr += -2 * image[m][c].rgbtRed;
yb += -2 * image[m][c].rgbtBlue;
yg += -2 * image[m][c].rgbtGreen;
}
}
if (m == i + 1)
{
if (c == j + 1 || c == j - 1)
{
yr += image[m][c].rgbtRed;
yb += image[m][c].rgbtBlue;
yg += image[m][c].rgbtGreen;
}
else
{
yr += 2 * image[m][c].rgbtRed;
yb += 2 * image[m][c].rgbtBlue;
yg += 2 * image[m][c].rgbtGreen;
}
}
else //c = j
{
yr += 0 * image[m][c].rgbtRed;
yb += 0 * image[m][c].rgbtBlue;
yg += 0 * image[m][c].rgbtGreen;
}
}
}
}
copia2[i][j].rgbtRed = yr;
copia2[i][j].rgbtGreen = yg;
copia2[i][j].rgbtBlue = yb;
}
}
//Implementing math operation to calculate resulting color
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int r = 0;
int g = 0;
int b = 0;
image[i][j].rgbtRed = (int) round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed *
copia2[i][j].rgbtRed)));
image[i][j].rgbtGreen = (int) round(sqrt((copia[i][j].rgbtGreen * copia[i][j].rgbtGreen) + (copia2[i][j].rgbtGreen *
copia2[i][j].rgbtGreen)));
image[i][j].rgbtBlue = (int) round(sqrt((copia[i][j].rgbtBlue * copia[i][j].rgbtBlue) + (copia2[i][j].rgbtBlue *
copia2[i][j].rgbtBlue)));
r = image[i][j].rgbtRed;
g = image[i][j].rgbtGreen;
b = image[i][j].rgbtBlue;
if (image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
if (image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
if (image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
}
}
return;
}
The problem you describe arises when you store round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed *copia2[i][j].rgbtRed))); into the variable image[i][j].rgbtRed(or any other variant thereof). This is because when calculating sqrt(gx^2 + gy^2) you are getting a number above 255. For example, you may get the integer value 395 after rounding. To store that value in to image[i][j].rgbtRed, C will store the value of 395 % 255, or 140, because the image cannot store values greater than 255, by definition.
This means that your if statements are useless, because the respective color values will never be greater than 255:
if (image[i][j].rgbtRed > 255)
{
image[i][j].rgbtRed = 255;
}
if (image[i][j].rgbtGreen > 255)
{
image[i][j].rgbtGreen = 255;
}
if (image[i][j].rgbtBlue > 255)
{
image[i][j].rgbtBlue = 255;
}
To solve this problem you have to cap the value before storing them into the image. A simple implementation of this would be by making a function called cap that returns 255 if an input is above 255.:
int cap(int rgb)
{
if (rgb > 255)
{
return 255;
}
else
{
return rgb;
}
}
You can then use this function in the following manner, which will solve your problem completely:
image[i][j].rgbtRed = cap(round(sqrt((copia[i][j].rgbtRed * copia[i][j].rgbtRed) + (copia2[i][j].rgbtRed * copia2[i][j].rgbtRed))));
image[i][j].rgbtGreen = cap(round(sqrt((copia[i][j].rgbtGreen * copia[i][j].rgbtGreen) + (copia2[i][j].rgbtGreen * copia2[i][j].rgbtGreen))));
image[i][j].rgbtBlue = cap(round(sqrt((copia[i][j].rgbtBlue * copia[i][j].rgbtBlue) + (copia2[i][j].rgbtBlue * copia2[i][j].rgbtBlue))));
This will also shorten your code, make it look cleaner, and avoid unnecessary repetition.
format of final.txt mind the spacing
I am trying to create a pie graph in gnu octave but i want it to run through a script file
i tried using
octave -persist<< EOF
[a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18] = textread( 'final.txt', '%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f ' ,'delimiter', ' ' ,1 );
pie([a1(2,1),a2(2,1),a3(2,1),a4(2,1),a5(2,1),a6(2,1),a7(2,1),a8(2,1),a9(2,1),a10(2,1),a11(2,1),a12(2,1),a13(2,1),a14(2,1),a15(2,1),a16(2,1),a17(2,1),a18(2,1)],[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],{"200","201","202","203","204","301","302","303","304","400","401","402","403","404","500","501","502","503"});
title(a0(2,1));
legend("200","201","202","203","204","301","302","303","304","400","401","402","403","404","500","501","502","503");
EOF
The above command work when i use them directly on octave command line
but i don't get any graph when i try to run through a script file
final.txt contents
VMinstance 200 201 202 203 204 301 302 303 304 400 401 402 403 404 500 501 502 503
dadamfl3w6v 683 0 0 0 0 0 1 0 669 0 0 0 0 0 0 0 0 0
92ssadz9qpq 41 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0
agcrarrcbqg 38 0 0 0 0 0 0 0 33 0 0 0 0 0 0 0 0 0
12t2bg6ws1j 25 0 0 0 0 0 0 0 17 0 0 0 0 0 0 0 0 0
gpusa121zdz 21 0 0 0 0 0 0 0 5520 0 0 0 0 0 0 0 0 0
dsad8urz24d 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
fggfbjrfkix 253 0 0 0 0 0 4 0 957 0 0 0 0 0 0 0 0 0
fqlie42dsxj 5 0 0 0 0 0 0 0 49 0 0 0 0 0 0 0 0 0
cbkajsbcj44 5 0 0 0 0 0 1 0 3 0 0 0 0 0 0 0 0 0
f4444fffffa 3968 0 0 0 0 0 4 0 3742 0 0 0 0 0 0 0 0 0
my output after textread on comandline
a0 =
{
[1,1] = VMinstance
[2,1] = dadamfl3w6v
[3,1] = 92ssadz9qpq
[4,1] = agcrarrcbqg
When I run your code with the final.txt file you've given, there are a lot of NaNs in the values that readtxt outputs. For example, a1 is entirely NaNs. Usually in Matlab/Octave, it is not a good idea to read the files like you do. I think you should remove the text in the first columun of your final.txt file, to obtain
200 201 202 203 204 301 302 303 304 400 401 402 403 404 500 501 502 503
683 0 0 0 0 0 1 0 669 0 0 0 0 0 0 0 0 0
Then, you just load it in a matrix A using
A=load("final.txt")
and use the appropriate indexation.
Edit:
Here is the ouptut I get when I run your code from the command line:
>> [a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18]=textread( 'final.txt','%s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f ' ,'delimiter', ' ' ,1 );
>>a1 =
200
NaN
NaN
NaN
Anyway, even if it works for you, what you are doing is just very bad practice. By using this c-style loading, you lose the abstraction, higher level flexibility, parallelisation and even nice syntax that matlab/octave have to offer.
Additionally, if you want to know to what type of data some row corresponds, just add a column to that row specifying and index into some dictionnary with your datatypes.
I have to create a function in Octave that receives a monochromatic image and a matrix with the original points and the destination points.
I've seen other answers but it's not what I have to do exactly.
This is what I did until now. The matrix "Entrada" are the entry points that I'm using to test.
Entrada = [1 1 50 50;
1 350 1 300;
390 1 390 1;
390 350 300 350];
L = [Entrada(1,3);
Entrada(1,4);
Entrada(2,3);
Entrada(2,4);
Entrada(3,3);
Entrada(3,4);
Entrada(4,3);
Entrada(4,4)]
A = [Entrada(1,1) Entrada(1,2) 1 0 0 0 -(Entrada(1,1) * Entrada(1,3)) -(Entrada(1,2) * Entrada(1,3));
0 0 0 Entrada(1,1) Entrada(1,2) 1 -(Entrada(1,1) * Entrada(1,4)) -(Entrada(1,2) * Entrada(1,4));
Entrada(2,1) Entrada(2,2) 1 0 0 0 -(Entrada(2,1) * Entrada(2,3)) -(Entrada(2,2) * Entrada(2,3));
0 0 0 Entrada(2,1) Entrada(2,2) 1 -(Entrada(2,1) * Entrada(2,4)) -(Entrada(2,2) * Entrada(1,4));
Entrada(3,1) Entrada(3,2) 1 0 0 0 -(Entrada(3,1) * Entrada(3,3)) -(Entrada(3,2) * Entrada(3,3));
0 0 0 Entrada(3,1) Entrada(3,2) 1 -(Entrada(3,1) * Entrada(3,4)) -(Entrada(3,2) * Entrada(3,4));
Entrada(4,1) Entrada(4,2) 1 0 0 0 -(Entrada(4,1) * Entrada(4,3)) -(Entrada(4,2) * Entrada(4,3));
0 0 0 Entrada(4,1) Entrada(4,2) 1 -(Entrada(4,1) * Entrada(4,4)) -(Entrada(4,2) * Entrada(4,4))]
AT = A';
X = (inv(AT*A))*AT*L;
T = [X(1,1) X(2,1) X(3,1);
X(4,1) X(5,1) X(6,1);
X(7,1) X(8,1) 1]
I have the transformation Matrix, and I have to associate the original points to the new image. I have to use the transformation matrix to every point and then use homogeneous coordinates correct?
Can anyone help me with this?
Regards
I'm currently trying to convert 44/7 to half-precision floating point format.
I'm not sure if I've done it correctly so far, so I'd really appreciate it if someone could have a look at it.
44/7 = 6,285714285714...
6 in dual -> 110;
0.285714 * 2 = 0,571428 -> 0
0.571428 * 2 = 1.142856 -> 1
0.142856 * 2 = 0.285714 -> 0
... -> 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1...
-> 110, 01001001001001
-> 1,1001001001001001 -> exponent: 2;
Bias + Exponent : 2+15 = 17 => 1 0 0 0 1
All stitched together: 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 1
I've never converted decimal to 16bit IEEE754, is this the correct way of converting it?
Thanks a lot!
Correct. As you might expect, it is quantized to 6.28515625.
0100011001001001(base 2) = 4649(base 16)
6.2857142857139996
= H(4649)
= F(40C92492)
= D(40192492 49249107)
= A(0X1.92492492491070P+2)
6.28515625
= H(4649)
= F(40C92000)
= D(40192400 00000000)
= A(0X1.92400000000000P+2)
Other data points:
+0. 0000
-0. 8000
-1. BC00
+1. 3C00
+2. 4000
+4. 4400
+8. 4800
+16. 4C00
+32768. 7800
+Max 7BFF 65504
+.5f 3800
+.25f 3400
+.125f 3000
+.0625f 2C00
+MinNorm 0400 +6.103515625e-05
-MinNorm 8400 -6.103515625e-05
+MinDenorm 0001 +5.9604644775390625e-08
-MinDenorm 8001 -5.9604644775390625e-08
+Infinity 7C00
-Infinity FC00
+NaN(0) 7E00
-NaN(0) FE00
If I have a 2d array like:
boolean[][] map = new boolean[50][50];
How can I set the outer edge of booleans to true only in a loop?
So, for the following array:
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
You would have:
1 1 1 1 1 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
I'm new to programming and I've been struggling to get this to work?
I thought possibly using 2 loops like:
for(int i = 0; i < map.length; i++)
{
map[i][0] = true;
map[i][map[0].length] = true;
}
for(int i = 0; i < map[0].length; i++)
{
map[0][i] = true;
map[map.length][i] = true;
}
But honestly I'm not sure if this is the correct approach?
for( int i = 0; i<maxx; i++)
{
for( int j = 0; j<maxy; j++)
{
if( i==0 || i == maxx-1 || j == 0 || j == maxy-1 )
{
//Write 1
}
else
{
//Write 0
}
}
}
This is probably not the best possible code, but it demonstrates easily how to achieve it:
Write something in all fields.
If the field is:
At the top or bottom of the 2d array
to the left or right
write a 1, otherwise a 0.
The question is, when it is at the top or bottom?
At the time the line-index (i) is either 0 or highest possible.
The same counts for the column index (j).
for( int i = 0; i<50; i++)
{
map[i][0] = 1;
map[i][49] = 1;
map[0][i] = 1;
map[49][i] = 1;
}
I'm assuming the structure is already initialized with 0s.
integer max = 50;
boolean[][] map = new boolean[max][max];
for ( integer x=0;x<max;x++) {
map[0,x] =1;
map[max-1,x] =1;
map[x,0] =1;
map[max-1,x] =1;
}
Problem: this initializes the corners more than once ..
In Java:
int firstRow = 0;
int lastRow = map.length - 1;
int width = map[0].length;
for (int i=0; i<width; i++) {
map[firstRow][i] = true;
}
System.arrayCopy (map[firstRow], 0, map[lastRow], 0, width);
int lastColumn = width - 1;
for (int i=1; i<lastRow; i++) {
map[i][0] = map[i][lastColumn] = true;
}
This is of course limited by the number of writes you must do, which are O(n) where n is the side length of the matrix, assuming the matrix is square.
You can of course simplify the code to only touch the outer elements:
for i in xrange(0, n - 1):
matrix[0][i] = true
matrix[i][-1] = true
matrix[-1][-(i + 1)] = true
matrix[-(i + 1)][0] = true
This does four writes per iteration of the loop. I think I did the indexing correctly now, the idea is to do the writes in this order, for the case where n=4 (apologies for the stunning ASCII graphics):
0120
2 1
1 2
0210
So, you can see that each side only goes from 0 to n - 2, inclusive. This is expressed in Python as for i in xrange(0, n -1).
In Python it is very easy
X=10
Y=5
m=[[1]*X]+[[1]+[0]*(X-2)+[1]]*(Y-2)+[[1]*X]
for row in m:
print row
outputs:
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Here is the breakdown
[1]*X # gives us a list of X 1's [1,1,1,1,1,1,1,1,1,1] in this case
[0]*(X-2) # gives us a list of (X-2) 0's [0,0,0,0,0,0,0,0] in this case
so
[[1]*x] # gives us an 1 by X array [[1,1,1,1,1,1,1,1,1,1]]
[[1]+[0]*(X-2)+[1]] # gives a 1 by X array [[1,0,0,0,0,0,0,0,0,1]]
we multiply the array above to give Y-2 identical lines
and then add another row of 1's to the bottom