Seam carving is an effective image processing technique with the help of which an image can be resized without removing important elements from the image. The basic approach is to find all the continuous pixels with low energy from left to right or from top to bottom. After the region is selected, it is removed from the original image, leaving only the relevant part of the image. An energy map is derived from the original image which represents apposite details of the image. With the help of the energy map, we can identify the seams that are spread from right to left or top to bottom.
How is Seam carving different from the traditional resizing approach?
Seam carving is different from resizing in the sense that in Seam carving all the valuable aspects and elements are still present in the image but resizing the image is simply copied to a newer size which may be responsible for losing important details.
Below is the Implementation of Seam carving technique:
Python3
# import the necessary libraries from skimage import transform from skimage import filters import cv2 # read the input image img = cv2.imread('Seam.jpg') # convert image from BGR # to GRAY scale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # filter out the gradient representation # image to further process for # seam carving algorithm # filters.sobel() is used to # find edges in an image filtered = filters.sobel(gray.astype("float")) for i in range(20, 180, 20): # apply seam carve to the image, # iterating over the image for # multiple frames # transform.seam_carve() can transform # the seam of image vertically as # well as horizontally carved_image = transform.seam_carve(img, filtered, 'vertical', i) # show the original image cv2.imshow("original", img) # show the carved image cv2.imshow("carved", carved_image) # print shape of both images print("Shape of original image ", img.shape) print("Shape of Carved image ", carved_image.shape) # wait cv2.waitKey(0) |
Output
Shape of original Image (667, 1000, 3) Shape of Carved image (667, 840, 3)
Original Image
Carved Image
Note: This code uses scikit-image version 0.15.0.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Image Resizing using OpenCV | Python
- Mahotas - Resizing Image
- Dynamically Resize Buttons When Resizing a Window using Tkinter
- PyQt5 – How to stop resizing of window | setFixedSize() method
- PyQt5 QSpinBox - Resizing it according to value
- MoviePy – Resizing Video File
- PYGLET – Setting Size / Resizing of Window
- OpenCV - Facial Landmarks and Face Detection using dlib and OpenCV
- Transition from OpenCV 2 to OpenCV 3.x
- OpenCV Python Program to analyze an image using Histogram
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Reading an image in OpenCV using Python
- Python | Detect corner of an image using OpenCV
- Image Pyramid using OpenCV | Python
- Negative transformation of an image using Python and OpenCV
- Python | Image blurring using OpenCV
- Find Circles and Ellipses in an Image using OpenCV | Python
- Image Translation using OpenCV | Python
- Image Registration using OpenCV | Python
- Python | Detect Polygons in an Image using OpenCV
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

