matplotlib 在灰度图像上散布网格网格

vltsax25  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(139)

我试图根据灰度图像创建一个numpy网格。例如,考虑到这张图片

我想分散点来得到类似于

在Caribbean坐标.有没有做这样的任务,或者我需要从头开始实现的东西库?提前感谢,我将感谢任何帮助.

qnzebej0

qnzebej01#

猜猜你的意思,这里有一个方法:

  • 加载图像为灰度
  • 纯黑和白色阈值
  • 获取所有黑色像素的坐标
  • 随机抽取0.5%的坐标
  • 创建空输出图像
  • 在二次采样位置的输出图像的alpha通道上绘制白色圆圈,这将使黑色背景在这些位置变得可见
#!/usr/bin/env python3

import numpy as np
import cv2 as cv

# Load image as greyscale
im = cv.imread('Gkzaa.png', cv.IMREAD_GRAYSCALE)

# Otsu threshold to pure black and pure white, i.e. 0 or 255
_, thresh = cv.threshold(im, 0, 255, cv.THRESH_BINARY+cv.THRESH_OTSU)

# Get X and Y coordinates of all black pixels
Y, X = np.where(thresh==0)
nBlack = len(Y)

# Get indices of 0.5% of those black pixels
indices = np.random.choice(nBlack, size=int(nBlack*0.005), replace=False)

# Form an empty (black) output image and an alpha channel for it
BGR = np.zeros((*im.shape,3), np.uint8)
A   = np.zeros_like(im)

# Draw circles of opacity into the alpha channel at selected indices
for i in indices:
    cv.circle(A, center=(X[i], Y[i]), radius=4, color=255, thickness=cv.FILLED)

# Stack the alpha channel onto the BGR channel to make BGRA and save
res = np.dstack((BGR,A))
cv.imwrite('result.png', res)

相关问题