運用measure.regionprops

import cv2
import numpy as np
from skimage.measure import label,regionprops
import math

def detection(c):
perimeter = cv2.arcLength(c, True)
approximate = cv2.approxPolyDP(c, 0.04*perimeter, True)
return approximate

def hough_detection(img):
img = img.astype(np.uint8)
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
_, thre_img = cv2.threshold(gray_img, 253, 255, cv2.THRESH_BINARY_INV)
circleDetect2 = cv2.HoughCircles(gray_img, cv2.HOUGH_GRADIENT, 1, 100, param1=40, param2=15, minRadius=30,
maxRadius=100)
circles = circleDetect2[0, :, :]
for i in circles[:]:
cv2.circle(img, (i[0], i[1]), i[2], (255, 0, 0), 2)
# cv2.circle(img, (i[0], i[1]), 1, (255, 0, 0), 2)
# cv2.imwrite('cleaning.png', img)
return img

def find_contours(img):
img = img.astype(np.uint8)
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
result_img = gray_img.copy()
crop_img = np.zeros([gray_img.shape[0], gray_img.shape[1]], np.uint8)
# gray_img = cv2.blur(gray_img, (3, 3))
_, thre_img = cv2.threshold(gray_img, 210, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
thre_img = cv2.morphologyEx(thre_img, cv2.MORPH_OPEN, kernel)
im_floodfill = thre_img.copy()

# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = thre_img.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)

# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0, 0), 255);

# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = thre_img | im_floodfill_inv
#_, contours, hierachy = cv2.findContours(thre_img(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
'''contours, hierachy = cv2.findContours(thre_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# img = hough_detection(img)
for idx, cnt in enumerate(contours):
    M = cv2.moments(cnt) #像像矩
    cnt_area = cv2.contourArea(cnt)  #廓廓積積
    (x, y), radius = cv2.minEnclosingCircle(cnt)  #最小外接圓
    circle_area = np.pi * radius * radius
    ratio = cnt_area / circle_area
    if ratio >= 0.7:
        cnt = cnt.reshape(-1, 2)
        min = np.min(cnt[:, 1])
        max = np.max(cnt[:, 1])
        for y in range(min+1, max):
            indices = np.argwhere(cnt[:, 1] == y)
            indices = indices.reshape(-1)
            contour = cnt[indices]
            index = np.argsort(contour[:, 0])
            x_min = np.min(contour[:, 0])
            x_max = np.max(contour[:, 0])
            for x in range(x_min, x_max+1):
                result_img[y, x] = result_img[y, x] + (255 - gray_img[y, x])
    (x, y, radius) = np.int0((x, y, radius))  # 圓心和半徑取整
    cv2.circle(img, (x, y), radius, (0, 0, 255), 2)'''
label_img = label(im_out, connectivity=2)
props = regionprops(label_img)
ind=[]
area=[]
for idx, prop in enumerate(props):
    # for prop in props:
    #ind = []
    p = prop.area
    per = prop.perimeter
    #coo=prop.coords
    roundness = 4 * math.pi * p / (per * per)
    if roundness >= 0.6 and p>5000:

        ind.append(idx)

    area.append(p)
print(area)
for i in range(len(ind)):

  coo=props[ind[i]].coords
  y=coo[:,1]
  x=coo[:,0]

  for j in range(len(coo)):
    im_out[x[j]][y[j]]=0
np.savetxt('new.csv', coo, delimiter=',')

#print(coo)
#thre_img[]
    #result_img=np.where(a > 0, a, 0)
    #if roundness >= 0.7:
        #thre_img[coo]=0
cv2.imwrite('RemoveResult.PNG', im_out)
cv2.imwrite('contours.png', img)

if name == 'main':
img = cv2.imread('./image/13-338~A.004.A.TIF')
find_contours(img)

https://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.regionprops

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市对蒲,隨后出現(xiàn)的幾起案子钩蚊,更是在濱河造成了極大的恐慌,老刑警劉巖蹈矮,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件砰逻,死亡現(xiàn)場離奇詭異,居然都是意外死亡泛鸟,警方通過查閱死者的電腦和手機蝠咆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來北滥,“玉大人刚操,你說我怎么就攤上這事≡儆螅” “怎么了菊霜?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長济赎。 經(jīng)常有香客問我鉴逞,道長,這世上最難降的妖魔是什么司训? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任构捡,我火速辦了婚禮,結(jié)果婚禮上壳猜,老公的妹妹穿的比我還像新娘勾徽。我一直安慰自己,他們只是感情好蓖谢,可當(dāng)我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布捂蕴。 她就那樣靜靜地躺著譬涡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪啥辨。 梳的紋絲不亂的頭發(fā)上涡匀,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天,我揣著相機與錄音溉知,去河邊找鬼陨瘩。 笑死,一個胖子當(dāng)著我的面吹牛级乍,可吹牛的內(nèi)容都是我干的舌劳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼玫荣,長吁一口氣:“原來是場噩夢啊……” “哼甚淡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起捅厂,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤贯卦,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后焙贷,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體撵割,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年辙芍,在試婚紗的時候發(fā)現(xiàn)自己被綠了啡彬。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡故硅,死狀恐怖庶灿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情吃衅,我是刑警寧澤跳仿,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站捐晶,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏妄辩。R本人自食惡果不足惜惑灵,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望眼耀。 院中可真熱鬧英支,春花似錦、人聲如沸哮伟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至池凄,卻和暖如春抡驼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肿仑。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工致盟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人尤慰。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓馏锡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親伟端。 傳聞我的和親對象是個殘疾皇子杯道,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容