mountain =
;hut =
;
indexFunc[s_, w_] := {Quotient[s, w, 1] 1, Mod[s, w, 1]};
edgeWeight[_[s_, t_], data_, w_] :=
Block[{sindex, tindex}, sindex = indexFunc[s, w];
tindex = indexFunc[t, w];
Norm[Extract[data, sindex]] Norm[Extract[data, tindex]]];
weightImageGraph[hut_, mountain_] :=
Block[{g, dimx, dimy, data, elist, vcount, eweights}, {dimx, dimy} =
ImageDimensions[mountain];
g = GridGraph[{dimx, dimy}];
elist = EdgeList[g];
vcount = VertexCount[g];
data = ImageData[hut][[;; dimy]] - ImageData[mountain];
eweights = edgeWeight[#, data, dimx] & /@ elist;
Graph[Join[Table[UndirectedEdge["s", i], {i, 1, dimx}], elist,
Table[UndirectedEdge[vcount - i, "t"], {i, 0, dimx - 1}]],
EdgeCapacity -> (Join[#, eweights, #] &[
Table[Total[eweights], {dimx}]])]];
graph = weightImageGraph[hut, mountain];
edgecut = FindEdgeCut[graph, "s", "t"];
synthesis[hut_, mountain_, edgecut_, graph_] :=
Block[{dimx, dimy, comp, newpart, mountaindata,
hutdata}, {dimx, dimy} = ImageDimensions[mountain];
mountaindata = ImageData[hut];
hutdata = ImageData[mountain];
comp = Select[ConnectedComponents[EdgeDelete[graph, edgecut]],
MemberQ[#, "s"] &] // First;
newpart = indexFunc[#, dimx] & /@ DeleteCases[comp, "s"];
ReplacePart[
mountaindata, ({#1, #2} -> hutdata[[#1, #2]]) & @@@ newpart]]
newdata = synthesis[hut, mountain, edgecut, graph];
Image[newdata, ColorSpace -> "RGB"]
--------By 官网(原谅小编不放链接,因为俺觉得不去官网混一圈的人,是很难理解Mathematica 与 Wolfram 本人是多么的伟大)