# Code to rebuild a patchwork image

I'm trying to rebuild an image which has been cut into strips vertically and horizontally then the ordering of the strips has been rearranged.

For example:

So far I've only been able to come up with code which requires manual entry of the block size as well as the column/row reordering. What tools should I be using to automate the process of rebuilding the image without explicitly giving the code the block size and reordering needed.

For reference I am on Mathematica 10.2 but will welcome answers which use newer features as I plan to upgrade soon.

• One possible method to determine the order is to pairwise compare the edges of each block and order them by similarity. Nov 18, 2021 at 14:07

There are other questions about reconstruction such as this one and this one. I wanted to specifically address automatically finding the block size. This is kind of hard in general as the shuffled image may not necessarily contain hard edges. Below is my crude attempt:

(* get the image *)
img = Import["https://i.stack.imgur.com/fJb9a.png"];

(** Mush all the rows of the image together and look at differences.
Hopefully should see the seams become big jumps in the list **)
diffs = Abs@Differences[Mean[ImageData[ColorConvert[img, "Grayscale"]]], 1];

(* get the peaks that survive a high level of filtering *)
peaks = FindPeaks[diffs, 8];
ListLinePlot[diffs, PlotRange -> All, Epilog -> {Red, Point[peaks]}]

(* get the median distance between peaks *)
estimatedBS = Median[Differences[peaks[[All, 1]]]]; (* 60 *)

(* extract pieces with estimated block size so we can feed to reconstruction later *)
pieces = Flatten@ImagePartition[img, estimatedBS]

• Thanks for those great links. I didn't find any when I search but didn't think to search for paper shredding. Based on their existence my question is probably a duplicate as you can do the Q1 from the paper shredding in the horizontal then vertical direction to solve. Although in the fish problem no one addressed how to find edges such as you have here so its worth keeping. Nov 19, 2021 at 12:49