Question

In: Computer Science

Javascript. Consider the following code fragment, that is supposed to compute the pixel value let c...

Javascript. Consider the following code fragment, that is supposed to compute the pixel value

let c = image.getPixel(x, y);
const m1 = (c[0] + c[1] + c[2]) / 3;
c = image.getPixel(x + 1, y);
const m2 = (c[0] + c[1] + c[2]) / 3;
image.setPixel(x, y, [m1 - m2, m1 - m2, m1 - m2]);
Give three pairs of pixel values (x, y) = [?, ?, ?] and (x+1, y) = [?, ?, ?] in the input image, for which this code does produce the correct result.

Pair 1.)

(x, y) = [?, ?, ?]

(x+1, y) = [?, ?, ?]

Pair 2.)

(x, y) = [?, ?, ?]

(x+1, y)= [?, ?, ?]

Pair 3.)

(x, y) = [?, ?, ?]

(x+1, y)= [?, ?, ?]

Note: that we consider the RGB values as  0-255 but for this assignment, I believe they want between 0-1

Solutions

Expert Solution

Okay lets see the value of M1 and M2 ,

These are the average value, of the pixels which tells us that this represents the black and white version of the image, so what we have to do here is either take a black and white picture or something that doesn't change by taking the average so it can be the same if that's the objective but since it's not necessarily mentioned.

The only play here is that we have to keep the value of M1-M2 in the range (0,255)

Anything that gives a output in a range of 0-255 so,

Pair 1

(x,y)= [123,167,167]

(x+1,y) = [98,67,65]

Pair 2

(x,y) = [255,255,255] the Complete black

(x+1,y) = [0,0,0] white so even after AVG it will remain the black only

Pair 3

(x,y) = [100,150,200]

(x+1,y) = [50,100,150]

Now the one thing here is that all the colours will have Equal proportions in the resulting image any pixel, and if we represent it with lesser data that is instead of (a,a,a) by (a) we can have a good black and white image.


Related Solutions

Write a code fragment in C language that tests the value of an integer num1. If...
Write a code fragment in C language that tests the value of an integer num1. If the value is 10, square num1. If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. Implement your code using SWITCH statements
Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that...
Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers. Assume that all data values and their addresses are kept in memory (at addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when they are operated on. Assume that values in registers are lost between iterations of the loop. Assume all addresses and words...
Given the following code fragment: x = (1,2,3) Match the syntax with the action: value =...
Given the following code fragment: x = (1,2,3) Match the syntax with the action: value = x{0} (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable value = x(0) (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable value = x[0] (a) value is bound to the value 1 (b) SyntaxError: invalid syntax (c) TypeError: 'tuple' object is not callable
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] =...
Determine the output using C++ of the following code fragment i) double *pt; double a[3] = {1.2, 2.3, 3.4}; pt = &a[1]; pt += 1; cout << *pt << endl; ii) int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { cout << num << "bb"; num *= 3; } cout << endl; i++; }
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code. int counter; int x = 5; int y = 6; for (counter =10; counter >0;counter--) IF(X==Y) Y = Y + 1 ; ELSE Y = Y + 2} Fill in the blanks in the following code: MOV__________ ;loop counter into r0-ten times round the loop MOV__________ ;Value of y loaded into r1 MOV__________ ;Value of x...
Consider the following code fragment and answer below 1. Read(fd, user entry, size of (user entry));...
Consider the following code fragment and answer below 1. Read(fd, user entry, size of (user entry)); 2. Comp=memcmp(userEntry, correct password, stream( user Entry)); 3. If (Comp!=0) 4. Return BAD_PASS - identify any 3 problems with code 8 for each defect describe: - A) what is this problem - B) how it can be found (code, review, Static analysis.....) - C) how it can be solved
For the following exercises, compute the value of the expression. C(12, 4)
For the following exercises, compute the value of the expression.C(12, 4)
For the following exercises, compute the value of the expression. C(8, 5)
For the following exercises, compute the value of the expression.C(8, 5)
For the following exercises, compute the value of the expression. C(26, 3)
For the following exercises, compute the value of the expression.  C(26, 3)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT