如何使用open去除原始图像中的噪声

2024-05-12 18:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用OpenCV扫描我项目中的工作区,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


Tags: the图像imageitcv2形式contourheight
1条回答
网友
1楼 · 发布于 2024-05-12 18:10:17

这不是噪音,而是混叠,因为你的分辨率比你想检测的方块小得多。增加正方形的大小或相机的分辨率。 你必须遵循Nyquist Rate,像素的大小必须至少是半个正方形。你知道吗

相关问题 更多 >