Ask Your Question
0

How do i remove background noise in binary picture?

asked 2020-04-09 16:20:26 -0600

nesse98 gravatar image

I am really new to opencv. How can I remove the noise in the background without losing info?

https://i.stack.imgur.com/LQ3W3.jpg

I started with this: https://i.stack.imgur.com/YHtN6.jpg, and Otsu thresholded it. I've tried erosion, dilation, bilateral filtering. My goal is to get a rectangle on the borders so I can perspective transform the thresholded picture, but it has trouble finding contours. Or maybe is there a different and better approach?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-04-10 09:03:58 -0600

supra56 gravatar image

updated 2020-04-11 09:16:33 -0600

Solved your problem. There are many ways to get rid of background and better result printed circuit board. I added both perspective transform. main.py:

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi 3B+/4B, Buster v10.
#Date: 11th April, 2020

import cv2 as cv
import numpy as np
from utils import get_four_points

img = cv.imread('pc_board.jpg')
dst = cv.fastNlMeansDenoisingColored(img, None,
                                     20, 20,
                                     7, 21)

img=cv.cvtColor(dst, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(img, (7, 7), 7)

img = cv.bitwise_not(gray)
adapt_thresh = cv.adaptiveThreshold(img, 255,
                                    cv.ADAPTIVE_THRESH_MEAN_C,
                                    cv.THRESH_BINARY_INV, 7, -2)

# Destination image
size = (640,480,3)
im_dst = np.zeros(size, np.uint8)   
pts_dst = np.array([[0,0],
                    [size[0] - 1, 0],
                    [size[0] - 1, size[1] -1],
                    [0, size[1] - 1 ]],
                   dtype=float)

print( f'''
      Click on the four corners of the book -- top left first and
       bottom left last -- and then hit ENTER''')

# Show image and wait for 4 clicks.
cv.imshow("Image", adapt_thresh)
pts_src = get_four_points(adapt_thresh);

# Calculate the homography
h, status = cv.findHomography(pts_src, pts_dst)

# Warp source image to destination
im_dst = cv.warpPerspective(adapt_thresh, h, size[0:2])

cv.imshow('Image', im_dst)
#cv.imwrite('pc_board_3.jpg', im_dst)
cv.waitKey(0)
cv.destroyAllWindows()

utils.py:

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi 3B+/4B, Buster v10.
#Date: 11th April, 2020

import cv2
import numpy as np

def mouse_handler(event, x, y, flags, data) :

    if event == cv2.EVENT_LBUTTONDOWN :
        cv2.circle(data['im'], (x,y),3, (0,0,255), 5, 16);
        cv2.imshow("Image", data['im']);
        if len(data['points']) < 4 :
            data['points'].append([x,y])

def get_four_points(im):

    # Set up data to send to mouse handler
    data = {}
    data['im'] = im.copy()
    data['points'] = []

    #Set the callback function for any mouse event
    cv2.imshow("Image",im)
    cv2.setMouseCallback("Image", mouse_handler, data)
    cv2.waitKey(0)

    # Convert array to np.array
    points = np.vstack(data['points']).astype(float)

    return points

Before Output:

image description

After perspective transform result: Click on the four corners of the book -- top left first and bottom left last -- and then hit ENTER

image description

There is better one. this will give cleanest noising board. you will not see any lines. Also background too:

#!/usr/bin/python37
#OpenCV 4.3.0, Raspberry pi 3B+/4B, Buster v10.
#Date: 11th April, 2020

import numpy as np
import cv2 as cv
from utils import get_four_points

img = cv.imread('pc_board.jpg')
dst = cv.fastNlMeansDenoisingColored(img,None,10,10,7,21)


# Destination image
size = (640,480,3)
im_dst = np.zeros(size, np.uint8)   
pts_dst = np.array([[0,0],
                    [size[0] - 1, 0],
                    [size[0] - 1, size[1] -1],
                    [0, size[1] - 1 ]],
                   dtype=float)

print( f'''
      Click on the four corners of the book -- top left first and
       bottom left last -- and then hit ENTER''')

# Show image and wait for 4 clicks.
cv.imshow("Image", dst)
pts_src = get_four_points(dst);

# Calculate the homography
h, status = cv.findHomography(pts_src, pts_dst)

# Warp source image to destination
im_dst = cv.warpPerspective(dst, h, size[0:2])

cv.imshow('Image', im_dst)
cv.imwrite('desnoising ...
(more)
edit flag offensive delete link more

Comments

Thank you so much !!!

nesse98 gravatar imagenesse98 ( 2020-04-11 11:18:57 -0600 )edit

Btw, you can select background instead of roi.

supra56 gravatar imagesupra56 ( 2020-04-11 20:57:14 -0600 )edit

@supra56 I actually need the process to be automatic, but i will try with better pics

nesse98 gravatar imagenesse98 ( 2020-04-12 05:09:36 -0600 )edit

@supra56 can you explain to me what this did?

img = cv.bitwise_not(gray)
nesse98 gravatar imagenesse98 ( 2020-04-12 14:41:53 -0600 )edit

and why did you do it

nesse98 gravatar imagenesse98 ( 2020-04-12 14:42:10 -0600 )edit
0

answered 2020-04-10 13:46:37 -0600

andrei186 gravatar image

updated 2020-04-10 13:50:46 -0600

I am even more new to opencv, but came across a code which might help you. If I read you correctly, noise removal is needed to find your psb contours to remove perspective distortion. If so the code below allows you to set the contours of a rectangle manually by clicking mouse on the 4 corners and immediately apply transform. You do not need converting original image into gray: www.learnopencv.com/homography-exampl...

edit flag offensive delete link more

Comments

Thank you!!!

nesse98 gravatar imagenesse98 ( 2020-04-11 11:19:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-04-09 16:20:26 -0600

Seen: 3,417 times

Last updated: Apr 11 '20