python中的图像分割

python中的图像分割,python,opencv,image-segmentation,Python,Opencv,Image Segmentation,我有这个形象 我正在寻找python解决方案,根据图像中的轮廓将图像中的形状分割成更小的部分 我在OpenCV中研究了Canny和findContours的解决方案,但它们都不适合我 编辑: 使用的代码: 使用Canny方法 使用findContours方法 诀窍是使模糊的单像素边界稍微大胆一些。我通过将任何具有两个相邻黑色像素(上方、下方、左侧或右侧)的白色像素更改为黑色来实现。(不过,我做得非常慢。我很确定,一定有更聪明的方法可以用OpenCV或Numpy来做。) 这是我的密码: #!/us

我有这个形象

我正在寻找python解决方案,根据图像中的轮廓将图像中的形状分割成更小的部分

我在OpenCV中研究了Canny和findContours的解决方案,但它们都不适合我

编辑: 使用的代码:

使用Canny方法 使用findContours方法
诀窍是使模糊的单像素边界稍微大胆一些。我通过将任何具有两个相邻黑色像素(上方、下方、左侧或右侧)的白色像素更改为黑色来实现。(不过,我做得非常慢。我很确定,一定有更聪明的方法可以用OpenCV或Numpy来做。)

这是我的密码:

#!/usr/bin/env python 

import numpy as np
import cv2

THRESH = 240

orig = cv2.imread("map.png")
img = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)

# Make the faint 1-pixel boundary bolder
rows, cols = img.shape
new_img = np.full_like(img, 255)    # pure white image
for y in range(rows):
    if not (y % 10):
        print ('Row = %d (%.2f%%)' % (y, 100.*y/rows))
    for x in range(cols):
        score  = 1 if y > 0 and img.item(y-1, x) < THRESH else 0
        score += 1 if x > 0 and img.item(y, x-1) < THRESH else 0
        score += 1 if y < rows-1 and img.item(y+1, x) < THRESH else 0
        score += 1 if x < cols-1 and img.item(y, x+1) < THRESH else 0
        if img.item(y, x) < THRESH or score >= 2:
            new_img[y, x] = 0       # black pixels show boundary

cv2.imwrite('thresh.png', new_img)

# Find all contours on the map
_th, contours, hierarchy = cv2.findContours(new_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print "Number of contours detected = %d" % len(contours)

# Fill second level regions on the map
coln = 0
colors = [
    [127, 0, 255],
    [255, 0, 127],
    [255, 127, 0],
    [127, 255, 0],
    [0, 127, 255],
    [0, 255, 127],
]
hierarchy = hierarchy[0]
for i in range(len(contours)):
    area = cv2.contourArea(contours[i])
    if hierarchy[i][3] == 1:
        print (i, area)
        coln = (coln + 1) % len(colors)
        cv2.drawContours(orig, contours, i, colors[coln], -1)

cv2.imwrite("colored_map.png", orig)
#/usr/bin/env python
将numpy作为np导入
进口cv2
THRESH=240
orig=cv2.imread(“map.png”)
img=cv2.CVT颜色(原色,cv2.COLOR\u BGR2GRAY)
#使模糊的1像素边界更粗体
行,列=img.shape
new_img=np.full_like(img,255)#纯白色图像
对于范围内的y(行):
如果不是(y%10):
打印('Row=%d(%.2f%%)'%(y,100.*y/行))
对于范围内的x(cols):
如果y>0且模拟项目(y-1,x)<0,则得分=1
如果x>0且模拟项目(y,x-1)<0,则得分+=1
如果y<行-1和模拟项目(y+1,x)<阈值0,则得分+=1
如果x=2:
新的_img[y,x]=0#黑色像素显示边界
cv2.imwrite('thresh.png',new_img)
#找到地图上的所有等高线
_等高线,层次=cv2.findContours(新建,cv2.RETR\u树,cv2.CHAIN\u近似值,无)
打印“检测到的轮廓数=%d”%len(轮廓)
#填充地图上的二级区域
coln=0
颜色=[
[127, 0, 255],
[255, 0, 127],
[255, 127, 0],
[127, 255, 0],
[0, 127, 255],
[0, 255, 127],
]
层次结构=层次结构[0]
对于范围内的i(透镜(轮廓)):
面积=cv2。轮廓面积(轮廓[i])
如果层次结构[i][3]==1:
打印(一、面积)
coln=(coln+1)%len(颜色)
cv2.绘制轮廓(原始、轮廓、i、颜色[coln],-1)
cv2.imwrite(“colored_map.png”,orig)
输入图像:

输出图像:


在这里,我只对最外面轮廓的直系子体着色(
层次[I][3]==1
)。但您可以更改它以排除湖泊。

请分享您尝试的示例代码,以便人们可以找到并指出您的代码需要更改的地方。图像在哪里。?这是图像的链接:谢谢安德烈,这就是我要找的。@Esther,我很高兴它有帮助。请考虑选择我的答案为“最佳答案”。对不起,我是新手。我可以知道我应该怎么做才能选择你最好的答案吗answer@Esther,“要将答案标记为已接受,请单击答案旁边的复选标记,将其从空心切换为绿色”。请参阅。注意:使用
cv2.filter2D
函数将自定义内核
[[0,1,0],[1,0,1],[0,1,0]]
作为矩阵来计算
分数,这是使边框更粗体的更快方法。
import numpy as np 
import argparse 
import cv2

image = cv2.imread('area_of_blob_maxcontrast_white.png')

lower = np.array([0, 0, 0]) upper = np.array([15, 15, 15]) shapeMask = cv2.inRange(image, lower, upper)

(_,cnts, _) = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_SIMPLE) print "I found %d black shapes" % (len(cnts)) cv2.imshow("Mask", shapeMask)

for c in cnts:
    # draw the contour and show it
    cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
    cv2.imshow("Image", image)
    cv2.waitKey(0)
#!/usr/bin/env python 

import numpy as np
import cv2

THRESH = 240

orig = cv2.imread("map.png")
img = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)

# Make the faint 1-pixel boundary bolder
rows, cols = img.shape
new_img = np.full_like(img, 255)    # pure white image
for y in range(rows):
    if not (y % 10):
        print ('Row = %d (%.2f%%)' % (y, 100.*y/rows))
    for x in range(cols):
        score  = 1 if y > 0 and img.item(y-1, x) < THRESH else 0
        score += 1 if x > 0 and img.item(y, x-1) < THRESH else 0
        score += 1 if y < rows-1 and img.item(y+1, x) < THRESH else 0
        score += 1 if x < cols-1 and img.item(y, x+1) < THRESH else 0
        if img.item(y, x) < THRESH or score >= 2:
            new_img[y, x] = 0       # black pixels show boundary

cv2.imwrite('thresh.png', new_img)

# Find all contours on the map
_th, contours, hierarchy = cv2.findContours(new_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print "Number of contours detected = %d" % len(contours)

# Fill second level regions on the map
coln = 0
colors = [
    [127, 0, 255],
    [255, 0, 127],
    [255, 127, 0],
    [127, 255, 0],
    [0, 127, 255],
    [0, 255, 127],
]
hierarchy = hierarchy[0]
for i in range(len(contours)):
    area = cv2.contourArea(contours[i])
    if hierarchy[i][3] == 1:
        print (i, area)
        coln = (coln + 1) % len(colors)
        cv2.drawContours(orig, contours, i, colors[coln], -1)

cv2.imwrite("colored_map.png", orig)