0

I am looking for a way to remove the black dots around the image border using OpenCV.

Image:

enter image description here

Expected solution:

enter image description here

import cv2 

def get_img(img_name):
    lower = (0, 0, 0)
    upper = (75, 75, 75) 
    img = cv2.imread(img_name)
    #print(img)
    img_rgb_inrange = cv2.inRange(img, lower, upper)
    neg_rgb_image = ~img_rgb_inrange
    w = cv2.cvtColor(neg_rgb_image,cv2.COLOR_GRAY2RGB)
    image3 = img-w
    cv2.imwrite('img.png', image3) 

get_img('address of the img')

I used the above code that I saw in link. The results are below:


output mask I got after running the code:

enter image description here

Final Output:

enter image description here

Wondering, is there any dynamic way (instead of initializing upper and lower bounds) where I can remove the noise from the image but still maintain my foreground and background?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Iamnotperfect
  • 109
  • 1
  • 2
  • 11
  • compression artefacts. figure out: why they happen, how much they are, why your processing is sensitive to them. -- is this a PNG, with alpha channel? then opencv won't show that. it will show the RGB data, regardless of transparency. if those pixels are supposed to be transparent, then they are allowed to have arbitrary RGB data. -- [mre] please. – Christoph Rackwitz Aug 27 '22 at 18:29
  • There should be a more straightforward way, right? I have a mask why am I not able to substract it straight away? – Iamnotperfect Aug 27 '22 at 18:33
  • MRE please. that includes input data (no screenshots). – Christoph Rackwitz Aug 27 '22 at 18:37
  • I am working on the same data it's not a screenshot. For this conversation's sake, I downloaded the image from this page and ran that code :(. Not sure what else are you expecting me to do. – Iamnotperfect Aug 27 '22 at 18:52
  • 1
    wait, _that is your input_? I thought you were working on some other input, and _that_ noisy stuff was the accidental result? you shouldn't ask how to fix this, but how to _prevent_ it from happening. – Christoph Rackwitz Aug 27 '22 at 19:43
  • 1
    The binary artefacts that you see are most probably the result of a mis-decoding of a JPEG stream or inappropriate processing applied to it. You'd better understand where it comes from, instead of trying to "hide" it later. –  Aug 27 '22 at 22:08
  • Note: “image border” is the border between inside and outside the image, the first and last row and column of the image. You are talking about the edge of an object. – Cris Luengo Aug 27 '22 at 22:16

1 Answers1

3
import cv2
import matplotlib.pyplot as plt

image = cv2.imread('E3BbU.jpeg')
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(image=thresh, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)
image2 = image.copy()
cv2.drawContours(image=image2, contours=sorted(contours, key=len)[:-1], contourIdx=-1, color=(255, 255, 255), thickness=2, lineType=cv2.LINE_AA)

fig, ax = plt.subplots(2, 1)
for i, img in enumerate([image, image2]):
    ax[i].imshow(img);
    ax[i].axes.get_xaxis().set_visible(False)
    ax[i].axes.get_yaxis().set_visible(False)
plt.show()

enter image description here

Michael Hodel
  • 2,845
  • 1
  • 5
  • 10