我尝试了四种不同的方法:OpenCV
枕头/枕头
使用ImageMagick命令行
撇渣形态
方法1-OpenCV开放分割图像为灰度
打开主图像作为灰度,并使颜色允许注释
使用cv2.findContours()查找轮廓
迭代等高线并使用cv2.drawContours()根据分割图像中的标签以颜色将每个轮廓绘制到主图像上。在
文档是here。在
所以,从这幅图开始:
这张分割图像:
当对比度被拉伸,三明治被标记为grey(1),鼻部被标记为grey(2),看起来是这样的:
代码如下:#!/usr/bin/env python3
import numpy as np
import cv2
# Load images as greyscale but make main RGB so we can annotate in colour
seg = cv2.imread('segmented.png',cv2.IMREAD_GRAYSCALE)
main = cv2.imread('main.png',cv2.IMREAD_GRAYSCALE)
main = cv2.cvtColor(main,cv2.COLOR_GRAY2BGR)
# Dictionary giving RGB colour for label (segment label) - label 1 in red, label 2 in yellow
RGBforLabel = { 1:(0,0,255), 2:(0,255,255) }
# Find external contours
_,contours,_ = cv2.findContours(seg,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
# Iterate over all contours
for i,c in enumerate(contours):
# Find mean colour inside this contour by doing a masked mean
mask = np.zeros(seg.shape, np.uint8)
cv2.drawContours(mask,[c],-1,255, -1)
# DEBUG: cv2.imwrite(f"mask-{i}.png",mask)
mean,_,_,_ = cv2.mean(seg, mask=mask)
# DEBUG: print(f"i: {i}, mean: {mean}")
# Get appropriate colour for this label
label = 2 if mean > 1.0 else 1
colour = RGBforLabel.get(label)
# DEBUG: print(f"Colour: {colour}")
# Outline contour in that colour on main image, line thickness=1
cv2.drawContours(main,[c],-1,colour,1)
# Save result
cv2.imwrite('result.png',main)
结果:
方法2-PIL/枕头和Numpy打开分段图像,找到独特的颜色
打开主图像并去饱和度
在列表中的每个唯一颜色上迭代
。。。将所有像素设为白色,其他像素为黑色
。。。找到边缘并使用边缘作为遮罩在主图像上绘制颜色
代码如下:
^{pr2}$
您将得到以下结果:
方法3-ImageMagick
您也可以从命令行执行相同的操作,而无需编写任何Python,只需使用ImageMagick,它安装在大多数Linux发行版上,可用于macOS和Windows:#!/bin/bash
# Make red overlay for "1" labels
convert segmented.png -colorspace gray -fill black +opaque "gray(1)" -fill white -opaque "gray(1)" -edge 1 -transparent black -fill red -colorize 100% m1.gif
# Make yellow overlay for "2" labels
convert segmented.png -colorspace gray -fill black +opaque "gray(2)" -fill white -opaque "gray(2)" -edge 1 -transparent black -fill yellow -colorize 100% m2.gif
# Overlay both "m1.gif" and "m2.gif" onto main image
convert main.png -colorspace gray -colorspace rgb m1.gif -composite m2.gif -composite result.png
方法4-撇渣形态学
这里我使用形态学来查找1像素附近的黑色像素和靠近2像素的黑色像素。在#!/usr/bin/env python3
import skimage.filters.rank
import skimage.morphology
import numpy as np
import cv2
# Load images as greyscale but make main RGB so we can annotate in colour
seg = cv2.imread('segmented.png',cv2.IMREAD_GRAYSCALE)
main = cv2.imread('main.png',cv2.IMREAD_GRAYSCALE)
main = cv2.cvtColor(main,cv2.COLOR_GRAY2BGR)
# Create structuring element that defines the neighbourhood for morphology
selem = skimage.morphology.disk(1)
# Mask for edges of segment 1 and segment 2
# We are basically looking for pixels with value 1 in the segmented image within a radius of 1 pixel of a black pixel...
# ... then the same again but for pixels with a vaue of 2 in the segmented image within a radius of 1 pixel of a black pixel
seg1 = (skimage.filters.rank.minimum(seg,selem) == 0) & (skimage.filters.rank.maximum(seg, selem) == 1)
seg2 = (skimage.filters.rank.minimum(seg,selem) == 0) & (skimage.filters.rank.maximum(seg, selem) == 2)
main[seg1,:] = np.asarray([0, 0, 255]) # Make segment 1 pixels red in main image
main[seg2,:] = np.asarray([0, 255, 255]) # Make segment 2 pixels yellow in main image
# Save result
cv2.imwrite('result.png',main)
注意:JPEG有损-不要将分割后的图像保存为JPEG,请使用PNG或GIF!在
关键词:Python、PIL、pill、OpenCV、分割、分割、标记、图像、图像处理、边缘、轮廓、skimage、ImageMagick、scikit图像、形态学、排名、排序过滤器、像素邻接。在