Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
3.2 kB
3
Indexable
import cv2
import numpy as np
from qutip import Qobj, basis, tensor, qeye, sigmax, sigmay, sigmaz, sesolve


def adjacency_matrix_4_neighborhood(width, height):
    dim = width * height
    adj_matrix = np.zeros((dim, dim))

    for y in range(height):
        for x in range(width):
            i = y * width + x
            if x > 0:
                adj_matrix[i, i - 1] = 1
            if x < width - 1:
                adj_matrix[i, i + 1] = 1
            if y > 0:
                adj_matrix[i, i - width] = 1
            if y < height - 1:
                adj_matrix[i, i + width] = 1

    return adj_matrix


def dtqw_operator(dim, coin_operator):
    U = tensor(coin_operator, qeye(dim)) * (tensor(basis(2, 0), qeye(dim)) * tensor(basis(2, 0).dag(), basis(dim, 0)) +
                                            tensor(basis(2, 1), qeye(dim)) * tensor(basis(2, 1).dag(), basis(dim, 1)))
    return U


def dtqw_step(state, U):
    return U * state


def dtqw_segmentation(image, iterations, adj_matrix):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    dim = gray_image.shape[0] * gray_image.shape[1]
    coin_operator = 1 / np.sqrt(2) * (sigmax() + sigmay() + sigmaz())
    U = dtqw_operator(dim, coin_operator)
    initial_state = tensor(basis(2, 0), basis(dim, 0))

    for _ in range(iterations):
        initial_state = dtqw_step(initial_state, U)

    probabilities = np.abs(initial_state.full()) ** 2
    segmented_image = np.reshape(probabilities, gray_image.shape)
    return segmented_image


def ctqw_operator(dim, adj_matrix):
    H = Qobj(adj_matrix)
    U = (-1j * H).expm()
    return U


def ctqw_step(state, U):
    return U * state


def ctqw_limiting_distribution(state, adj_matrix):
    ld_state = sesolve(H=Qobj(adj_matrix), rho0=state, tlist=[0], e_ops=[], options=None, progress_bar=None).states[-1]
    return ld_state


def ctqw_segmentation(image, iterations, adj_matrix):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    dim = gray_image.shape[0] * gray_image.shape[1]
    U = ctqw_operator(dim, adj_matrix)
    initial_state = basis(dim, 0)

    for _ in range(iterations):
        initial_state = ctqw_step(initial_state, U)

    ld_state = ctqw_limiting_distribution(initial_state, adj_matrix)
    probabilities = np.abs(ld_state.full()) ** 2
    segmented_image = np.reshape(probabilities, gray_image.shape)

    # Load the input image
    input_image = cv2.imread('input_image.jpg')

    # Generate the adjacency matrix for the image
    height, width, _ = input_image.shape
    adj_matrix = adjacency_matrix_4_neighborhood(width, height)

    # Apply segmentation (you can replace dtqw_segmentation with ctqw_segmentation)
    iterations = 100
    segmented_image = dtqw_segmentation(input_image, iterations, adj_matrix)

    # Normalize the segmented image to the range [0, 255]
    segmented_image_normalized = cv2.normalize(segmented_image, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX,
                                               dtype=cv2.CV_8U)

    # Save the segmented image
    cv2.imwrite('segmented_image.jpg', segmented_image_normalized)

    return segmented_image