Beregninger med bilder

Initialiseringer

cm=72/2.54;
SetDirectory["/Users/knutm/work/Calculus book/2011/images"]

"/Users/knutm/Work/Calculus book/2011/images"

f[x_,n_]:=(ArcTan[n(x-0.5)]/ArcTan[0.5n]+1)/2

g[x_,eps_]:=(Log[x+eps]-Log[eps])/(Log[1+eps]-Log[eps])

normaliser[l_List]:=
Module[{min=Min[l],max=Max[l]},
(l-min)/(max-min)]

Test

a={{1/3,0,1},{1/4,1/2,0},{7/8,0,2/3}}

MatrixForm[a]

Graphics[Raster[a]]

a=Table[{RandomReal[],RandomReal[],RandomReal[]},{i,100},{j,100}];

a[[1,1]]

Graphics[Raster[a]]

Innlesing

b=Import["bus-small.png"];

Print[b]

Hent bildedata

im=b[[1]];

im=ImageData[ImageReflect[b],"Byte"];

a=Map[Take[#,{1,3}]&,im,{2}];

a[[2,1]]

aa=a/255.0;

Graphics[Raster[aa]]

Plukk noen sampler

bb=Take[aa,{1,-1,2},{1,-1,2}];
Graphics[Raster[bb]]

Utdrag fra bildet

utdrag[i_,j_,m_,n_]:=
Module[{ii,jj,grid,pts,gr,utdr},
utdr=Table[aa[[ii,jj]],{ii,i,i+n},{jj,j,j+m}];grid={Table[Line[{{ii+0.5,0},{ii+0.5,n+1}}],{ii,0,m}],Table[Line[{{0,ii+0.5},{m+1,ii+0.5}}],{ii,0,n}]};
pts=Map[Point,Table[{ii+0.5,jj+0.5},{ii,0,m},{jj,0,n}]];
gr=Graphics[{Raster[utdr],grid,{PointSize[0.01],pts}},ImageSize->15cm]
]

g=utdrag[103,103,7,7]

Export["grid.png",g]

g1=utdrag[103,103,40,40]

Export["grid1.png",g1]

Konvertering til svart-hvitt

Graphics[Raster[bb]]

bb[[1,1]]

gg3=Map[If [Max[#]≤0.5,0,1]&,bb,{2}];
Graphics[Raster[gg3]]

Konvertering til gråtoner

Max av rød, grønn og blå

gg=Apply[Max,aa,{2}];

gmax=Graphics[Raster[gg]]

Summen av rød, grønn og blå

gL1=Map[(#.{1,1,1})&,aa,{2}];
gL1=gL1/Max[gL1];
gg1=Graphics[Raster[gL1]]

Lengden av rød, grønn og blå

gL2=Map[Sqrt[#.#]&,aa,{2}];
gL2=gL2/Max[gL2];
ggl2=Graphics[Raster[gL2]]

{gmax,gg1,ggl2}

ginv=Map[(1-#)&,gL2,{2}];
inv=Graphics[Raster[ginv]]

Øke kontrast

Plot[{f[x,4],f[x,10],f[x,100]},{x,0,1},PlotRange->All]

Plot[{g[x,0.1],f[x,0.01],g[x,0.001]},{x,0,1}]

ed2=Map[f[#,10]&,gL2,{2}];
Graphics[Raster[ed2]]

ed3=Map[Sqrt,gL2,{2}];
Graphics[Raster[ed3]]

Bilde som flate

Gråtoner som eksplisitt flate

pic=ListPlot3D[gL2,ViewPoint->{2,-10,30},ColorFunction->GrayLevel,Axes->False,Mesh->False]

bb=Take[gL2,{1,-1,2},{1,-1,2}];

Graphics[Raster[bb]]

ggg=ListPlot3D[bb,ColorFunction->GrayLevel]

RGB-bilde som parametrisk flate

grgb=ListPlot3D[bb,Axes->False]

De tre farvekomponentene

Rød

rr=Map[#[[1]]&,aa,{2}];
gr=Graphics[Raster[rr]]

Grønn

gg=Map[#[[2]]&,aa,{2}];
ggg=Graphics[Raster[gg]]

Blå

bb=Map[#[[3]]&,aa,{2}];
gb=Graphics[Raster[bb]]

{gr,ggg,gb}

Glatting

filter[m_,n_]:=Table[Binomial[m,i]Binomial[n,j],{i,0,m},{j,0,n}]/2^(m+n);
filter[6,6]//MatrixForm

ed=ListConvolve[filter[2,2],gL2];
ed=ed/Max[ed];
sm2=Graphics[Raster[ed]]

{Graphics[Raster[gL2]],sm2}

ed=ListConvolve[filter[6,6],gL2];
ed=ed/Max[ed];
sm6=Graphics[Raster[ed]]

ed=ListConvolve[filter[60,60],gL2];
ed=ed/Max[ed];
sm60=Graphics[Raster[ed]]

Differenser

{{0,0,0},{-1,0,1},{0,0,0}}//MatrixForm

X-derivert

dx=ListConvolve[{{0,0,0},{-1,0,1},{0,0,0}},gL2];
Graphics[Raster[dx]]

Min[dx]

Max[dx]

Normaliser til [0,1]

gx=normaliser[dx];
Graphics[Raster[gx]]

Bedre kontrast

gxc=Map[f[#,50]&,gx,{2}];
ggx=Graphics[Raster[gxc]]

{{0,1,0},{0,0,0},{0,-1,0}}//MatrixForm

Y-derivert

dy=ListConvolve[{{0,1,0},{0,0,0},{0,-1,0}},gL2];
Graphics[Raster[dy]]

Normaliser til [0,1]

gy=normaliser[dy];
Graphics[Raster[gy]]

Bedre kontrast

gyc=Map[f[#,50]&,gy,{2}];
ggy=Graphics[Raster[gyc]]

Lengden til gradienten

grad=Sqrt[dx^2+dy^2];
Graphics[Raster[grad]]

Normaliser til [0,1]

grad=normaliser[grad];
Graphics[Raster[grad]]

Bedre kontrast

gradc=Map[f[#,50]&,grad];
ggrad=Graphics[Raster[gradc]]

gradc=Map[Log[#+0.1]&,grad,{2}];
gradc=normaliser[gradc];
ggrad=Graphics[Raster[gradc]]

{ggx,ggy,ggrad}

Andrederivert

XX-derivert

MatrixForm[{{0,-1,0},{0,2,0},{0,-1,0}}]

dxx=ListConvolve[{{0,-1,0},{0,2,0},{0,-1,0}},gL2];
Graphics[Raster[dxx]]

Normaliser til [0,1]

gxx=normaliser[dxx];
Graphics[Raster[gxx]]

Bedre kontrast

gxxc=Map[f[#,100]&,gxx,{2}];
ggxx=Graphics[Raster[gxxc]]

YY-derivert

dyy=ListConvolve[{{0,-1,0},{0,2,0},{0,-1,0}},gL2];
Graphics[Raster[dyy]]

Normaliser til [0,1]

gyy=normaliser[dyy];
Graphics[Raster[gyy]]

Bedre kontrast

gyyc=Map[f[#,100]&,gyy,{2}];
ggyy=Graphics[Raster[gyyc]]

XY-derivert

dxy=ListConvolve[{{1,0,-1},{0,0,0},{-1,0,1}},gL2];
Graphics[Raster[dxy]]

Normaliser til [0,1]

gxy=normaliser[dxy];
Graphics[Raster[gxy]]

Bedre kontrast

gxyc=Map[f[#,100]&,gxy,{2}];
ggxy=Graphics[Raster[gxyc]]

{ggxx,ggxy,ggyy}

Figurer

cg=Plot[{f[x,4],f[x,10],f[x,100]},{x,0,1},ImageSize->6cm]

Export["contrast1.pdf",cg]

cg=Plot[{g[x,0.1],g[x,0.01],g[x,0.001]},{x,0,1},PlotRange->All,ImageSize->6cm]

Export["contrast2.pdf",cg]

FullSimplify[g[x,e]]

Export["rcontrast1.png",cg]

grgb=ListPlot3D[bb,Axes->False]

Export["imparsurf.png",grgb]

gg=Apply[Max,aa,{2}];

gmax=Graphics[Raster[gg]]

Export["gmax.png",gmax]

ginv=Map[(1-#)&,gg,{2}];

inv=Graphics[Raster[ginv]]

Export["ginvmax.png",inv]

gg1=Map[(#.{1,1,1})&,aa,{2}];

gg1=gg1/Max[gg1];

gg1=Graphics[Raster[gg1]]

Export["gg1.png",gg1]

ginvl1=Map[(1-#)&,gg1,{2}];

inv=Graphics[Raster[ginvl1]]

Export["ginvl1.png",inv]

gg2=Map[Sqrt[#.#]&,aa,{2}];

gg2=gg2/Max[gg2];

ggl2=Graphics[Raster[gg2]]

Export["ggl2.png",ggl2]

ginv=Map[(1-#)&,gg2,{2}];

inv=Graphics[Raster[ginv]]

Export["ginvl2.png",inv]

pic=ListPlot3D[gg2,ViewPoint->{2,-10,30},ColorFunction->GrayLevel,Axes->False,Mesh->False]

Export["surf.png",pic]

utdrag1=Table[gg2[[i,j]],{i,215,220},{j,210,215}];
grid={Table[Line[{{i+0.5,0},{i+0.5,6}}],{i,0,5}],Table[Line[{{0,i+0.5},{6,i+0.5}}],{i,0,5}]};
pts=Map[Point,Table[{i+0.5,j+0.5},{i,0,5},{j,0,5}]];
gr=Graphics[{Raster[utdrag1],grid,{PointSize[0.015],pts}},ImageSize->15cm]

Export["grid.pdf",gr]

gg3=Map[If [#≤0.5,0,1]&,gg2,{2}];

Export["bus-small-grey.png",gg2]

Graphics[Raster[gg3]]

rr=Map[#[[1]]&,aa,{2}];

gr=Graphics[Raster[rr]]

Export["gr.png",gr]

gg=Map[#[[2]]&,aa,{2}];

ggg=Graphics[Raster[gg]]

Export["ggg.png",ggg]

bb=Map[#[[3]]&,aa,{2}];

gb=Graphics[Raster[bb]]

Export["gb.png",gb]

gg=Map[g[#,0.01]&,gg2,{2}];
gp=Graphics[Raster[gg]]

Export["contrast4.png",gp]

ed=ListConvolve[{{1,4,6,4,1},{4,16,24,16,4},{6,24,36,24,6},{4,16,24,16,4},{1,4,6,4,1}}/256,gg2];

ed=ed/Max[ed];

Graphics[Raster[ed]]

Differenser

X-derivert

dx=ListConvolve[0.5{{0,0,0},{-1,0,1},{0,0,0}},gL2];
px1=Graphics[Raster[dx]]

Export["px1.png",px1]

Min[dx]

Max[dx]

Normaliser til [0,1]

gx=normaliser[dx];
px2=Graphics[Raster[gx]]

Export["px2.png",px2]

Bedre kontrast

gxc=Map[f[#,50]&,gx,{2}];
px3=ggx=Graphics[Raster[gxc]]

Export["px3.png",px3]

Y-derivert

dy=ListConvolve[{{0,1,0},{0,0,0},{0,-1,0}},gL2];
Graphics[Raster[dy]]

Normaliser til [0,1]

gy=normaliser[dy];
Graphics[Raster[gy]]

Bedre kontrast

gyc=Map[f[#,50]&,gy,{2}];
py=Graphics[Raster[gyc]]

Export["py.png",py]

Lengden til gradienten

grad=Sqrt[dx^2+dy^2];
grad1=Graphics[Raster[grad]]

Max[grad]

Export["grad1.png",grad1]

Normaliser til [0,1]

grad=normaliser[grad];
grad2=Graphics[Raster[grad]]

Export["grad2.png",grad2]

Bedre kontrast

gradc=Map[f[#,50]&,grad,{2}];
ggrad=Graphics[Raster[gradc]]

gradc=Map[g[#,0.01]&,grad,{2}];
ggrad=Graphics[Raster[gradc]]

Export["grad.png",ggrad]

{ggx,ggy,ggrad}

Andrederivert

XX-derivert

dxx=ListConvolve[{{0,-1,0},{0,2,0},{0,-1,0}},gL2];
Graphics[Raster[dxx]]

Normaliser til [0,1]

gxx=normaliser[dxx];
Graphics[Raster[gxx]]

Bedre kontrast

gxxc=Map[f[#,100]&,gxx,{2}];
ggxx=Graphics[Raster[gxxc]]

Export["dxx.png",ggxx]

YY-derivert

dyy=ListConvolve[{{0,-1,0},{0,2,0},{0,-1,0}},gL2];
Graphics[Raster[dyy]]

Normaliser til [0,1]

gyy=normaliser[dyy];
Graphics[Raster[gyy]]

Bedre kontrast

gyyc=Map[f[#,100]&,gyy,{2}];
ggyy=Graphics[Raster[gyyc]]

Export["dyy.png",ggyy]

XY-derivert

dxy=ListConvolve[{{-1,0,1},{0,0,0},{1,0,-1}},gL2];
Graphics[Raster[dxy]]

Normaliser til [0,1]

gxy=normaliser[dxy];
Graphics[Raster[gxy]]

Bedre kontrast

gxyc=Map[f[#,100]&,gxy,{2}];
ggxy=Graphics[Raster[gxyc]]

Export["dxy.png",ggxy]

{ggxx,ggxy,ggyy}