如何使用opencv

时间:2019-09-19 15:15:29

标签: python image opencv image-processing

我正在尝试使用国际象棋棋盘形式的OpenCV扫描项目中的工作区域。为了做到这一点,我已按照以下网站中所述的步骤进行操作

Code To scan document

  1. 检测边缘
  2. 使用图像中的边缘找到代表工作场所边界的轮廓
  3. 应用透视变换以获得工作场所的自顶向下视图

但是我得到的结果是失真的,这是由于我从相机拍摄的原始照片中的噪点所致。

因此,是否有任何方法可以消除由于相机引起的原始图像噪点,以便最终获得不失真的输出。

通过不失真的输出,我的意思是工作场所采用黑白棋盘形式,就像我们在国际象棋棋盘中一样。

为您考虑,我还附上以下内容

a)我用于处理的原始图像 b)完成处理后得到的输出图像

我使用的代码段如下

image = cv2.imread(arg["image"])
(h, w, d) = image.shape

#Resize image 
ratio = image.shape[0]/500.0
orig = image.copy()
image = imutils.resize(image,height = 500)

#Find edge, blur it
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)   
gray = cv2.GaussianBlur(gray,(3,3),0)
edged = cv2.Canny(gray,75,200)


# find the contours in the edged image, keeping only the 
# largest ones, and initialize the screen contour
cnts = cv2.findContours(edged.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts,key = cv2.contourArea, reverse = True)[:5] 

#loop over the contours 
for c in cnts: 
    #approximate the contour
    peri = cv2.arcLength(c,True)
    approx = cv2.approxPolyDP(c,0.02*peri, True)

    #if our approximated contour has four points, then we
    # can assume that we have found our screen 
    if len(approx) == 4:
        screenCnt = approx
        break
# show the contour (outline) of the piece of paper

cv2.drawContours(image,[screenCnt],-1,(0,255),2)
cv2.imshow("Outline",image)

#apply the four point transform to obtain a top-down 
#view of original image 
warped = four_point_transform(orig,screenCnt.reshape(4,2)*ratio)

#convert the wrapped image to grayscle, then threshold it 
#to give it that 'black and white ' paper effect
warped = cv2.cvtColor(warped,cv2.COLOR_BGR2GRAY)
T = threshold_local(warped,11,offset =10,method = "gaussian")
warped = (warped >T).astype("uint8")*255

#show the original and scanned images
print("STEP3: Apply perspective transform")
cv2.imshow("Original",imutils.resize(orig,height=650))
cv2.imshow("Scanned",imutils.resize(warped,height=650))
cv2.imwrite("OutputImage.png",imutils.resize(warped,height=650))

如果您需要其他任何信息,请通知我。

非常感谢:)

原始图片

Original Image

处理后输出图像

Output Image After Processing

1 个答案:

答案 0 :(得分:1)

不是噪声,而是混叠,是由于您的分辨率与要检测的平方相比很小而产生的。增大正方形的大小或相机的分辨率。 您必须遵循Nyquist Rate,像素大小必须至少为正方形的一半。