亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在輪廓 opencv 上找到主色

在輪廓 opencv 上找到主色

幕布斯6054654 2021-07-05 12:13:56
我試圖在輪廓(黑色或白色)內找到主色。我正在使用 OpenCV 讀取圖像并在黑色圖像上提取白色。這是我到目前為止得到的:綠色輪廓是輪廓,藍色線條是邊界框。所以我在這個例子中我試圖提取數字 87575220 但正如你所看到的,它也識別出一些隨機偽像,例如字母 G。我認為解決方案是在輪廓內找到主色,這種顏色應該是接近白色。我不知道如何做到這一點。這是我目前擁有的代碼:import argparseimport cv2import imutilsimport numpy as npparser = argparse.ArgumentParser()parser.add_argument("--image", "-i", required=True, help="Image to detect blobs from")args = vars(parser.parse_args())image = cv2.imread(args["image"])image = imutils.resize(image, width=1200)grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(grey)maxval_10 = maxVal * 0.5ret, threshold = cv2.threshold(grey, maxval_10, 255, cv2.THRESH_BINARY)canny = cv2.Canny(grey, 200, 250)lines = cv2.HoughLines(canny, 1, np.pi / 180, 140)print(maxVal)theta_min = 60 * np.pi / 180.theta_max = 120 * np.pi / 180.0theta_avr = 0theta_deg = 0filteredLines = []for rho, theta in lines[0]:    a = np.cos(theta)    b = np.sin(theta)    x0 = a * rho    y0 = b * rho    x1 = int(x0 + 1000 * (-b))    y1 = int(y0 + 1000 * (a))    x2 = int(x0 - 1000 * (-b))    y2 = int(y0 - 1000 * (a))    cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)    if theta_min <= theta <= theta_max:        filteredLines.append(theta)        theta_avr += thetaif len(filteredLines) > 0:    theta_avr /= len(filteredLines)    theta_deg = (theta_avr / np.pi * 180) - 90else:    print("Failed to detect skew")image = imutils.rotate(image, theta_deg)canny = imutils.rotate(canny, theta_deg)im2, contours, hierarchy = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)# cv2.drawContours(image, contours, -1, (0, 255, 0), 1)cv2.imshow('Contours', im2)boundingBoxes = []filteredContours = []for cnt in contours:    (x, y, w, h) = cv2.boundingRect(cnt)    if (h > 20 and h < 90 and w > 5 and w < h):        if cv2.contourArea(cnt, True) <= 0:            boundingBoxes.append((x, y, w, h))            filteredContours.append(cnt)
查看完整描述

3 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您可以從每個輪廓創建一個蒙版:


mask = np.zeros(image.shape, dtype="uint8")

cv2.drawContours(mask, [cnt], -1, 255, -1)

然后計算掩碼內所有像素的平均值:


mean = cv2.mean(image, mask=mask)

然后檢查是否mean足夠接近白色


查看完整回答
反對 回復 2021-07-21
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

由于顏色空間屬性,顏色和均值不能很好地匹配。我會創建一個直方圖并選擇最常見的一個(也可以應用一些顏色下采樣)


查看完整回答
反對 回復 2021-07-21
  • 3 回答
  • 0 關注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號