努力在对象周围绘制边界并且对象在图像内部
Working to draw boundary around an object and the object is inside the image
我试图在一个对象周围画一个边界,在这种情况下,假设一个病变对象在一个图像中,背景简单,噪音不大。我拍摄了图像并读取了像素值。
并尝试使用活动轮廓算法计算沿对象边界的 XY 坐标,但绘制的边界位于整个图像的边缘而不是沿对象内部的边界,我无法更好地获得好坐标。那么,如果有任何更好的方法来 find/draw 图像内部对象周围的边界,或者我应该使用 openCV 和 Xcode 更好地制作一个 ios 应用程序,你能建议我吗?欢迎推荐。
我还没有尝试使用 OpenCV 的活动轮廓,但事实上你在图像边缘有近乎黑色和近乎白色的轮廓可能没有帮助
(输入上面评论部分提供的图片)
(这是一个opencv+python原型)
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('skin.png')
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
red = rgb_img[:,:,0]
height, width, channels = rgb_img.shape
创建遮罩并应用泛光填充
mask = np.zeros((height+2, width+2), np.uint8)
flooded = red.copy()
flags = 4 | cv2.FLOODFILL_MASK_ONLY
cv2.floodFill(flooded, mask, (8, 8), 1, 2, 2, flags)
plt.imshow(1-mask)
plt.colorbar()
plt.show()
有点形态清理
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))
omask = cv2.morphologyEx(1-mask, cv2.MORPH_OPEN, kernel)
plt.imshow(omask)
plt.colorbar()
plt.show()
找到轮廓:
contours, hierarchy = cv2.findContours(omask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
求最大面积等高线:
largest_contour = []
largest_area = 0
for contour in contours:
area = cv2.contourArea(contour)
if area > largest_area:
largest_area = area
largest_contour = contour
显示结果(选择矩形和轮廓):
x,y,w,h = cv2.boundingRect(largest_contour)
cv2.drawContours(rgb_img, [largest_contour], -1, (0, 128, 128), 3)
cv2.rectangle(rgb_img, (x,y), (x+w, y+h), (0, 0, 0), 2)
plt.imshow(rgb_img)
plt.show()
这是一个非常直接和简单的解决方案,但并不完美。它可以通过 grabcut(以计算复杂性为代价)或其他技术进行改进。
我试图在一个对象周围画一个边界,在这种情况下,假设一个病变对象在一个图像中,背景简单,噪音不大。我拍摄了图像并读取了像素值。
并尝试使用活动轮廓算法计算沿对象边界的 XY 坐标,但绘制的边界位于整个图像的边缘而不是沿对象内部的边界,我无法更好地获得好坐标。那么,如果有任何更好的方法来 find/draw 图像内部对象周围的边界,或者我应该使用 openCV 和 Xcode 更好地制作一个 ios 应用程序,你能建议我吗?欢迎推荐。
我还没有尝试使用 OpenCV 的活动轮廓,但事实上你在图像边缘有近乎黑色和近乎白色的轮廓可能没有帮助
(输入上面评论部分提供的图片)
(这是一个opencv+python原型)
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('skin.png')
rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
red = rgb_img[:,:,0]
height, width, channels = rgb_img.shape
创建遮罩并应用泛光填充
mask = np.zeros((height+2, width+2), np.uint8)
flooded = red.copy()
flags = 4 | cv2.FLOODFILL_MASK_ONLY
cv2.floodFill(flooded, mask, (8, 8), 1, 2, 2, flags)
plt.imshow(1-mask)
plt.colorbar()
plt.show()
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7))
omask = cv2.morphologyEx(1-mask, cv2.MORPH_OPEN, kernel)
plt.imshow(omask)
plt.colorbar()
plt.show()
contours, hierarchy = cv2.findContours(omask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE )
求最大面积等高线:
largest_contour = []
largest_area = 0
for contour in contours:
area = cv2.contourArea(contour)
if area > largest_area:
largest_area = area
largest_contour = contour
显示结果(选择矩形和轮廓):
x,y,w,h = cv2.boundingRect(largest_contour)
cv2.drawContours(rgb_img, [largest_contour], -1, (0, 128, 128), 3)
cv2.rectangle(rgb_img, (x,y), (x+w, y+h), (0, 0, 0), 2)
plt.imshow(rgb_img)
plt.show()
这是一个非常直接和简单的解决方案,但并不完美。它可以通过 grabcut(以计算复杂性为代价)或其他技术进行改进。