Generate a co-adjacency matrix for an image and then turn it into an image that can be viewed
#!/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()