我撰寫了一個簡單的代碼來搜索檔案中的圓圈(因為印章是圓形的)。
但是由于影像質量差,列印輪廓模糊,opencv不能一直檢測到。我在 Photoshop 中編輯了圖片并增強了深色。我保存了圖片并發送它進行處理。它幫助了我。Opencv 已經識別出一個代表低質量列印的圓圈(在高質量檔案中沒有這樣的問題)。我的代碼:
import numpy as np
import cv2
img = cv2.imread(r"C:\buh\doc.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# I tried experimenting with bluer, but opencv doesn't see circles in this case
# blurred = cv2.bilateralFilter(gray.copy(), 15, 15, 15 )
# imS = cv2.resize(blurred, (960, 540))
# cv2.imshow('img', imS)
# cv2.waitKey(0)
minDist = 100
param1 = 30 #500
param2 = 100 #200 #smaller value-> more false circles
minRadius = 90
maxRadius = 200 #10
# docstring of HoughCircles: HoughCircles(image, method, dp, minDist[, circles[, param1[, param2[, minRadius[, maxRadius]]]]]) -> circles
circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, minDist, param1=param1, param2=param2, minRadius=minRadius, maxRadius=maxRadius)
if circles is not None:
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2)
# Show result for testing:
imS = cv2.resize(img, (960, 540))
cv2.imshow('img', imS)
cv2.waitKey(0)
檔案中的印章是照片中的圓圈:

不幸的是,我無法添加原始印章所在檔案的照片,因為這是私人資訊...
因此,在嘗試尋找圓圈之前,我需要增強照片中的黑色陰影。我怎樣才能做到這一點?如果有人已經遇到過這種情況,我也會聽取其他改進印章(郵票)輪廓的建議。
謝謝你。
例子:

uj5u.com熱心網友回復:
這是一個簡單的方法:
獲取二值影像。加載影像,轉換為灰度,高斯模糊,然后是 Otsu 的閾值。
將小輪廓合并成一個大輪廓。我們使用擴張
cv2.dilate來將圓圈合并成一個輪廓。尋找外部輪廓。最后我們找到帶有外部標志的外部輪廓和
cv2.RETR_EXTERNALcv2.drawContours()
影像管道的可視化
輸入影像
二值影像的閾值
擴張
檢測到的綠色輪廓
代碼
import cv2
import numpy as np
# Load image, grayscale, Gaussian blur, Otsus threshold, dilate
image = cv2.imread('3.PNG')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
dilate = cv2.dilate(thresh, kernel, iterations=1)
# Find contours
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(image, [c], -1, (36,255,12), 3)
cv2.imshow('image', image)
cv2.imshow('dilate', dilate)
cv2.imshow('thresh', thresh)
cv2.waitKey()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446934.html
標籤:Python 图片 opencv 图像处理 计算机视觉
上一篇:如何將段落放置在影像塊的右側?
