Using Bi-cubic Interpolation - image

I read about it on Wikipedia, theory sounds good, but I don't know to apply to practice.
I have an small example like this one:
Original Image Matrix
1 2
3 4
If I want to double size the image, then the new matrix is
x x x x
x x x x
x x x x
x x x x
Now, the fun part is how to transfer old values in original matrix to the new matrix, I intend to do like this
1 x 2 x
x x x x
3 x 4 x
x x x x
Then applying the Bi cubic Interpolation on it (at this moment just forget about using 16 neighbor pixel, I don't have enough space to demonstrate such a large matrix here).
Now my questions are:
1. Do I do the data transferring (from old to new matrix) right? If not, what should it look like?
2. What should be the value of x variables in the new matrix? to me , this seems correct because at least we have some values to do the calculation instead of x notations.
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
3. Will all of the pixels in new matrix be interpolated? Because the pixels at the boundary do not have enough neighbor pixels to perform the calculation.
Thank you very much.

Interpolation means estimating a value for points that don't physically exist. You need to start with a coordinate system, so let's just use two incrementing integers for X position and Y position.
0, 0 1, 0
0, 1 1, 1
Your output requires 4x4 pixels which should be spaced at 0.5 intervals instead of the 1.0 intervals of the input:
-0.25,-0.25 0.25,-0.25 0.75,-0.25 1.25,-0.25
-0.25, 0.25 0.25, 0.25 0.75, 0.25 1.25, 0.25
-0.25, 0.75 0.25, 0.75 0.75, 0.75 1.25, 0.75
-0.25, 1.25 0.25, 1.25 0.75, 1.25 1.25, 1.25
None of the coordinates in the output exist in the input, so they'll all need to be interpolated.
The offset of the first coordinate of -0.25 is chosen so that the first and last coordinate will be equal distances from the edges of the input, and is calculated by the difference between the output and input intervals divided by 2. For example if you wanted a 10x zoom the interval is 0.1, the initial offset is (0.1-1)/2 and the points would be (-0.45, -0.35, -0.25, -0.15, ... 1.35, 1.45).
The Bicubic algorithm will require data for points outside of the original image. The easiest solution comes when you use a premultiplied alpha representation for your pixels, then you can just use (0,0,0,0) as the value for anything outside the image boundaries.

Related

Get X and Y positions of a Pixel given the HEIGHT , WIDTH and index of a pixel in FLATTENED array representing the image

Imagine you have this image
[[1 2 3] [4 5 6] [7 8 90]]
You flatten it into this format -
[1 2 3 4 5 6 7 8 90]
Now you are given the index of Pixel 90 to be 8.
How can you find that pixel 90 is in Row 3 and column 3?
OpenCL, similarly to other programming languages like C, C++, Java and so on, uses zero based indexing. So in this terms you are looking for Row 2 and Column 2.
Now to calculate which row that is we need to divide index position 8 by number of columns:
8 / 3 = 2
So in zero based indexing that is a second row.
Now to calculate which column that is we use modulo operator:
8 % 3 = 2
In the 2D case, a point (x,y) in a rectangle with the dimensions (sx,sy) can be represented in 1D space by a linear index n as follows:
n = x+y*sx
Converting the 1D index n back to (x,y) works as follows:
x = n%sx
y = n/sx
For the 3D case, a point (x,y,z) in the a box with dimensions (sx,sy,sz) can be represented in 1D as
n = x+(y+z*sy)*sx
and converted back to (x,y,z) like this:
z = n/(sx*sy);
temp = n%(sx*sy);
y = temp/sx;
x = temp%sx;
Note that "/" here means integer division (always rounds down the result) and "%" is the modulo operator.

Decompose 2 basic factors from a risk severity

I have 2 matrices of values:
Matrix A size 2x4 and matrix B size 2x4 as follows:
Main Matrix B Scales
0.00 2.50
2.50 5.00
5.00 11.25
11.25 25.00
the other matrix A is:
0.00 1.25
1.25 2.50
2.50 7.50
7.50 25.00
and can be of any values.
When we multiply 2 variant numbers of values from (0 to 5) we get a value that we can represent in matrix A levels (between the limits of any level). We can represent the outcome of multiplication of (x,y) into the second scale (matrix) to be on the same level (line 1 or line 2 or line 3 or line 4) by the following operation:
We get the known outcome of multiplied known numbers (x and y) and we see on what level it goes in matrix A (lets say level 3).
We need to map it to be on matrix B on the same level (level 3).
we map the value (result of X*y) that lies in level z in matrix A to matrix B by doing the following math: (result-minimum value of the level it lies in)/ (max level value - minimum level value)) and we get a percentage outcome. this outcome we use it as:
(the percentage outcome * (maximum limit of the mapped level in the matrix B -minimum limit of the mapped level in matrix B)/100) + minimum limit of the mapped level in matrix B
the new mapped value lies in matrix B on the same level where the original value lies in matrix A.
my question what are the best representative values of 2 variables a and b where a* b = the new mapped value in matrix B.
where a>= x and b>= y
Example:
x=2, y=2.8 the value is (2*2.8=5.6 this is lets say in Matrix B), now we do the calculation above and get the new value to be mapped into matrix A = 8.87. What we need is to decompose the new 8.87 to another x and y where when we multiply them we get the new value 8.87
This should be generic to any x and y values and any matrix A and B, etc.
more examples are: x= 1.5 y=3 severity = 1.5*3 = 4.5 this lies in level 3 of matrix B. the new mapped severity calculation is 7.499 and lies in level 3 of matrix A. the question what are the 2 numbers that we need to multiply (a,b) to get the number 7.499 on matrix A
more examples are: x= 3 y=2 severity = 3*2 = 6 this lies in level 3 of matrix B. the new mapped severity calculation is 9.374 and lies in level 3 of matrix A. the question what are the 2 numbers that we need to multiply (a,b) to get the number 9.374 on matrix A
more examples are: x= 1.2 y=2 severity = 1.2*2 = 2.4 this lies in level 2 of matrix B. the new mapped severity calculation is 4.799 and lies in level 2 of matrix A. the question what are the 2 numbers that we need to multiply (a,b) to get the number 4.799 on matrix A

Plot 2-d data in Matlab [duplicate]

This question already has an answer here:
Creating colormap at specific point and color weights at matlab
(1 answer)
Closed 5 years ago.
I have data like this :
x coordinate| y coordinate | Z
0.01 | 0.15 | 1
0.23 | 0.17 | 5
0.28 | 0.18 | 6
... ... ...
I want to plot all of these data in 2-d such that , in each point (x,y)
we have the corresponding intensity Z which i want to be depicted with a colour . Just like the function 'image' which already exists . But i have a problem that the aforementioned function of matlab plot in a uniform manner all the points. So if i have
x= [0 0.01 1];
y = [0 1];
'Z = [1 1 0;0 1 1];'
Then it will plot the corresponding densities at the (0,0) (0.5,0) , (1 ,0)... So it takes the max of x and the min of x and take uniform pieces .
I want to plot my data in specific points.
Any ideas , is there any other suitable function for this ?
How can i construct something like that ?
If your data is a grid that has sligth variations on the values, but still a grid, do:
surf(x,y,z,'linestyle','none');
axis tight;axis off; view(2)
Example output with
z=peaks;
[x,y]=meshgrid(1:49,1:49);
x=x+rand(size(x))*0.1;
y=y+rand(size(x))*0.1;

In Matlab Finding gradient of an image using first derivative

How can I do enhancements on an image using the first derivative.
I do not want to use operators like sobel..
I want to find the gradient using the first derivative
You're always free to define your own impulse response and then convolve that with your image.
diff1X =[1/2, 0, -1/2] %first derivative in the x direction
diff1Y = [1/2; 0; -1/2] %first derivative in the y direction
%These are finite difference approximations to the first derivative note they
%are time reversed because the convolution involves a time reversal
x = [1,2,3; 2,4,6; 4,8,12]
x =
1 2 3
2 4 6
4 8 12
conv2(x, diff1X, 'same') %same parameter cuts results to initial image size
ans =
1 1 -1
2 2 -2
4 4 -4
%note that the image is zero padded so the derivative at the edges reflects this
conv2(x, diff1Y, 'same')
ans =
1.0000 2.0000 3.0000
1.5000 3.0000 4.5000
-1.0000 -2.0000 -3.0000

Converting points into another coordinate system

There are 3 points in 3D space. There are 2 orthogonal coordinate systems with the same origin. I know coordinates of those 3 points in both coordinate systems. Given a new point with its coordinates in the first coordinate system, how can I find its coordinates in the second coordinate system?
I think it's possible to get a rotation matrix using given points which does this, but I did not succeed doing this.
You can do it using matrix inverses. Three matrix-vector multiplications (e.g. transforming three 3D vectors by a 3x3 matrix) is equivalent to multiplying two 3x3 matrices together.
So, you can put your first set of points in one matrix, call it A:
0 0 1 < vector 1
0 1 0 < vector 2
2 0 0 < vector 3
Then put your second set of points in a second matrix, call it C. As an example, imagine a transform that scales by a factor of 2 around the origin and flips the Y and Z axes:
0 2 0 < vector 1
0 0 2 < vector 2
4 0 0 < vector 3
So, if A x B = C, we need to find the matrix B, which we can find by finding the A-1:
Inverse of A:
0 0 0.5
0 1 0
1 0 0
The multiply A-1 x C (in that order):
2 0 0
0 0 2
0 2 0
This is a transform matrix B that you can apply to new points. Dot-product multiply the vector by the first column to get the transformed X, second column to get the transformed Y, etc.

Resources