MATLAB: This image correspond to a specimen deformed in the vertical direction. Would you have any suggestion to lift deformed pixels and make a “perfecly horizontal block”

image processingImage Processing Toolbox

NOTE: A mask can be easily created. Thank you.

Best Answer

Just go along your mask finding the top line, then use circshift or imtranslate or simple indexing to lift the line.
[rows, columns] = size(mask);
for col = 1 : columns
thisColumn = grayImage(:, col);
topLine = find(mask(:, col), 1, 'first');
thisColumn = [thisColumn(topLine:end), zeros(topLine-1, 1)];
grayImage(:, col) = thisColumn;
end