Skip to main content

Seam Carving

Definition​

Seam Carving Algorithm is a content-aware image resizing technique that intelligently removes or adds pixels from an image's least noticeable areas, rather than simply scaling it. This allows for resizing without distorting important features in the image

Practice​

seam_carving(image, new_width, new_height):
while image.width > new_width:
energy_map = calculate_energy_map(image)
cumulative_energy_map = calculate_cumulative_energy_map(energy_map)
seam = find_vertical_seam(cumulative_energy_map)
image = remove_vertical_seam(image, seam)

while image.height > new_height:
energy_map = calculate_energy_map(image)
cumulative_energy_map = calculate_cumulative_energy_map(energy_map)
seam = find_horizontal_seam(cumulative_energy_map)
image = remove_horizontal_seam(image, seam)

return image

remove_vertical_seam(image, seam):
new_image = create_new_image(image.width - 1, image.height)
for row from 0 to image.height:
for col from 0 to image.width:
if col < seam[row]:
new_image[row][col] = image[row][col]
else if col > seam[row]:
new_image[row][col - 1] = image[row][col]
return new_image

remove_horizontal_seam(image, seam):
new_image = create_new_image(image.width, image.height - 1)
for col from 0 to image.width:
for row from 0 to image.height:
if row < seam[col]:
new_image[row][col] = image[row][col]
else if row > seam[col]:
new_image[row - 1][col] = image[row][col]
return new_image