Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Co-occurancy Matrix #10

Open
wiwichips opened this issue Apr 15, 2023 · 0 comments
Open

Co-occurancy Matrix #10

wiwichips opened this issue Apr 15, 2023 · 0 comments

Comments

@wiwichips
Copy link
Owner

Generate a co-adjacency matrix for an image and then turn it into an image that can be viewed

Here is an example python program I wrote to do this

#!/usr/bin/env python3
from PIL import Image
import numpy as np

# Load image
img = Image.open("mb.png")

# Convert image to numpy array
img_arr = np.array(img)

# Initialize co-occurrence matrices for each color channel
red_co = np.zeros((256, 256))
green_co = np.zeros((256, 256))
blue_co = np.zeros((256, 256))

# Loop through each pixel in the image
for i in range(img_arr.shape[0]-1):
    for j in range(img_arr.shape[1]-1):
        # Update co-occurrence matrix for red channel
        red_co[img_arr[i,j,0], img_arr[i+1,j,0]] += 1
        # Update co-occurrence matrix for green channel
        green_co[img_arr[i,j,1], img_arr[i+1,j,1]] += 1
        # Update co-occurrence matrix for blue channel
        blue_co[img_arr[i,j,2], img_arr[i+1,j,2]] += 1

# Print co-occurrence matrices
print("Red channel co-occurrence matrix:")
print(red_co)
print("Green channel co-occurrence matrix:")
print(green_co)
print("Blue channel co-occurrence matrix:")
print(blue_co)

# Convert co-occurrence matrices to 8-bit unsigned integer format
red_co = red_co.astype(np.uint8)
green_co = green_co.astype(np.uint8)
blue_co = blue_co.astype(np.uint8)

# Combine co-occurrence matrices into a single RGB image
co_image = np.dstack((red_co, green_co, blue_co))

# Create PIL image from numpy array
co_pil = Image.fromarray(co_image)

# Resize image to 256x256
co_pil = co_pil.resize((256, 256))

# Display image
co_pil.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant