Saurabh Jain
Saurabh Jain

Reputation: 53

How do I remove noise in original image using opencv

I am trying to scan working area in my Project using OpenCV, which is in form of chess board. In order to do it I have taken following steps as also mentioned in following website

Code To scan document

  1. Detect Edges
  2. Use the edges in the image to find the contour representing boundary of Workplace
  3. Apply a perspective transform to obtain top-down view of the workplace

But result I am getting is in distorted form and it is due to noise in original picture I have taken from camera.

So, whether there is any way to remove noise in original picture due to camera so that I get undistorted output in the end.

By undistorted output I mean workplace in form of black and white boxes as we have in chess board.

For your consideration I am also attaching following things

a) Original image I have used for processing b) Output image I get after complete processing

Snippet of code I used is as follows

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))

Please inform me if you need any other information.

Many thanks:)

Original image

Original Image

Output Image After Processing

Output Image After Processing

Upvotes: 1

Views: 660

Answers (1)

Is is not noise, it is aliasing, produced because your resolution is to much small compared with the squares you want to detect. Inscrease the size of your squares or your camera's resolution. Your must follow the Nyquist Rate, the size of your pixel must be al least a half of a square.

Upvotes: 1

Related Questions