Modifying bilinear interpolation matlab code to enlarge an image instead - matlab

I am trying to modify this code to enlarge an image instead of shrinking it by when I change the
eliminate the factor line and made shrinking factor= 2 and multiply by s where s is used, I didn't get the right output. Can anyone point me to what I am doing wrong.
Here is the code:
image=imread('b100.jpg');
factor=1.5;
shriking_factor= 1/factor;
[r c d] = size(image);
rn = floor(shriking_factor*r);
cn = floor(shriking_factor*c);
s = shriking_factor;
im_zoom = zeros(rn,cn,d);
for i = 1:rn;
x1 = cast(floor(i/s),'uint16');
x2 = cast(ceil(i/s),'uint32');
if x1 == 0
x1 = 1;
end
x = rem(i/s,1);
for j = 1:cn;
y1 = cast(floor(j/s),'uint32');
y2 = cast(ceil(j/s),'uint16');
if y1 == 0
y1 = 1;
end
ctl = image(x1,y1);
cbl = image(x2,y1);
ctr = image(x1,y2);
cbr = image(x2,y2);
y = rem(j/s,1);
tr = (ctr*y)+(ctl*(1-y));
br = (cbr*y)+(cbl*(1-y));
im_shrink(i,j) = (br*x)+(tr*(1-x));
end
end
image_shrink = cast(im_shrink,'uint8');
imshow(image_shrink)

Related

Matlab Code for Linear System by Central Difference Method

I have a linear system Ay = b, which is created by matrix looks like this:
Here attempt to find the curves based on the matrix in the image description:
n = 10;
x0 = 0;
xn = 1;
h = 1/n;
y0 = 0;
y1 = 0;
x = zeros(1:n-1);
for i = 1:n-1;
x(i) = i*h
end
A =zeros(n-1);
for j = 1:n-2;
A(j,j+1) = (1+h/2);
A(j,j) = (h*exp(x(j))-2);
A(j+1,j) = (1-h/2);
end
A(n-1,n-1) = (h*exp(x(n-1))-2);
b = zeros(1,n-1); %Right-hand side vector
for i = 1:n-1
b(i)=h^2*((exp(x(i))-pi^2)*sin(pi*x(i))+pi*cos(pi*x(i)));
end
b=b';
y = zeros(1,n-1);
y = inv(A)*b % Solving for y
figure
plot(x,y,x,sin(x))
This is code that I create but the curves disappear, anyone can help me to check my code?

Why do the plot and patch commands both not execute in Matlab?

So I was tasked with plotting the Golden Rectangles along with the Golden Spiral over top of the rectangles. We were given the code for the rectangles, for the "spiral" I used multiple arcs from this forum post, and I can only use if and for statements. The code for the rectangles works and the code for the spiral almost works, but what I really need is to get the patch to display behind the plot. This is the code that I have now:
clear variables; close all; clc
phi = (1+sqrt(5))/2;
x0 = 0;
y0 = 0;
for i = 1:8
edgeLength = 1/(phi^(i-1));
moveLength = 1/(phi^i);
modStep = mod(i,4);
if (modStep == 1) %move W, orient NW
x0 = x0 - moveLength;
sx = [x0 x0 x0-edgeLength x0-edgeLength];
sy = [y0 y0+edgeLength y0+edgeLength y0];
%spiral
P1 = [(x0-edgeLength);y0];
P2 = [x0;(y0+edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 1
vx1 = v(1,:);
vy1 = v(2,:);
elseif i == 5
vx6 = (v(1,:)+(1/(phi^5)));
vy6 = (v(2,:)+(1/(phi^5)));
end
end
if (modStep == 2) %move N, orient NE
y0 = y0 + moveLength;
sx = [x0 x0+edgeLength x0+edgeLength x0];
sy = [y0 y0 y0+edgeLength y0+edgeLength];
%spiral
P1 = [x0;(y0+edgeLength)];
P2 = [(x0+edgeLength);y0];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 2
vx2 = v(1,:);
vy2 = (v(2,:)+(1/(phi^2)));
elseif i == 6
vx5 = (v(1,:)+(1/(phi^5)));
vy5 = (v(2,:)+(1/(phi^6)));
end
end
if (modStep == 3) %move E, orient SE
x0 = x0 + moveLength;
sx = [x0 x0 x0+edgeLength x0+edgeLength];
sy = [y0 y0-edgeLength y0-edgeLength y0];
%spiral
P1 = [(x0+edgeLength);y0];
P2 = [x0;(y0-edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 3
vx3 = (v(1,:)+(1/(phi^3)));
vy3 = (v(2,:)+(1/(phi^2)));
elseif i == 7
vx7 = (v(1,:)+(1/(phi^7)));
vy7 = (v(2,:)+(1/(phi^6)));
end
end
if (modStep == 0) %move S, orient SW
y0 = y0 - moveLength;
sx = [x0 x0-edgeLength x0-edgeLength x0];
sy = [y0 y0 y0-edgeLength y0-edgeLength];
%spiral
P1 = [(x0-edgeLength);y0];
P2 = [x0;(y0-edgeLength)];
P0 = [x0;y0];
n = 1000;
v1 = P1-P0;
v2 = P2-P0;
c = det([v1,v2]);
a = linspace(0,atan2(abs(c),dot(v1,v2)),n);
v3 = [0,-c;c,0]*v1;
v = v1*cos(a)+((norm(v1)/norm(v3))*v3)*sin(a);
if i == 4
vx4 = (v(1,:)+(1/(phi^3)));
vy4 = (v(2,:)+(1/(phi^3)));
elseif i == 8
vx8 = (v(1,:)+(1/(phi^7)));
vy8 = (v(2,:)+(1/(phi^7)));
end
end
end
patch(sx,sy,0.8+0.2*rand(1,3));
vx = [vx1 vx2 vx3 vx4 vx5 vx6 vx7 vx8];
vy = [vy1 vy2 vy3 vy4 vy5 vy6 vy7 vy8];
plot(vx,vy);
axis equal
In the code, the comments about North, West, etc. are in reference to the rectangles that are displayed and their orientation. The parts of the code that make up the spiral are likewise labeled.
So I really have two questions, however, the one that is most pressing is why will only the plot(vx,vy) command only work and not the patch? I tried looking at other examples of things but they seem to just type each command in in the order they want them to print and it does it.
The other issue I have it that when I do run this code the arcs work perfectly for i = 1:4 but after that, you can see, the lines are all messed up. I don't really know why that is either.
If I left out any other information I'm sorry!
Thank you

Matlab: how to calculate the Pseudo Zernike moments?

The code below is defined as algorithm 1 that computes the Pseudo Zernike Radial polynomials:
function R = pseudo_zernike_radial_polynomials(n,r)
if any( r>1 | r<0 | n<0)
error(':zernike_radial_polynomials either r is less than or greater thatn 1, r must be between 0 and 1 or n is less than 0.')
end
if n==0;
R =ones(n +1, length(r));
return;
end
R =ones(n +1, length(r));
rSQRT= sqrt(r);
r0 = ~logical(rSQRT.^(2*n+1)) ; % if any low r exist, and high n, then treat as 0
if any(r0)
m = n:-1:mod(n,2); ss=1:sum(r0);
R0(m +1, ss)=0;
R0(0 +1, ss)=1;
R(:,r0)=R0;
end
if any(~r0)
rSQRT= rSQRT(~r0);
R1 = zernike_radial_polynomials(2*n+1, rSQRT );
m = 2:2: 2*n+1 +1;
R1=R1(m,:);
for m=1:size(R1,1)
R1(m,:) = R1(m,:)./rSQRT';
end
R(:,~r0)=R1;
end
Then, this is algorithm 2 that calculates the moments:
and I translate into the code as follow:
clear all
%input : 2D image f, Nmax = order.
f = rgb2gray(imread('Oval_45.png'));
prompt = ('Input PZM order Nmax:');
Nmax = input(prompt);
Pzm =0;
l = size(f,1);
for x = 1:l;
for y =x;
for n = 0:Nmax;
[X,Y] = meshgrid(x,y);
R = sqrt((2.*X-l-1).^2+(2.*Y-l-1).^2)/l;
theta = atan2((l-1-2.*Y+2),(2.*X-l+1-2));
R = (R<=1).*R;
rad = pseudo_zernike_radial_polynomials(n, R);
for m = 0:n;
%find psi
if mod(m,2)==0
%m is even
newd1 = f(x,y)+f(x,y);
newd2 = f(y,x)+f(y,x);
newd3 = f(x,y)+f(x,y);
newd4 = f(y,x)+f(y,x);
x1 = newd1;
y1 = (-1)^m/2*newd2;
x2 = newd3;
y2 = (-1)^m/2*newd4;
psi = cos(m*theta)*(x1+y1+x2+y2)-(1i)*sin(m*theta)*(x1+y1-x2-y2);
else
newd1 = f(x,y)-f(x,y);
newd2 = f(y,x)-f(y,x);
newd3 = f(x,y)-f(x,y);
newd4 = f(y,x)-f(y,x);
x1 = newd1;
y1 = (-1)^m/2*newd2;
x2 = newd3;
y2 = (-1)^m/2*newd4;
psi = cos(m*theta)*(x1+x2)+sin(m*theta)*(y1-y2)+(1i)*(cos(m*theta)*(y1+y2)-sin(m*theta)*(x1-x2));
end
Pzm = Pzm+rad*psi;
end
end
end
end
However its give me error :
Error using *
Integers can only be combined with integers of the same class, or scalar doubles.
Error in main_pzm (line 44)
Pzm = Pzm+rad*psi;
The detail of the calculation can be seen here

Function contourf, how i can get the area and centroid of the different objects

I am using contourf function with binary image. I am trouble how i can get the area and centroid of the different surface in the image, need this task to classify the objects.
You need to use the the Contour Matrix output
Here is an example:
function data = ContourInfo(C)
data = [];
if isempty(C)
return
end
k = 1;
j = 1;
while j < size(C,2);
data(k).numxy = C(2,j);
data(k).x = C(1,j+1:j+data(k).numxy);
data(k).y = C(2,j+1:j+data(k).numxy);
data(k).level = C(1,j);
[data(k).centroid(1) data(k).centroid(2) data(k).area] = ...
polycentroid(data(k).x, data(k).y);
data(k).area = polyarea(data(k).x, data(k).y);
data(k).centroid = polycentroid(data(k).x, data(k).y);
j = j + data(k).numxy + 1;
k = k+1;
end
function [x0,y0,a] = polycentroid(x,y)
[m1,n1] = size(x); [m2,n2] = size(y);
n = max(m1,n1);
x = x(:); y = y(:);
x2 = [x(2:n);x(1)];
y2 = [y(2:n);y(1)];
a = 1/2*sum (x.*y2-x2.*y);
x0 = 1/6*sum((x.*y2-x2.*y).*(x+x2))/a;
y0 = 1/6*sum((x.*y2-x2.*y).*(y+y2))/a;
Call as follow:
Z = peaks(20);
[C, h] = contourf(Z,10);
contourData = ContourInfo(C)
disp('Area of contour 1:');
disp(contourData(1).area
disp('Centroid of contour 1:');
disp(contourData(1).centroid);

Matlab surf only points, not lines

I have to draw a hipsometric map on a 3D plot. I have two vectors 1x401 (named xLabels and yLabels) which are the geo coordinates, and401x401(namedA`) matrix with the altitude data. To plot the data I use:
surf(xLabels, yLabels,A,'EdgeColor','None','Marker','.');
which leads to something like that:
But i would like to have something like that:
On the second image, only the surface is plotted, while my image looks like pillars.
I tried even make my vectors to 401x401 using meshgrid but it did not have any effect.
Do you have any idea what I should change?
#EDIT
I checked for X and Y data. I quess is too small interval (0.0083), but when i try plot good second of upper plots with same interval it draws correctly.
#EDIT2:
sizeX = 4800;
sizeY = 6000;
pixdegree = 0.0083; % 1 pixel is 0.0083 degree on map
intSize = 2;
lon = 37 + (35/60);
lat = 55+ (45/60);
fDEM = 'E020N90';
fHDR = 'E020N90.HDR';
[startXY, endXY] = calcFirstPixel(lon, lat); %calc borders for my area
f = fopen('E020N90.DEM');
offset = (startXY(1,2)*sizeX*intSize)+(startXY(1,1)*intSize);
fseek(f, offset,0); %seek from curr file pos
x = 0;
A = [];
BB = [];
jump = (intSize*sizeX)-(401*2);
while x<401
row = fread(f, 802);
fseek(f, jump, 0); %jump 2 next row
A = [A row];
x = x+1;
end
fclose(f);
A = A';
A = A(:,2:2:802);
m1 = min(A(:)); %wartość minimalna dla naszej podziałki
m2 = max(A(:)); %wartość maksymalna dla naszej podziałki
step = m2/8; % będzie 8 kolorów
highScale = m1:step:m2-step; %wartości graniczne dla każdego z nich
%handles.axes1 = A;
colormap(hObject, jet(8));
startXtick = 20 + pixdegree*startXY(1,1);
endXtick = 20 + pixdegree*endXY(1,1);
startYtick = 90 - pixdegree*endXY(1,2);
endYtick = 90 - pixdegree*startXY(1,2);
[XX,YY] = ndgrid(startXtick:pixdegree:endXtick,startYtick:pixdegree:endYtick);
xLabels = startXtick:pixdegree:endXtick;
yLabels = startYtick:pixdegree:endYtick;
surf(xLabels, yLabels,A,'EdgeColor','None','Marker','.');
set(gca,'YDir','normal');
grid on;
view([45 45])
And .DEM files
function [startXY, endXY] = calcFirstPixel(lon,lat)
global fHDR;
format = '%s %s';
f = fopen(fHDR);
cont = textscan(f, format);
LonStart = str2double(cont{1,2}{11,1});
LatStart = str2double(cont{1,2}{12,1});
diffPerPix = str2double(cont{1,2}{13,1});
fclose(f);
x = LonStart;
countX = 0
y = LatStart;
countY= 0;
while x<lon
x=x+diffPerPix
countX = countX +1;
end
while y>lat
y=y-diffPerPix
countY = countY+1;
end
startXY= [countX-200 countY-200];
endXY = [countX+200 countY+200];
end