codai
codai

Reputation: 11

How can I remove the bright glare regions in image

I have some tomato images with bright shadow on tomatoes. I want to remove/reduce these bright shadow points. Is there any suggestion?

enter image description here

I tried below code but It did not solve my problem:

def decrease_brightness(img, value=30):
  hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  h, s, v = cv2.split(hsv)

  lim = 255 - value
  v[v >= lim] -= value

  final_hsv = cv2.merge((h, s, v))
  img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
  return img

image = decrease_brightness(image, value=50)

Upvotes: 1

Views: 5356

Answers (1)

fmw42
fmw42

Reputation: 53091

Here is how to do the inpainting in Python/OpenCV.

Note that shadows are dark. You want to remove the bright glare regions. Please use the correct terms so that you do not confuse others on the forum. Refer to a dictionary.

  • Read the input
  • Threshold on the gray background using cv2.inRange()
  • Apply morphology to close and dilate
  • Floodfill the outside with black to make a mask image
  • Use the mask to do the inpainting (two methods)
  • Save the results

Input:

enter image description here

import cv2
import numpy as np

# read image
img = cv2.imread('tomato.jpg')
hh, ww = img.shape[:2]

# threshold
lower = (150,150,150)
upper = (240,240,240)
thresh = cv2.inRange(img, lower, upper)

# apply morphology close and open to make mask
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (25,25))
morph = cv2.morphologyEx(morph, cv2.MORPH_DILATE, kernel, iterations=1)

# floodfill the outside with black
black = np.zeros([hh + 2, ww + 2], np.uint8)
mask = morph.copy()
mask = cv2.floodFill(mask, black, (0,0), 0, 0, 0, flags=8)[1]

# use mask with input to do inpainting
result1 = cv2.inpaint(img, mask, 101, cv2.INPAINT_TELEA)
result2 = cv2.inpaint(img, mask, 101, cv2.INPAINT_NS)

# write result to disk
cv2.imwrite("tomato_thresh.jpg", thresh)
cv2.imwrite("tomato_morph.jpg", morph)
cv2.imwrite("tomato_mask.jpg", mask)
cv2.imwrite("tomato_inpaint1.jpg", result1)
cv2.imwrite("tomato_inpaint2.jpg", result2)

# display it
cv2.imshow("IMAGE", img)
cv2.imshow("THRESH", thresh)
cv2.imshow("MORPH", morph)
cv2.imshow("MASK", mask)
cv2.imshow("RESULT1", result1)
cv2.imshow("RESULT2", result2)
cv2.waitKey(0)

Threshold Image:

enter image description here

Morphology and Floodfill Image:

enter image description here

Mask Image:

enter image description here

Inpaint Telea:

enter image description here

Inpaint Navier-Stokes:

enter image description here

Upvotes: 4

Related Questions