2017-11-21 23 views
0

以下の画像とコードは、私が実行している実験を反映したおもちゃの例です。 私は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

+0

私の英語が悪いので、私はあなたの目的だものを手に入れることができない...あなたは「全体」ディスク、または何を取得したいですか? – Silencer

+0

はいグレーの部分ではなく、青い部分からディスクを抽出したいです。 – Michael

+1

私はこのように "ディスク"を分離しようとします:https://i.stack.imgur.com/B7kT6.jpg – Silencer

答えて

1

起源が大きすぎるので、私は、ディスクイメージをリサイズ。したがって、関数内のパラメータを変更することができます。


ソース:私はディスクがより明確である、S(HSV)で見つかったので、私

enter image description here

は、 "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() 
+1

この問題のシンプルで洗練されたコード!ヒストグラムとカラーチャンネルをプロットするためのスクリプトが既に設定されていますか?それはとても便利です、私は同じことをやろうと思います。 –

関連する問題