gpt4 book ai didi

python - 如何在 Matplotlib 中填充任意闭合区域?

转载 作者:太空狗 更新时间:2023-10-30 00:10:01 25 4
gpt4 key购买 nike

让我从我所在的地方开始:

enter image description here

我使用以下代码创建了上面的图像:

import matplotlib.pyplot as plt
import numpy as np

color_palette_name = 'gist_heat'
cmap = plt.cm.get_cmap(color_palette_name)
bgcolor = cmap(np.random.rand())

f = plt.figure(figsize=(12, 12), facecolor=bgcolor,)
ax = f.add_subplot(111)
ax.axis('off')

t = np.linspace(0, 2 * np.pi, 1000)

x = np.cos(t) + np.cos(6. * t) / 2.0 + np.sin(14. * t) / 3.0
y = np.sin(t) + np.sin(6. * t) / 2.0 + np.cos(14. * t) / 3.0

ax.plot(x, y, color=cmap(np.random.rand()))
ax.fill(x, y, color=cmap(np.random.rand()))
plt.tight_layout()
plt.savefig("../demo/tricky.png", facecolor=bgcolor, edgecolor=cmap(np.random.rand()), dpi=350)

有没有办法填充当线与其他颜色交叉时创建的循环(或类似三角形的区域)?它不一定是 matplotlib,它可以是 scikit-image 或其他一些库。

我在想一些伪代码,例如:

for region in regions:
ax.fill(region, color=cmap(np.random.rand()))

但我不知道如何获取 regions,或者如何填充它。

最佳答案

这个问题起初对我来说似乎很简单,我的想法是使用 blob 分析来检测不同的 blob,按大小对它们进行分组,然后使用 floodfill 算法为它们着色。

但是,我遇到了一些我没有修改的 blob 分析默认值的问题,这花费了一些时间。此外,我还没有找到任何用于使用 OpenCV 填充或着色 blob 的 python 代码片段,并且与我只能找到很少文档和示例代码的旧版本相比,使用 SimpleBlobDetection 的语法发生了一些变化。所以也许所有这些代码对其他用户也有用。

希望我已经正确识别了您想要查找的分割市场。如果您不想包括大的深色外层叶子,可以注释掉一行。

为了可视化,您可以调整图像的大小(此时已注释掉,请记住将大小阈值相应地调整为 4*4=16 倍)

enter image description here

包含所有这些选项的代码有些冗长,但希望易于阅读。我在使用 OpenCV 解决这个问题时学到了很多关于 blob 分析的知识,谢谢!

顺便说一句,图片不错。

import numpy as np
import cv2

im = cv2.imread('tricky.png')

# For better visibility, resize image to better fit screen
#im= cv2.resize(im, dsize=(0,0),fx=0.25, fy=0.25)

#convert to gray value for blob analysis
imgray= cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)



#### Blob analysis to find inner white leaves
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh = cv2.threshold(imgray,160, 255,type=cv2.THRESH_BINARY_INV)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Filter by Area.
params.filterByArea = True
params.minArea = 15000
params.maxArea = 150000

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(imthresh)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(imthresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)




####floodfill inner white leaves with blue
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html

#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255] )
print imgray.shape[:2]
print maskborder.shape[:2]

#Create result image for floodfill
result = im.copy()

#fill white inner segments with blue color
for k in keypoints:
print int(k.pt[0]),int(k.pt[1])
seed_pt = int(k.pt[0]),int(k.pt[1])
cv2.floodFill(result, maskborder, seed_pt, (255,0, 0))




#### Blob analysis to find small triangles
# SimpleBlobDetector will find black blobs on white surface, this is why type=cv2.THRESH_BINARY_INV is necessary
ret,imthresh2 = cv2.threshold(imgray,150, 255,type=cv2.THRESH_BINARY)
ret,imthresh3 = cv2.threshold(imgray,140, 255,type=cv2.THRESH_BINARY_INV)
imthresh4 = cv2.add(imthresh2,imthresh3)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Filter by Area.
params.filterByArea = True
params.minArea = 20
params.maxArea = 1000
params.maxArea = 50000 #Using this line includes the outer dark leaves. Comment out if necessary

# Don't filter by Circularity
params.filterByCircularity = False

# Don't filter by Convexity
params.filterByConvexity = False

# Don't filter by Inertia
params.filterByInertia = False

# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(imthresh4)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob
im_with_keypoints2 = cv2.drawKeypoints(imthresh4, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints2", im_with_keypoints2)



####floodfill triangles with green
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html

#Create a black mask for floodfill. Mask needs to be 2 pixel wider and taller
maskborder=imgray.copy()
maskborder[:] = 0
bordersize=1
maskborder=cv2.copyMakeBorder(maskborder, top=bordersize, bottom=bordersize, left=bordersize, right=bordersize, borderType= cv2.BORDER_CONSTANT, value=[255,255,255] )
print imgray.shape[:2]
print maskborder.shape[:2]

#Create result image for floodfill
result2 = result.copy()

#fill triangles with green color
for k in keypoints:
print int(k.pt[0]),int(k.pt[1])
seed_pt = int(k.pt[0]),int(k.pt[1])
cv2.floodFill(result2, maskborder, seed_pt, (0,255, 0))



#cv2.imshow('main',im)
#cv2.imshow('gray',imgray)
#cv2.imshow('borders',maskborder)
#cv2.imshow('threshold2',imthresh2)
#cv2.imshow('threshold3',imthresh3)
#cv2.imshow('threshold4',imthresh4)
cv2.imshow("Result", result2)
cv2.imwrite("result.png",result2)

cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 如何在 Matplotlib 中填充任意闭合区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36256661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com