在灰度图像上散布网格

问题描述 投票:0回答:1

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

我想分散点以获得类似的东西

在笛卡尔坐标系中。 是否有任何库可以完成这样的任务,或者我需要从头开始实现一些东西?预先感谢,我将不胜感激任何帮助。

python matplotlib image-processing mesh
1个回答
0
投票

猜测一下你的意思,这是一种方法:

  • 将图像加载为灰度
  • 纯黑白的门槛
  • 获取所有黑色像素的坐标
  • 对坐标的 0.5% 进行随机子采样
  • 创建空输出图像
  • 在二次采样位置的输出图像上绘制圆圈

#!/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)

© www.soinside.com 2019 - 2024. All rights reserved.