通过分析邻居像素强度来绘制磁盘

时间:2017-11-21 12:57:58

标签: python image numpy opencv

下面的图片和代码是一个玩具示例,应该反映我正在运行的实验。 我想提取一个对应于图片边界的磁盘,其中像素强度相同或相似(在本例中为蓝色圆盘)

使用HoughCircles程序,我可以提取图片中最可能的圆圈的中心。 从那里我想从探测到的中心在不同半径(更高或更低)的中心探测360°,以定义下图中蓝色的边界(最大半径和最小半径)。

我该怎么做? 我尝试通过应用多个蒙版来分析直方图,但没有成功。 绿色圆圈是HoughCircles检测到的绿色圆圈,蓝色和红色圆圈是+/- 15%半径圆圈。

import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread("./picture.jpg")
output = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 2, 800,
                                          param1=300,
                                          param2=1,
                                          minRadius=100,
                                          maxRadius=0)

if circles is not None:
    # convert the (x, y) coordinates and radius of the circles to integers
    circles = np.round(circles[0, :]).astype("int")
    output = image.copy()
    # loop over the (x, y) coordinates and radius of the circles
    for (x, y, r) in circles:
        # draw the circle in the output image, then draw a rectangle
        # corresponding to the center of the circle
        cv2.circle(output, (x, y), r, (0, 255, 0), 2)
        cv2.rectangle(output, (x - 2, y - 2), (x + 2, y + 2), (0, 128, 255), -1)
        # create the mask and explore histograms
        # height,width,depth = output.shape
        # mask = np.zeros((height,width), np.uint8)
        # cv2.circle(mask, (x, y), int(round(r - (r* .15))), 1, thickness=-1)
        # output = cv2.bitwise_and(output, output, mask=mask)

        # hist_full = cv2.calcHist([output],[0],None,[256],[0,256])
        # hist_mask = cv2.calcHist([output],[0],mask,[256],[0,256])
        # plt.hist(image.ravel(),256,[0,256]); plt.show()
        # plt.plot(hist_full), 
        # plt.plot(hist_mask)
        # plt.xlim([0,256]) 
        # plt.show()

        cv2.circle(output, (x, y), int(round(r * 1.15)), (255, 0, 0), 2)
        cv2.circle(output, (x, y), int(round(r - (r* .15))), (0, 0, 255), 2)


    # show the output image
    cv2.imshow("output", np.hstack([image, output]))
    cv2.waitKey(0)

enter image description here

1 个答案:

答案 0 :(得分:1)

我调整了磁盘映像的大小,因为原点太大了。因此,您可以修改函数中的参数。

来源

enter image description here

我在S(HSV)中发现磁盘更清晰,所以我在" S" 中做了精干。

enter image description here

结果:

enter image description here

您可以使用代码重现结果。

#!/usr/bin/python3
# 2017.11.21 21:03:09 CST
# 2017.11.22 23:21:42 CST
# 2017.11.25 16:32:46 CST

import cv2
import numpy as np

img = cv2.imread("disk2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

## Canny edge in S(HSV)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
canny = cv2.Canny(s, 30, 200)

## The inner circle using gray
circles1 = cv2.HoughCircles(gray, method = cv2.HOUGH_GRADIENT,
        dp = 2, minDist = 100,
        param1=200, param2=100,
        minRadius=80, maxRadius=200)


## The outer circle using canny
circles2 = cv2.HoughCircles(canny, method = cv2.HOUGH_GRADIENT,
        dp = 2, minDist = 100,
        param1=200, param2=100,
        minRadius=200, maxRadius=0)

x1,y1, r1 = circles1[0][0]
x2,y2, r2 = circles2[0][0]

## create the mask
mask = np.zeros_like(canny)
cv2.circle(mask, (x2, y2), r2, 255, -1)
cv2.circle(mask, (x1, y1), r1, 0, -1)

## crop
imask = mask > 0
masked = np.zeros_like(img)
masked[imask] = img[imask]

cv2.imshow("canny", canny)
cv2.imshow("mask", mask)
cv2.imshow("croped", masked)
cv2.waitKey()
cv2.destroyAllWindows()