

In this example, we show a basic image segmentation algorithm to partition animage into segments based on their pixel values. To solve this problem, we usethe hybrid discrete quadratic model solver available in Leap, and demonstratehow to build a DQM object from a set of numpy vectors.
To run the demo, type the command:
python image_segmentation.py
This will build a random image based on the specifications stated by the user.The first prompt will ask for the dimensions in pixels (a square image will becreated), and the second prompt will ask how many segments we want in our image.
Alternatively, the user can specify an input image such astest_2_segments.png by typing:
python image_segmentation.py test_2_segments.png
The program prompts the user for the number of segments to partition the imageinto.
After the program executes, a file is saved asoutput.png that shows theoriginal image on the left and the partition outlines in an image on the right.
A few example images have been provided.
test_2_segments.png is a small image with 2 segments.test_4_segments.png is a small image with 4 segments.test_image.jpeg is a larger image with 2 segments (foreground andbackground) that will take longer to run.
Note: For this demo to run relatively quickly, image sizes should be keptbelow 50x50 pixels with fewer than 10 segments. Several small image files areincluded in the repository.
A simple method to partition an image into segments is to compare their pixelvalues. If colors are similar, then they might belong to the same object in theimage. This program builds a DQM object in which we have a variable for eachpixel and a case for each segment. As we compare pixels, we examine theirdifference using the providedweight function, which assigns smallervalues for more alike colors, and larger values for more different colors.Using this weight function, we assign quadratic biases between pixels in thesame cases. As the solver minimizes the energy landscape, it is then minimizingthe difference between pixels placed in the same segment or partition.