[sample image]
Here's my code
import cv2
import numpy as np
image = cv2.imread("1.bmp")
img_grey = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(img_grey,120,255,cv2.THRESH_BINARY)
ret, thresh = cv2.threshold(img_grey,120,255,cv2.THRESH_BINARY_INV)
cv2.imshow("image 1", thresh1)
cv2.imshow("image 2", thresh)
cv2.waitKey(0)
Any idea to segment the grey region ? I thought some sort of subtracting the white and black region using raw image but not working.
CodePudding user response:
You need to place different threshold values but the same binary operation. Then try subtracting the result:
ret, thresh1 = cv2.threshold(img_grey,20,255,cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_grey,235,255,cv2.THRESH_BINARY)
result = thresh1 - thresh2
cv2.imshow("Result", result)
cv2.waitKey(0)
See the images of thresh1
and thresh2
to understand what's going on.
And if you are worried about how to subtract use cv2.subtract(), it handles pixel range:
result2 = cv2.subtract(thresh1, thresh2)
cv2.imshow("Result2", result2)
cv2.waitKey(0)
CodePudding user response:
Here are two ways to do that in Python/OpenCV/Numpy. The first method uses cv2.inRange() color thresholding. The second method use Numpy. Both threshold so that the gray region becomes white and the rest black.
Input:
import cv2
import numpy as np
# read input
img = cv2.imread("white_gray_black.png")
# Method 1 Use cv2.inRange
low = (127,127,127)
high = (127,127,127)
mask1 = cv2.inRange(img, low, high)
cv2.imwrite("white_gray_black_mask1.png",mask1)
# Method 2 Use Numpy
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask2 = img.copy()
mask2[gray==127] = 255
mask2[gray!=127 ] = 0
cv2.imwrite("white_gray_black_mask2.png",mask2)
cv2.imshow('mask1', mask1)
cv2.imshow('mask2', mask2)
cv2.waitKey(0)
cv2.destroyAllWindows()
Result 1:
Result 2:
If you need to preserve the gray, then the second method is easier.
# Method 3 Use Numpy
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask3 = img.copy()
mask3[gray==127] = 127
mask3[gray!=127 ] = 0
cv2.imwrite("white_gray_black_mask3.png",mask3)
cv2.imshow('mask3', mask3)
cv2.waitKey(0)
cv2.destroyAllWindows()