【问题标题】:opencv area of contour轮廓的opencv区域
【发布时间】:2018-04-05 20:28:04
【问题描述】:

我正在尝试从图片中找到动物的轮廓。我们假设它是一只鸡。从图片中我可以找到它的轮廓,但它们没有闭合。此外,我从白色背景(与鸡一样)中得到很多噪音。 我正在使用在 stackoverflow 上找到的简单代码。

import numpy as np
import cv2

img = cv2.imread('lateral.jpg')
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blurred = cv2.GaussianBlur(imgray, (5, 5), 0)
# edged = cv2.Canny(blurred, 10, 11) # 10 and 40 to be more perceptive
# contours_canny= cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
edges = cv2.Canny(imgray, 10,30)    

cv2.imshow('edges', edges)
k = cv2.waitKey()

有没有办法找到这只鸡的轮廓? 提前致谢。

【问题讨论】:

  • 在 Photoshop Express 中快速检查:如果您将饱和度最大化,鸡肉会染成黄色,而墙壁和地板会变成蓝色或中性色。所以泵饱和通道并使用grabCut。对于这种特殊情况。
  • 你需要对这张确切的照片做吗?

标签: opencv opencv-contour


【解决方案1】:

寻找轮廓很容易。问题是您的图像在鸡肉和背景之间的对比度很低。所以,你使用精明边缘的想法还不错,它只需要一些后期处理。

我猜这就是你要找的东西:

import cv2
import numpy as np


image = cv2.imread("./chicken.jpg", cv2.IMREAD_COLOR)
image = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 


imgray = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)[...,0]
edges = cv2.Canny(imgray, 10,30)    
blurred = cv2.GaussianBlur(edges, (9, 9), 0)
clahe = cv2.createCLAHE(clipLimit=5.0, tileGridSize=(32,32))
contrast = clahe.apply(blurred)
ret, thresh = cv2.threshold(contrast, 20, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
_, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

maxArea = 0
best = None
for contour in contours:
  area = cv2.contourArea(contour)
  print (area)
  if area > maxArea :

    maxArea = area
    best = contour


cv2.drawContours(image, [best], 0, (0, 0, 255), -1)

while True:
  cv2.imshow("result", image)
  k = cv2.waitKey(30) & 0xff
  if k == 27:
      break

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-11-09
    • 2018-05-15
    • 2015-02-02
    • 1970-01-01
    • 2019-12-07
    • 2012-01-12
    相关资源
    最近更新 更多