0

I'm new to image processing and I'm really having a hard time understanding stuff...so the idea is that how do you create a matrix from a binary image in python?

to something like this:

It not the same image though the point is there. Thank you for helping, I appreciate it cheers

2 Answers 2

2

Using cv2 -Read more here

import cv2
img = cv2.imread('path/to/img.jpg')
resized = cv2.resize(img, (128, 128), cv2.INTER_LINEAR)
print pic

Using skimage - Read more here

import skimage.io as skio
faces = skio.imread_collection('path/to/images/*.pgm',conserve_memory=True) # can load multiple images at once

Using Scipy - Read more here

from scipy import misc
pic = misc.imread('path/to/img.jpg')
print pic

Plotting Images

import matplotlib.pyplot as plt
plt.imshow(faces[0],cmap=plt.cm.gray_r,interpolation="nearest")
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

example I am currently working with.

"""
A set of utilities that are helpful for working with images. These are utilities
needed to actually apply the seam carving algorithm to images
"""


from PIL import Image


class Color:
    """
    A simple class representing an RGB value.
    """

    def __init__(self, r, g, b):
        self.r = r
        self.g = g
        self.b = b

    def __repr__(self):
        return f'Color({self.r}, {self.g}, {self.b})'

    def __str__(self):
        return repr(self)


def read_image_into_array(filename):
    """
    Read the given image into a 2D array of pixels. The result is an array,
    where each element represents a row. Each row is an array, where each
    element is a color.

    See: Color
    """

    img = Image.open(filename, 'r')
    w, h = img.size

    pixels = list(Color(*pixel) for pixel in img.getdata())
    return [pixels[n:(n + w)] for n in range(0, w * h, w)]


def write_array_into_image(pixels, filename):
    """
    Write the given 2D array of pixels into an image with the given filename.
    The input pixels are represented as an array, where each element is a row.
    Each row is an array, where each element is a color.

    See: Color
    """

    h = len(pixels)
    w = len(pixels[0])

    img = Image.new('RGB', (w, h))

    output_pixels = img.load()
    for y, row in enumerate(pixels):
        for x, color in enumerate(row):
            output_pixels[x, y] = (color.r, color.g, color.b)

    img.save(filename)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.