Giter VIP home page Giter VIP logo

image-segmentation's Introduction

Open in GitHub Codespaces Linux/Mac/Windows build status

Image Segmentation

In this example, we show a basic image segmentation algorithm to partition an image into segments based on their pixel values. To solve this problem, we use the hybrid discrete quadratic model solver available in Leap, and demonstrate how to build a DQM object from a set of numpy vectors.

Usage

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 be created), and the second prompt will ask how many segments we want in our image.

Alternatively, the user can specify an input image such as test_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 image into.

After the program executes, a file is saved as output.png that shows the original 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 and background) that will take longer to run.

Note: For this demo to run relatively quickly, image sizes should be kept below 50x50 pixels with fewer than 10 segments. Several small image files are included in the repository.

Building the DQM

A simple method to partition an image into segments is to compare their pixel values. If colors are similar, then they might belong to the same object in the image. This program builds a DQM object in which we have a variable for each pixel and a case for each segment. As we compare pixels, we examine their difference using the provided weight function, which assigns smaller values for more alike colors, and larger values for more different colors. Using this weight function, we assign quadratic biases between pixels in the same cases. As the solver minimizes the energy landscape, it is then minimizing the difference between pixels placed in the same segment or partition.

image-segmentation's People

Contributors

hemantdwave avatar joelpasvolsky avatar randomir avatar vgoliber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

image-segmentation's Issues

Add some expected runtimes

It would be helpful to set expectations on the runtimes of this example, those with preinstalled images are much longer than I would have guessed (compared to small random generated and our other DQM examples),
Also, running one preinstalled image, within recommended parameter limits, fails (I assume this was tested in the past so might need also an IDE issue).

Here's what I got on a few executions in Leap IDE:

Random generated:

  • python image_segmentation.py for dimensions set to 2 and segments set to 2 ran quickly (under a minute)
  • python image_segmentation.py for dimensions set to 3 and segments set to 2 ran quickly (under a minute)
  • python image_segmentation.py for dimensions set to 6 and segments set to 2 and to 4 ran quickly (under a minute)

Preinstalled images:

  • python image_segmentation.py test_2_segments.png for 2 segments took ~15 minutes
  • python image_segmentation.py test_4_segments.png for 4 segments took ~15 minutes
  • python image_segmentation.py test_image.jpeg for 5 segments aborted after about 20 minutes:
Leap IDE /workspace/image-segmentation $ python image_segmentation.py test_image.jpeg

Reading in your image...

        Enter number of segments > 5

Preparing DQM object...
Killed
  • python image_segmentation.py test_image.jpeg for 2 segments aborted after about 20 minutes:
Leap IDE /workspace/image-segmentation $ python image_segmentation.py test_image.jpeg

Reading in your image...

        Enter number of segments > 2

Preparing DQM object...
Killed

Difference calculations on unsigned ints wrap around

The data type of the image data is uint8, which means that the difference calculations in the weight function will wrap around when negative values are encountered. This produces incorrect (and asymmetric) values for the differences between pixels.

For example, using the test_2_segments.png image data:

In [12]: img[40,40,:]
Out[12]: array([255, 255, 255], dtype=uint8)

In [13]: img[26,26,:]
Out[13]: array([0, 0, 0], dtype=uint8)

In [14]: img[40,40,:] - img[26,26,:]
Out[14]: array([255, 255, 255], dtype=uint8)

In [15]: img[26,26,:] - img[40,40,:]
Out[15]: array([1, 1, 1], dtype=uint8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.